Java Operator

Like any other programming language. Java has almost all popular operators.

Java provides a rich set of operators to manipulate variables

What is operator?

Operator are used to perform operation on value and variable

For example,
+ an arithmetic operator use for add two value

Example :

public class MyClass {
  public static void main(String[] args) {
    int x = 100 + 50;
    System.out.println(x);
  }
}
150

There are following type of the operator available in java.

  • Assignment Operator
  • Arithmetic Operator
  • Bitwise Operator
  • Comparison Operator
  • Logical Operator
  • Unary Operator
  • Conditional operator or Ternary Operator

Assignment Operator:

Assignment operator is use for assign value to the variable

Example:

In the example, we use the assignment operator    =   to assign the value 30  to a variable called  a   and 20 to b

1.	class Assignment{  
2.	public static void main(String args[]){  
3.	int a=30;  
4.	int b=20;  
5.	System.out.println(a);  
6.	System.out.println(b);  
7.	}}  
14
16

Arithmetic operator:

The arithmetic operator is used to performing a mathematical operation on value and variable

For Example:

  *     is a multiplication operator

public class MyClass {
  public static void main(String[] args) {
    int x = 5;
    int y = 3;
    System.out.println(x * y);
  }
}
15

List of all Arithmetic operator:

int A = 10
int B = 20


Bitwise operator:

Bitwise operator treat their operator as a set of 32 bits ( zero  and one), rather than treating them as decimal, hexadecimal, or octal number

Assume if a = 60 and b = 13; now in binary format they will be as follows

a = 0011 1100

b = 0000 1101

—————–

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a  = 1100 0011

For example:

 &  is a bitwise operator

The bitwise & operator always checks both conditions whether the first condition is true or false.

1.	class OperatorExample{  
2.	public static void main(String args[]){  
3.	int a=10;  
4.	int b=5;  
5.	int c=20;  
6.	System.out.println(a<b&&a<c); //false && true = false  
7.	System.out.println(a<b&a<c);  //false & true = false  
8.	}}  
false
false

List of Bitwise operator:


Comparison Operator or Relational Operator:

These operators compare the two variables and return a logical value based on whether the comparison is true or false.

Example :

class Relational {
    public static void main(String[] args) {
    	
        int number1 = 10, number2 = 5;

        if (number1 > number2) {
            System.out.println("number1 is greater than number2.");
        }
        else {
            System.out.println("number2 is greater than number1.");
        }
    }
}
Number1 is greater than number2.

List of relational operator:

int a = 10;
int b = 20;


Logical Operator:

The logical operator is used with the Boolean value. When two Logical value is compared, it returns another logical value. However, is one of the operands is a non-Boolean value, the operation may return the non-Boolean value.

Example:

class LogicalOperator {
    public static void main(String[] args) {
    	
        int number1 = 1, number2 = 2, number3 = 9;
        boolean result;
    	
        // At least one expression needs to be true for the result to be true
        result = (number1 > number2) || (number3 > number1);

        // result will be true because (number1 > number2) is true
        System.out.println(result);
    			
        // All expression must be true from result to be true	
        result = (number1 > number2) && (number3 > number1);

        // result will be false because (number3 > number1) is false
        System.out.println(result);
    }
}
true
false

List of Logical Operators:


Unary operator:

The unary operator performs operations on only one operand.

Unary operators are used to perform various operations i.e.:

  • incrementing/decrementing a value by one
  • negating an expression
  • inverting the value of a boolean

Example:

1.	class OperatorExample{  
2.	public static void main(String args[]){  
3.	int x=10;  
4.	System.out.println(x++);//10 (11)  
5.	System.out.println(++x);//12  
6.	System.out.println(x--);//12 (11)  
7.	System.out.println(--x);//10  
8.	}}  
10
12
12
10

Conditional Operator or Ternary Operator:

  • It is also known as Ternary operator.
  • Ternary operator used as one liner replacement for if-then-else statement
  • Conditional operator is the only conditional operator which takes three operands.
  • The goal of the operator is to decide, which value should be assigned to the variable.

The syntax of ternary operator is

variable = Expression ? expression1 : expression2

Example:

class ConditionalOperator {
    public static void main(String[] args) {
    	
        int februaryDays = 29;
        String result;
    	
         result =  (februaryDays == 28) ? "Not a leap year" : "Leap year";
         System.out.println(result);
    }
}
Leap year



SHARE THIS PAGE





Important Link