Operators in Java Program – Java Tutorial
Problem Statement
List and Explain different operators in Java with examples.
Solution:
Following is the list of operators that are used Java programming language:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Increment Operator
- Decrement Operator
Type of Operator | Operator | Meaning | Example |
Arithmetic Operator | + | Addition or Unary Plus | c=a+b |
– | Subtraction or Unary Minus | c=a-b | |
* | Multiplication | c=a*b | |
/ | Division | c=a/b | |
% | Modulus | c=a%b | |
Relational Operator | < | Less than | a<10 |
> | Greater than | a>10 | |
<= | Less than or equal to | a<=10 | |
>= | Greater than or equal to | a>=10 | |
== | Equal to | a==10 | |
!= | Not equal to | a!=10 | |
Logical Operator | && | Logical And | 0&&1 |
|| | Logical Or | 0||1 | |
Assignment Operator | = | is assigned to | a=10 |
Increment Operator | ++ | Increment by 1 | a++ or ++a |
Decrement Operator | – – | Decrement by 1 | a– or –a |
Conditional Operators in Java
The conditional operator is “?”
The syntax of the conditional operator is
Condition?expression1:expression2
Where expression1 denotes the true condition and expression2 denotes the false condition.
For example:
a >b?true:false
This means that if a is greater than b then the expression will return the value true otherwise it will return false.
Operator Precedence in Java
The precedence relation specifies which operation must be done first during the expression evaluation. The following table denotes the precedence relation. The operators are arranged from highest priority to lowest priority. The operators in the same row indicate equal precedence.
Program for demonstrating arithmetic operators in Java
class ArithOperDemo { public static void main(String[] args) { System.out.println("\n Performing arithmetic operations "); int a=10, b=20, c; System.out.println("a= "+a); System.out.println("b= "+b); c=a+b; System.out.println("\n The Addition of two numbers is "+c); c=a-b; System.out.println("\n The Subtraction of two numbers is "+c); c=a*b; System.out.println("\n The Multiplication of two numbers is "+c); c=a/b; System.out.println("\n The Division of two numbers is "+c); } }
Output
Performing arithmetic operations
a= 10 b= 20
The addition of two numbers is 30
The Subtraction of two numbers is -10
The Multiplication of two numbers is 200
The division of two numbers is 0
Program for demonstrating relational operators in Java
class RelOperDemo { public static void main(String[] args) { System.out.println("\n Performing Relational operations "); int a=10,b=20,c=10; System.out.println("a= "+a); System.out.println("b= "+b); System.out.println("\n The a<b is "+(a<b)); System.out.println("\n The a>b is "+(a>b)); System.out.println("\n The a= =c is "+(a==c)); System.out.println("\n The a< =b is "+ (a< =b)); System.out.println("\n The a> =c is "+(a> =c)); System.out.println("\n The a!=b is "+(a!=b)); } }
Output
Performing Relational operations
a= 10
b= 20
The a<b is true
The a>b is false
The a= =c is true
The a< =b is true
The a> =c is true
The a! =b is true
Program for demonstrating increment and decrement Operators in Java
class IncrDecrOperDemo { public static void main(String[] args) { System.out.println("\n Performing increment and decrement operations "); int a=11,b=22; System.out.printin("a= "+a); System.out.println("b= "+b); System.out.println(An Pre-Incrementing a "+ + +a); System.out.println("\n Post-Incrementing b "+ b++); System.out.println(An After this statement the value of b is "+ b); } }
Output
Performing increment and decrement operations
a= 11 b= 22
Pre-Incrementing a 12
Post-Incrementing b 22
After this statement, the value of b is 23
Short Circuit Operator in Java
The && and || are the short circuit operators in Java. These operators are called short-circuited operators because the outcome of the expression can be determined by the left operand only then the right operand won’t be evaluated and hence the order of evaluation will always be from left to right irrespective of whether you place the operand within parentheses or not.
For example:
Suppose we have an expression as ‘operand1 && operand2’ and ‘operand1’ is evaluated to be false, then there is no point evaluating the right operand ‘operand2’ irrespective of whether that operand is true or false the expression will always be false only.
Suppose we have an expression as ‘operand1 || operand2’ and ‘operand1’ is evaluated to be true, then there is no point evaluating the right operand ‘operand2’ irrespective of whether that operand is true or false the expression will always be true only.
Shift Operators in Java
There are left, right shift, and unsigned right shift operators in Java.
Left shift operator:
The left shift operator is denoted by <<. The syntax of the left shift is
value << n
where the bits in the value are shifted by n positions to the left.
For example:
5 << 2 will result in 0000 0101 << 2 –> 0000 1010 –> 0001 0100 = 20
Right shift operator:
The right shift operator is denoted by >>. The syntax of the right shift is
Value >> n
where the bits in the value are shifted by n positions to the right.
For example
5 >> 2 will result in 0000 0101 >> 2 –> 0000 0010 –> 0000 0001 = 1
-5 >> 2 will result in 1111 1011 >> 2 –> 11111101 –> 11111110 = -2
Unsigned right shift operator:
The unsigned right shift operator is denoted by >>>.
The syntax of the unsigned right shift operator is
Value >>> n
where the bits in the value are shifted by n positions to the right and fills MSB with 0.
For example 5 >>> 2 will result in
0000 0101 >>> 2 –> 0000 0010 –> 0000 0001 = 1
Summary:
In this article, we understood the operator in Java Program – Java Tutorial. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and the YouTube channel for video tutorials.