Operators

Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. Java has many types of operators as:

1.Assignment operators
2.Mathematical operators
3.Auto increment and decrement operators
4.Relational operators
5.Logical operators
6.Bitwise operators
7.Shift operators
8.Ternary if-else operator
9.String operator + and +=

1. Assignment Operators

Assignment is performed with the operator =. It means “Take the value of the right-hand side (often called the rvalue) and copy it into the left-hand side (often called the lvalue)”.

class base {
	void function() {
		System.out.println("inside base function");
	}
}
public class AssigOperators {
	public static void main(String[] args) {
		base b1 = new base(); // assignment operator used to allocate memory to
								// object
		base b2 = new base();
		int a = 1; // assignment operator used to assign 1 to a
		b2 = b1; // aliasing (another use of assignment operator)
	}
}

2.Mathematical operators

The basic mathematical operators are the same as the ones available in most programming languages: addition (+), subtraction (-), division (/), multiplication (*) and modulus (%, which produces the remainder from integer division). Integer division truncates, rather than rounds, the result.

Java also uses the shorthand notation from C/C++ that performs an operation and an assignment at the same time. This is denoted by an operator followed by an equal sign, and is consistent with all the operators in the language (whenever it makes sense). For example, to add 4 to the variable x and assign the result to x, use: x += 4.
This example shows the use of the mathematical operators:

public class MathOperators {
	public static void main(String[] args) {
		int iresult, irem;
		double dresult, drem;
		iresult = 10 / 3;
		irem = 10 % 3;
		dresult = 10.0 / 3.0;
		drem = 10.0 % 3.0;
		System.out.println("Result and remainder of 10 / 3: " + iresult + " "
				+ irem);
		System.out.println("Result and remainder of 10.0 / 3.0: " + dresult
				+ " " + drem);
	}
}

3.Auto increment and decrement operators.

the following program demonstrates this operator

public class autoIncDec {
	public static void main(String[] args) {
		int i = 1;
		System.out.println(++i); // pre increment i before printing, this will
									// print 2
		System.out.println(i++); // post increment i after printing, this will
									// print 2
		System.out.println(i); // this will print 3
		System.out.println(--i); // pre decrement i before printing, will print
									// 2
		System.out.println(i--); // post decrement i after printing, this will
									// print 2
		System.out.println(i); // this will print 1
	}
}

4.Relational operators

Relational operators are the operators which define a relation between two or other values, and based on these relations the corresponding statements will be executed. Various relational operators are demonstrated in the following table.

Operator Meaning
==  Equal to
!=  Not equal to
Greater than
Less than
>=  Greater than or equal to
<=  Less than or equal to

Example for the relational operators is :

public class relationalOperators {
	public static void main(String[] args) {
		int i, j;
		boolean b1, b2;
		i = 10;
		j = 11;
		if (i < j)
			System.out.println("i < j");
		if (i <= j)
			System.out.println("i <= j");
		if (i != j)
			System.out.println("i != j");
		if (i == j)
			System.out.println("this won't execute");
		if (i >= j)
			System.out.println("this won't execute");
		if (i > j)
			System.out.println("this won't execute");
	}
}

5.Logical operators

Each of the logical operators AND (&&), OR (||) and NOT (!) produces a boolean value of true or false based on the logical relationship of its arguments.

public class LogicalOperators {
	public static void main(String args[]) {
		int i = 10;
		int j = 20;

		if ((i == 10) && (j == 20)) // logical AND, all conditions must be true
									// in order to execute with && operator
		{
			System.out.println("&& operator demonstrated");
		}
		if ((i > 10) || (j == 20)) // if first statement (i>10) goes returns
									// false then 2nd statement (j==20) will be
									// executed
		{
			System.out.println("|| or operator demonstrated");
		}

		if (!(i == 10)) // ! will negate the result (true to false or false to
						// true)
		{
			System.out.println("will not executed");
		}
	}
}

6.Bitwise operators

The bitwise operators allow you to manipulate individual bits in an integral primitive data type. Bitwise operators perform Boolean algebra on the corresponding bits in the two arguments to produce the result.
The bitwise operators come from C’s low-level orientation, where you often manipulate hardware directly and must set the bits in hardware registers. Java was originally designed to be embedded in TV set-top boxes, so this low-level orientation still made sense

public class BitWiseOperators {
	public static void main(String args[]) {
		int a = 60; /* 60 = 0011 1100 */
		int b = 13; /* 13 = 0000 1101 */
		int c = 0;

		c = a & b; /* 12 = 0000 1100 */
		System.out.println("a & b = " + c);

		c = a | b; /* 61 = 0011 1101 */
		System.out.println("a | b = " + c);

		c = a ^ b; /* 49 = 0011 0001 */
		System.out.println("a ^ b = " + c);

		c = ~a; /*-61 = 1100 0011 */
		System.out.println("~a = " + c);
	}
}

7.Shift operators

The shift operators also manipulate bits. They can be used solely with primitive, integral types. The left-shift operator (<<) produces the operand to the left of the operator after it has been shifted to the left by the number of bits specified to the right of the operator (inserting zeroes at the lower-order bits). The signed right-shift operator (>>) produces the operand to the left of the operator after it has been shifted to the right by the number of bits specified to the right of the operator. The signed right shift >> uses sign extension: If the value is positive, zeroes are inserted at the higher-order bits; if the value is negative, ones are inserted at the higher-order bits. Java has also added the unsigned right shift >>>, which uses zero extension: Regardless of the sign, zeroes are inserted at the higher-order bits. This operator does not exist in C or C++.

8.Ternary if-else operator

The ternary operator, also called the conditional operator, is unusual because it has three operands. It is truly an operator because it produces a value, unlike the ordinary if-else statement. The expression is of the form:

boolean expression ? true value : false value
The above syntax indicates that, if the condition before the ? resulted in true then the statement after ? will be executed otherwise on false the statement after : will be executed.
Following example will demonstrate this :

public class ConditionalOperator {
	public static void main(String[] args) {
		int a = 10;
		int b = a < 10 ? 20 : 10; // if a <10 then 20 is assigned to b else 10
									// is assigned to b
		System.out.println("b is " + b);
	}
}

9.String operator + and +=

There’s one special usage of an operator in Java: The + and += operators can be used to concatenate strings. It seems a natural use of these operators even though it doesn’t fit with the traditional way that they are used.

public class StringConcat {
	public static void main(String[] args) {
		String s1 = "Hello";
		String s2 = "nice" + "World"; // + is used to concatenate two strings
		System.out.println(s2);

		String s3;
		s3 = s1 += s2; // s2 is appended to s1 and then assigned to s3
		System.out.println(s3);

	}
}