Operators in java

Let’s learn operators in java.

Operators in java

Operator(+,-,*,/ etc) is a symbol used to perform operations on variables. Here are types of operators,

Arithmetic operators

Arithmetic operator is used in mathematical expressions to perform division, addition, subtraction and multiplication.

public class ArithmeticOperatorDemo
{
   public static void main(String[] args)
   {
      int i = 25;
      int j = 5;
      System.out.println(i + j);
      System.out.println(i - j);
      System.out.println(i * j);
      System.out.println(i / j);
      System.out.println(i % j);
   }
}

Output:

30
20
125
5
0


Unary operator in java

Unary operator needs only one operand to increment, decrement, etc. Here’s an example on ‘NOT’ (!) operator.

public class JavaUnaryOperator
{
   public static void main(String[] args)
   {
      boolean bool = true;
      int a = 14, b = 5;
      System.out.println("Before using NOT operator(!): " + bool);
      System.out.println("a = " + a);
      System.out.println("b = " + b);
      System.out.println("After using using NOT operator(!): " + !bool);
      System.out.println("!(a < b) = " + !(a < b));
      System.out.println("!(a > b) = " + !(a > b));
   }
}


Output:

Before using NOT operator(!): true
a = 14
b = 5
After using using NOT operator(!): false
!(a < b) = true !(a > b) = false


Here’s an example on unary minus operator(-).

public class JavaUnaryOperator
{
   public static void main(String[] args)
   {
      int a = 5;
      System.out.println("Given number : " + a);
      a = -a;
      System.out.println("After using unary minus operator(-) : " + a);
   }
}


Output:

Given number : 5
After using unary minus operator(-) : -5


Let’s see an example on unary increment (++) operator.

public class JavaUnaryOperator
{
   public static void main(String[] args)
   {
      int a = 7;
      System.out.println("post-increment = " + a++);
      System.out.println("pre-increment = " + ++a);
   }
}


Output:

post-increment = 7
pre-increment = 9


Let’s see an example on unary decrement (–) operator.

public class JavaUnaryOperator
{
   public static void main(String[] args)
   {
      int a = 7;
      System.out.println("post-decrement = " + a--);
      System.out.println("a = " + a);
      System.out.println("pre-decrement = " + --a);
   }
}


Output:

post-decrement = 7
a = 6
pre-decrement = 5


Logical operator in java

Logical operators combine more than two conditions which returns boolean output.

Let’s see an example on Logical AND (&&) operator.

public class LogicalANDOperator 
{
   public static void main(String[] args) 
   {
      System.out.println((40 > 20) && (70 > 50));
      System.out.println((30 > 10) && (60 < 40));
   }
}


Output:

true
false

Let's see an example on Logical OR (||) operator.

public class LogicalOROperator 
{
   public static void main(String[] args) 
   {
      System.out.println((70 < 40) || (90 > 60));
      System.out.println((80 < 20) || (100 < 50));
   }
}


Output:

true
false


Let's see an example on Logical NOT (!) operator.

public class LogicalNOTOperator 
{
   public static void main(String[] args) 
   {
      System.out.println(!(70 == 40));
      System.out.println(!(70 > 40));
   }
}


Output:

true
false


Ternary operator in java

Ternary or conditional operator in java has three operands expression1, expression2 and expression3. Here expression2 is executed only if expression1 is true else expression3 is executed. Ternary operator is shorthand for if-else statement.

Syntax:

variable = expression1 ? expression2(if true) : expression3(if false)

Let's see ternary operator in java example.

public class TernaryOperatorDemo
{
   public static void main(String[] args)
   {
      int a = 50, b = 500, bigger;
      System.out.println("First number: " + a);
      System.out.println("Second number: " + b);
      bigger = (a > b) ? a : b;
      System.out.println("Bigger number is = " + bigger);
   }
}


Output:

First number: 50
Second number: 500
Bigger number is = 500


Let's see another example on ternary operator in java.

public class TernaryOperatorDemo
{
   public static void main(String[] args) 
   {
      int a = 50, b = 700, output;
      System.out.println("First number: " + a);
      System.out.println("Second number: " + b); 
      output = (a > b) ? (a + b) : (a - b);
      System.out.println("Output is = " + output);
   }
}


Output:

First number: 50
Second number: 700
Output is = -650


Now let's see precedence of above discussed operators.

operators in java