Java Operators

These are used to manipulate primitive data types. Java operators can be classified as Unary, Binary and Ternary i.e. taking one, two or three arguments. A Unary operator may appear before its argument or after its argument. A Binary or Ternary operator appears between its arguments.

Arithmetic Operators

  1. + for Addition (Add two numbers) or Concatenation (Add two strings)
  2. - for Subtraction
  3. * for Multiplication
  4. / for Division
  5. % for modulo Division (Remainder)
Arithmetic Operators Example
/*  Arithmetic Operators Example  */
/*  Save with file name ArithmeticOperators.java  */

public class ArithmeticOperators
{
	public static void main(String args[])
	{
		System.out.println();
		System.out.println("Arithmetic Operators Example");
		System.out.println("============================");
		System.out.println();
		System.out.println("Assignment (20+10) = " + (20+10)); //Assignments
		System.out.println("Subtraction (20-10) = " + (20-10)); //Subtraction
		System.out.println("Multiplication (20X10) = " + (20*10)); //Multiplication
		System.out.println("Division (20/10) = " + (20/10)); //Disivion
		System.out.println("Modulo Division  (20%12) = " + (20%12)); //Modulo Division
	}
}
                        
Assignment Operator Example
/*  Assignment Operator Example  */
/*  Save with file name AssignmentOperator.java  */

public class AssignmentOperator
{
	public static void main(String args[])
	{
		int a; //VARIABLE DECLARATION
		System.out.println();
		System.out.println("Assignment Operator Example");
		System.out.println("===========================");
		System.out.println();
		a = 10; //ASSIGNMENT
		System.out.println("Value of a After Assignment : " + a); //PRINTS TEN

	}
}
                        

Arithmetic Assignment Operators

  1. +=
  2. -=
  3. *=
  4. /=
Arithmetic Assignment Operators
/*  Arithmetic Assignment Operators Example  */
/*  Save with file name ArithmeticAssignmentOperators.java  */

public class ArithmeticAssignmentOperators
{
	public static void main(String args[])
	{
		int a = 1, b = 7; //VARIABLE DECLARATION AND ASSIGN A VALUE
		a +=10;
		System.out.println();
		System.out.println("Arithmetic Assignment Operators Example");
		System.out.println("=======================================");
		System.out.println();
		System.out.println("Addition : " + a);
		b -= 2;
		System.out.println("Subtraction : " + b);
		float c; //YOU CAN ALSO DECLARE A VARIABLE AT ANY PLACE
		c = 3;
		c *= 5;
		System.out.println("Multiplication : " + c);
		double d = 20;
		d /= 10;
		System.out.println("Division : " + d);
	}
}
                        

Logical Operators

Logical operators return true or false value only i.e. the result is always a boolean data type.

  1. && - AND
  2. || - OR
  3. ! - NOT
Logical Operators
/*  Logical Operators Example  */
/*  Save with file name LogicalOperators.java  */

public class LogicalOperators
{
	public static void main(String args[])
	{
		//VARIABLES DECLARATION SAME TYPE WITH COMMA OPERATOR
		boolean T = true, F = false;
		System.out.println();
		System.out.println("Logical Operators Example");
		System.out.println("=========================");
		System.out.println();
		System.out.println("T AND F : " + (T && F)); //TRUE AND FALSE = FALSE
		System.out.println("T OR F : " + (T || F)); //TRUE OR FALSE = TRUE
		System.out.println("NOT T : " + (!T)); //NOT TRUE = FALSE
	}
}
                        

Relational Operators

These are used to compare two values or two objects.

  1. == - EQUAL TO
  2. != - NOT EQUAL TO
  3. > - GREATER THAN
  4. < - LESS THAN
  5. >= - GREATER THAN OR EQUAL TO
  6. <= - LESS THAN OR EQUAL TO
Relational Operators
/*  Relational Operators Example  */
/*  Save with file name RelationalOperators.java  */

public class RelationalOperators
{
	public static void main(String args[])
	{
		System.out.println();
		System.out.println("Relational Operators Example");
		System.out.println("============================");
		System.out.println();
		System.out.println("1 Equal to 1 : " + (1==1));
		System.out.println("1 Not Equal to 1 : " + (1!=1));
		System.out.println("3 Greater Than 2 : " + (3>2));
		System.out.println("1 Less Than 2 : " + (1<2));
		System.out.println("3 Greater Than or Equal to 2 : " + (3>=2));
		System.out.println("1 Less Than or Equal to 2 : " + (1<=2));
	}
}
                        

Increment and Decrement Operators

  1. ++ - Increment One
  2. -- - Decrement One
These are two types
  1. Post Increment or Post Decrement
  2. Pre Increment or pre Decrement
Increment and Decrement Operators
/*  Increment and Decrement Operators Example  */
/*  Save with file name IncrementDecrementOperators.java  */

public class IncrementDecrementOperators
{
	public static void main(String args[])
	{
		int a = 10, b = 20, c = 30, d = 40;
		System.out.println();
		System.out.println("Increment and Decrement Operators Example ");
		System.out.println("=========================================");
		System.out.println();
		//VALUE IS 10 AFTER THIS STATEMENT a VALUE IS 11
		System.out.println("Post Increment : " + (a++));
		System.out.println("After Post Increment : " + a);
		//VALUE IS 20 AFTER THIS STATEMENT b VALUE IS 19
		System.out.println("Post Decrement : " + (b--));
		System.out.println("After Post Decrement : " + b);
		System.out.println("Pre Increment : " + (++c)); //VALUE IS 31
		System.out.println("Pre Decrement : " + (--d)); //VALUE IS 39
	}
}
                        

Bitwise Operators

These are used to manipulate the contents of variables at the bit level. These variables must be of numeric data type (char, short, int, or long).

  1. &
  2. |
  3. ^
  4. <<
  5. >>
  6. >>>
Bitwise Operators
/*  Bitwise Operators Example  */
/*  Save with file name BitwiseOperators.java  */

public class BitwiseOperators
{
	public static void main(String args[])
	{
		int a = 10, b = 20;
		System.out.println();
		System.out.println("Bitwise Operators Example ");
		System.out.println("=========================");
		System.out.println();
		System.out.println("a & b : " + (a & b));
		System.out.println("a | b : " + (a | b));
		System.out.println("a ^ b : " + (a ^ b));
		System.out.println("~a : " + (~a));
		System.out.println("a << b : " + (a << b));
		System.out.println("a >> b : " + (a >> b));
		System.out.println("a >>> b : " + (a >>> b));
		//There is no unsigned left shift operator
	}
}
                        

Conditional (Ternary) Operator

  1. ? - Question Mark
  2. : - colon

The Conditional operator is ternary i.e. it takes three arguments. The operator evaluates the first argument and, if true then evaluates the second argument. If the first argument evaluates to false, then the third argument is evaluated. The conditional operator is the expression equivalent of the if-else statement. The conditional expression can be nested and the conditional operator associates from right to left

Conditional (Ternary) Operator
/*  Conditional Operator Example  */
/*  Save with file name ConditionalOperator.java  */

public class ConditionalOperator
{
	public static void main(String args[])
	{
		int a = 10, b = 20, c, d;
		c=d=0; //ASSIGN A VALUE TO c, d VARIABLES AT A TIME
		System.out.println();
		System.out.println("Conditional (Ternary) Operator Example ");
		System.out.println("======================================");
		System.out.println();
		c = (a > b ? 100 : b); //c VALUE IS 20
		d = (a == 10 ? 0 : 15); //d VALUE IS 0
		System.out.println("Value of c : " + c);
		System.out.println("Value of d : " + d);
	}
}