Download Java Operators in Formats
QR Code for this Page
Java Operators
Complete Tutorial on 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.

What is Operator in Java ?

An operator in Java, is a special symbols performing specific operations on one, two or three operands and then returning a result. The operators are classified and listed according to precedence order. Java operators are generally used to manipulate primitive data types. The Java operators are classified into eight different categories: assignment, arithmetic, relational, logical, bitwise, compound assignment, conditional and type comparison operators.

What is Assignment Operator in Java ?

An Assignment operator in Java, Assign the value on its right to the operand on its left. It is denoted by the symbol =. Assignment operator is used the following ways.

  • int a = 7, b;
  • b = a;

Java Assignment Operator Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*  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);
  }
}

Arithmetic Operators in Java

There are eight arithmetic operators available in Java. They perform addition, subtraction, multiplication, division, modulo (or remainder), increment (or add 1), decrement (or subtract 1), and negation. + Additive operator (also used for String concatenation), - Subtraction operator, * Multiplication operator, / Division operator, % remainder operator.

What are the Arithmetic Operators used in Java ?

Java arithmetic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations. They are +, -, *, /, %.

Following are the List of All 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*  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 Arithmetic Assignment Operators

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

Java Arithmetic Assignment Operators Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*  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 either true or false.

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

What does " || " mean in Java ?

It means Logical-OR or simply OR like A or B. If A is true, it doesn't evaluate B. A || B in this case it is automatically true.

Java Logical Operators Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*  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. In other words, the relational operators are used to compare 2 or more objects. There are six relational operators in Java.

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

Java Relational Operators Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*  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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*  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). There are seven bitwise operators. They are AND, OR, Exclusive-OR, compliment, Left-shift, Signed Right-Shift, and Unsigned Right-shift.

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

Java Bitwise Operators Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*  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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*  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);
  }
}

Special Operators in Java

Java provides some special operators as given below.

  1. instanceof Operator
  2. . Dot Operator
  3. new Operator

instanceof Operator

The instanceof operator is used to know if an object is an instance of a particular class or not. This operator returns true if an object given at the left hand side is an instance of the class given at right hand side. Otherwise, it returns false.

. Dot Operator

The . Dot operator is used to access the variables and methods (to select members of a class or object instance) of a class. This dot operator is also used to access classes, packages etc.

new Operator

The new keyword is a Java operator that creates the object. The new operator is followed by a call to a constructor, which initializes the new object. The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor. The new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The new operator returns a reference to the object it created. This reference is usually assigned to a variable of the appropriate type.

What are the type comparison operators in Java ?

The instanceof operator compares an object to a specified type. This operator can be used to test if an object is an instance of a class, subclass or a particular interface.

Download Java Operators in Formats