Operators In Java

Operators in java are special symbols that perform special operations on variables and values. In the below example, we used + operator to add two numbers.

Example:

public class AddTwoNumber {  
    public static void main(String args[]){  
    	int num1=45;
    	int num2=85;
    	int result=num1+num2;
    	System.out.println("Addition of Two Number using + 
operator is: "+result);
    }  
}

output:

Addition of Two Number using + operator is: 130

List Of Operators In Java

  1. Arithmetic Operator
  2. Comparison Operators
  3. Logical Operator
  4. Assignment Operator
  5. Conditional  Operator
  6. Unary Operator
operators in java

Arithmetic Operator

Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, and division in java.

Operator Name Description Example
+ Addition Used for add two number( also used for String concatenation )a+b
Subtraction Used for subtract one value from another a-b
* Multiplication Multiplication operator a*b
/ Division Division operator a/b
% Modulus Remainder  operator a%b

Example:

public class ArithmeticExample {  
    public static void main(String args[]){  
    	int num1=95;
    	int num2=85;
    	System.out.println(num1+num2);  
    	System.out.println(num1-num2); 
    	System.out.println(num1*num2); 
    	System.out.println(num1/num2); 
    	System.out.println(num1%num2);
    }  
}

output:

180
10
8075
1
10

Comparison Operators

Comparison operators are used to compare two values

Operator Name Example
== equal to a==b
!= not equal to a!=b
> greater than a>b
>= greater than or equal to a>=b
<less than a<b
<= less than or equal to a<=b

Example:

public class ComparisonExample {  
    public static void main(String args[]){  
    	int value1 = 98;
        int value2 = 85;
        if(value1 == value2)
            System.out.println("value1 == value2");
        if(value1 != value2)
            System.out.println("value1 != value2");
        if(value1 > value2)
            System.out.println("value1 > value2");
        if(value1 > value2)
            System.out.println("value1 >= value2");
        if(value1 < value2)
            System.out.println("value1 < value2");
        if(value1 <= value2)
            System.out.println("value1 <= value2");
    }
}

output:

value1 != value2
value1 > value2
value1 >= value2

Logical Operator

The logical operators || (Logical-OR) and && (Logical-AND) operater on boolean expressions.

  1. && :  returns true when both conditions are true.
  2. || :  returns true if at least one condition is true.

Example:

public class ConditionalExample {  
    public static void main(String args[]){  
    	int value1 = 98;
        int value2 = 85;
        if((value1 == 98) &amp;&amp; (value2 == 85))
            System.out.println("value1 is 98 AND value2 is 85");
        if((value1 == 98) || (value2 == 75))
            System.out.println("value1 is 98 OR value2 is 85");
    }
}j

output:

value1 is 98 AND value2 is 85
value1 is 98 OR value2 is 85

In the above example both if condition is run. Because in first if condition, both values are true and in second if condition only one value is true.

Assignment Operator

Assignment operators are used to assigning a value in a variable.

Example:

String name="Study4Geeks;
int value=45;
int value2+=45;
Operator Example1We can also write
=x = 45x = 45
+=x += 45 x = x + 45
-=x -= 45 x = x – 45
*=x *= 45 x = x * 45
/=x /= 45 x = x / 45
%=x %= 45 x = x % 45
&=x &= 45 x = x & 45
|=x |= 45 x = x | 45
^=x ^= 45 x = x ^ 45
>>=x >>= 45 x = x >> 45
<<=x <<= 45 x = x << 45

Unary Operator

Unary Operator is used for incrementing/decrementing a value by one. ++ , – – is the example of unary Operator.

Example:

public class Example {  
    public static void main(String args[]){  
    	int num = 50;
       System.out.println(++num);
       System.out.println(--num);
    }
}

output:

51
50

Conditional  Operator

This operator is a short form of if-else statement.

Syntax:

variable = (condition)? value if true: value if false

Example:

public class Example {
	public static void main(String args[]){  
    	int num = 50;
    	int result = (num == 50) ? 150: 170;
        System.out.println(result); // 150
        result = (num == 45) ? 150: 170;
        System.out.println(result); // 170
    }
}

output:

150
170
Do you know?
Operators In Java This article is contribute by Study4Geeks If you want share your knowledge on Study4Geeks then go to login section.