Download Java Operators in Formats
QR Code for this Page
Java Operators

Java Operators - Operators in Java

Operators in Java

Java Operators 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 in Java

for Addition (Add two numbers) or Concatenation (Add two strings)
for Subtraction
for Multiplication
for Division
for modulo Division (Remainder)

Java Arithmetic Operators Example

/*  Java Arithmetic Operators Example
    Save with file name ArithmeticOperators.java    */

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

Java Assignment Operator Example

/*  Java Assignment Operator Example
    Save with file name AssignmentOperator.java	*/

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

Java Arithmetic Assignment Operators

  • +=
  • -=
  • *=
  • /=

Java Arithmetic Assignment Operators Example

/*  Java Arithmetic Assignment Operators Example
    Save with file name ArithmeticAssignmentOperators.java  */

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

Java Logical Operators

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

  • && - AND
  • || - OR
  • ! - NOT

Java Logical Operators Example

/*  Java 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("Logical Operators Example");
		//TRUE AND FALSE = FALSE
		System.out.println("T AND F : " + (T && F));
		//TRUE OR FALSE = TRUE
		System.out.println("T OR F : " + (T || F));
		//NOT TRUE = FALSE
		System.out.println("NOT T : " + (!T));
	}
}

Java Relational Operators

Java Relational Operators are used to compare two values or two objects.

  • == EQUAL TO
  • != NOT EQUAL TO
  • > GREATER THAN
  • < LESS THAN
  • >= GREATER THAN OR EQUAL TO
  • <= LESS THAN OR EQUAL TO

Java Relational Operators Example

/*  Java Relational Operators Example
    Save with file name RelationalOperators.java    */

public class RelationalOperators
{
	public static void main(String args[])
	{
		System.out.println("Java Relational Operators Example");
		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));
	}
}

Java Increment and Decrement Operators

  • ++ Increment One
  • -- Decrement One
Java Increment and Decrement Operators are two types
  • Post Increment or Post Decrement
  • Pre Increment or Pre Decrement

Java Increment and Decrement Operators Example

/*  Java 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("Java Increment and Decrement Operators Example");
		//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);
		//VALUE IS 31
		System.out.println("Pre Increment : " + (++c));
		//VALUE IS 39
		System.out.println("Pre Decrement : " + (--d));
	}
}

Java Bitwise Operators

Java Bitwise Operators 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).

  • &
  • |
  • ^
  • <<
  • >>
  • >>>

Java Bitwise Operators Example

/*  Java 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("Java Bitwise Operators Example");
		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
	}
}

Java Conditional (Ternary) Operator

  • ? - Question Mark
  • : - 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

Java Conditional (Ternary) Operator Example

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

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