0% found this document useful (0 votes)
96 views9 pages

Operators in Java

Operators in Java are symbols that are used to perform operations like arithmetic, logical, relational, etc. There are different types of operators including unary, arithmetic, relational, bitwise, logical, and assignment. Operator precedence determines the order in which operators are evaluated in an expression. Common operators include arithmetic operators for math operations, logical operators to combine conditional expressions, and assignment operators for assigning values.

Uploaded by

Ganesh Bommana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
96 views9 pages

Operators in Java

Operators in Java are symbols that are used to perform operations like arithmetic, logical, relational, etc. There are different types of operators including unary, arithmetic, relational, bitwise, logical, and assignment. Operator precedence determines the order in which operators are evaluated in an expression. Common operators include arithmetic operators for math operations, logical operators to combine conditional expressions, and assignment operators for assigning values.

Uploaded by

Ganesh Bommana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 9

Operators in Java

• Operator in Java is a symbol that is used to perform operations. For


example: +, -, *, / etc.
• There are many types of operators in Java which are given below:
1. Unary Operator,
2. Arithmetic Operator,
3. Shift Operator,
4. Relational Operator,
5. Bitwise Operator,
6. Logical Operator,
7. Ternary Operator and
8. Assignment Operator.
Java Operator Precedence
Operator Type Category Precedence

Unary postfix expr++ expr--


prefix ++expr --expr +expr -expr ~ !

Arithmetic multiplicative */%


additive +-
Shift shift << >> >>>
Relational comparison < > <= >= instanceof
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
Java Unary Operator
• The Java unary operators require only one operand. Unary operators are used to perform various
operations i.e.:
1. incrementing/decrementing a value by one
2. negating an expression
3. inverting the value of a boolean
• Java Unary Operator Example: ++ and --
public class OperatorExample{  
public static void main(String args[]){  
int x=10;  
System.out.println(x++);//10 (11)  
System.out.println(++x);//12  
System.out.println(x--);//12 (11)  
System.out.println(--x);//10  
}
}  
We call x++ as postincrement and ++x as preincrement.
In x++ the variable value is used first in the expression and then it is incremented after the operation.
In ++x the variable is incremented and then uses the new value in the expression. 
Here is an example that will explain about them:
int x = 5, y = 5;
System.out.println(++x); // outputs 6
System.out.println(x); // outputs 6
System.out.println(y++); // outputs 5
System.out.println(y); // outputs 6
Java Arithmetic Operators
• Java arithmetic operators are used to perform addition, subtraction, multiplication, and
division. They act as basic mathematical operations.
public class OperatorExample{  
public static void main(String args[]){  
int a=10;  
int b=5;  
System.out.println(a+b);//15  
System.out.println(a-b);//5  
System.out.println(a*b);//50  
System.out.println(a/b);//2  
System.out.println(a%b);//0  
}}  
Java AND Operator Example: Logical && and Bitwise &
• The logical && operator doesn't check the second condition if the first
condition is false. It checks the second condition only if the first one is true.
public class OperatorExample{  
public static void main(String args[]){  
int a=10;  
int b=5;  
int c=20;  
System.out.println(a<b&&a<c);//false && true = false  
System.out.println(a<b&a<c);//false & true = false  
}}  
Java OR Operator

• The logical || operator doesn't check the second condition if the first condition is true. It checks the second condition only
if the first one is false.
public class OperatorExample{  
public static void main(String args[]){  
int a=10;  
int b=5;  
int c=20;  
System.out.println(a>b||a<c);//true || true = true  
System.out.println(a>b|a<c);//true | true = true  
//|| vs |  
System.out.println(a>b||a++<c);//true || true = true  
System.out.println(a);//10 because second condition is not checked  
System.out.println(a>b|a++<c);//true | true = true  
System.out.println(a);//11 because second condition is checked  
}}  
Java Assignment Operator
• Java assignment operator is one of the most common operators. It is used to assign the
value on its right to the operand on its left.
public class OperatorExample{  
public static void main(String args[]){  
int a=10;  
int b=20;  
a+=4;//a=a+4 (a=10+4)  
b-=4;//b=b-4 (b=20-4)  
System.out.println(a);  
System.out.println(b);  
}}  

You might also like