0% found this document useful (0 votes)
126 views

Operators in Java - Javatpoint

The document discusses various types of operators in Java including unary, arithmetic, shift, relational, bitwise, logical, and assignment operators. It provides examples of how to use unary operators like ++ and --, arithmetic operators like +, -, *, /, %, shift operators like << and >>, logical operators like && and ||, and bitwise operators like & and |. It explains the difference between logical operators and bitwise operators, and how operator precedence determines the order of operations.

Uploaded by

Krishnaprasad k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

Operators in Java - Javatpoint

The document discusses various types of operators in Java including unary, arithmetic, shift, relational, bitwise, logical, and assignment operators. It provides examples of how to use unary operators like ++ and --, arithmetic operators like +, -, *, /, %, shift operators like << and >>, logical operators like && and ||, and bitwise operators like & and |. It explains the difference between logical operators and bitwise operators, and how operator precedence determines the order of operations.

Uploaded by

Krishnaprasad k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

5/18/22, 2:36 PM Operators in Java - Javatpoint

Home Java Programs OOPs String Exception Multithreading


https://www.javatpoint.com/operators-in-java 1/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

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:

Unary Operator,

Arithmetic Operator,

Shift Operator,

Relational Operator,

Bitwise Operator,

Logical Operator,

Ternary Operator and

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 ^

https://www.javatpoint.com/operators-in-java 2/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

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.:

incrementing/decrementing a value by one

negating an expression

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)  

https://www.javatpoint.com/operators-in-java 3/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

System.out.println(++x);//12  
System.out.println(x--);//12 (11)  
System.out.println(--x);//10  
}}  

Output:

10

12

12

10

Java Unary Operator Example 2: ++ and --

public class OperatorExample{  
public static void main(String args[]){  
int a=10;  
int b=10;  
System.out.println(a++ + ++a);//10+12=22  
System.out.println(b++ + b++);//10+11=21  
  
}}  

Output:

22

21

Java Unary Operator Example: ~ and !

public class OperatorExample{  
public static void main(String args[]){  
int a=10;  
int b=-10;  

https://www.javatpoint.com/operators-in-java 4/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

boolean c=true;  
boolean d=false;  
System.out.println(~a);//-11 (minus of total positive value which starts from 0)  
System.out.println(~b);//9 (positive of total minus, positive starts from 0)  
System.out.println(!c);//false (opposite of boolean value)  
System.out.println(!d);//true  
}}  

Output:

-11

false

true

Java Arithmetic Operators

Java arithmetic operators are used to perform addition, subtraction, multiplication, and division.
They act as basic mathematical operations.

Java Arithmetic Operator Example

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  
}}  

Output:

https://www.javatpoint.com/operators-in-java 5/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

15

50

Java Arithmetic Operator Example: Expression

public class OperatorExample{  
public static void main(String args[]){  
System.out.println(10*10/5+3-1*4/2);  
}}  

Output:

21

Java Left Shift Operator

The Java left shift operator << is used to shift all of the bits in a value to the left side of a specified
number of times.

Java Left Shift Operator Example

public class OperatorExample{  
public static void main(String args[]){  
System.out.println(10<<2);//10*2^2=10*4=40  
System.out.println(10<<3);//10*2^3=10*8=80  
System.out.println(20<<2);//20*2^2=20*4=80  
System.out.println(15<<4);//15*2^4=15*16=240  
}}  

Output:

https://www.javatpoint.com/operators-in-java 6/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

40

80

80

240

Java Right Shift Operator

The Java right shift operator >> is used to move the value of the left operand to right by the
number of bits specified by the right operand.

Java Right Shift Operator Example

public OperatorExample{  
public static void main(String args[]){  
System.out.println(10>>2);//10/2^2=10/4=2  
System.out.println(20>>2);//20/2^2=20/4=5  
System.out.println(20>>3);//20/2^3=20/8=2  
}}  

Output:

Java Shift Operator Example: >> vs >>>


https://www.javatpoint.com/operators-in-java 7/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

public class OperatorExample{  
public static void main(String args[]){  
    //For positive number, >> and >>> works same  
    System.out.println(20>>2);  
    System.out.println(20>>>2);  
    //For negative number, >>> changes parity bit (MSB) to 0  
    System.out.println(-20>>2);  
    System.out.println(-20>>>2);  
}}  

Output:

-5

1073741819

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.

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

public class OperatorExample{  
https://www.javatpoint.com/operators-in-java 8/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

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  
}}  

Output:

false

false

Java AND Operator Example: Logical && vs Bitwise &

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);//10 because second condition is not checked  
System.out.println(a<b&a++<c);//false && true = false  
System.out.println(a);//11 because second condition is checked  
}}  

Output:

false

10

false

11

Java OR Operator Example: Logical || and Bitwise |


https://www.javatpoint.com/operators-in-java 9/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

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.

The bitwise | operator always checks both conditions whether first condition is true or 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  
}}  

Output:

true

true

true

10

true

11

Java Ternary Operator

Java Ternary operator is used as one line replacement for if-then-else statement and used a lot in
Java programming. It is the only conditional operator which takes three operands.

https://www.javatpoint.com/operators-in-java 10/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

Java Ternary Operator Example

public class OperatorExample{  
public static void main(String args[]){  
int a=2;  
int b=5;  
int min=(a<b)?a:b;  
System.out.println(min);  
}}  

Output:

Another Example:

public class OperatorExample{  
public static void main(String args[]){  
int a=10;  
int b=5;  
int min=(a<b)?a:b;  
System.out.println(min);  
}}  

Output:

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.

Java Assignment Operator Example

https://www.javatpoint.com/operators-in-java 11/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

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);  
}}  

Output:

14

16

Java Assignment Operator Example

public class OperatorExample{  
public static void main(String[] args){  
int a=10;  
a+=3;//10+3  
System.out.println(a);  
a-=4;//13-4  
System.out.println(a);  
a*=2;//9*2  
System.out.println(a);  
a/=2;//18/2  
System.out.println(a);  
}}  

Output:

https://www.javatpoint.com/operators-in-java 12/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

13

18

Java Assignment Operator Example: Adding short

public class OperatorExample{  
public static void main(String args[]){  
short a=10;  
short b=10;  
//a+=b;//a=a+b internally so fine  
a=a+b;//Compile time error because 10+10=20 now int  
System.out.println(a);  
}}  

Output:

Compile time error

After type cast:

public class OperatorExample{  
public static void main(String args[]){  
short a=10;  
short b=10;  
a=(short)(a+b);//20 which is int now converted to short  
https://www.javatpoint.com/operators-in-java 13/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

System.out.println(a);  
}}  

Output:

20

You may also like

Operator Shifting in Java

← Prev
Next →

Youtube
For Videos Join Our Youtube Channel: Join Now

Feedback

Send your Feedback to [email protected]

https://www.javatpoint.com/operators-in-java 14/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

Help Others, Please Share

Learn Latest Tutorials

Splunk tutorial SPSS tutorial Swagger T-SQL tutorial


tutorial
Splunk SPSS Transact-SQL
Swagger

Tumblr tutorial React tutorial Regex tutorial Reinforcement


learning tutorial
Tumblr ReactJS Regex
Reinforcement
Learning

R Programming RxJS tutorial React Native Python Design


tutorial tutorial Patterns
RxJS
R Programming React Native Python Design
Patterns

Python Pillow Python Turtle Keras tutorial


tutorial tutorial
Keras
Python Pillow Python Turtle

https://www.javatpoint.com/operators-in-java 15/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

Preparation

Aptitude Logical Verbal Ability Interview


Reasoning Questions
Aptitude Verbal Ability
Reasoning Interview Questions

Company
Interview
Questions
Company Questions

Trending Technologies

Artificial AWS Tutorial Selenium Cloud


Intelligence tutorial Computing
AWS
Tutorial tutorial
Selenium
Artificial Cloud Computing
Intelligence

Hadoop tutorial ReactJS Data Science Angular 7


Tutorial Tutorial Tutorial
Hadoop
ReactJS Data Science Angular 7

Blockchain Git Tutorial Machine DevOps


Tutorial Learning Tutorial Tutorial
Git
Blockchain Machine Learning DevOps

https://www.javatpoint.com/operators-in-java 16/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

B.Tech / MCA

DBMS tutorial Data Structures DAA tutorial Operating


tutorial System tutorial
DBMS DAA
Data Structures Operating System

Computer Compiler Computer Discrete


Network tutorial Design tutorial Organization and Mathematics
Architecture Tutorial
Computer Network Compiler Design
Computer Discrete
Organization Mathematics

Ethical Hacking Computer Software html tutorial


Tutorial Graphics Tutorial Engineering
Web Technology
Tutorial
Ethical Hacking Computer Graphics
Software
Engineering

Cyber Security Automata C Language C++ tutorial


tutorial Tutorial tutorial
C++
Cyber Security Automata C Programming

Java tutorial .Net Python tutorial List of


Framework Programs
Java Python
tutorial
Programs
.Net

Control Data Mining


Systems tutorial Tutorial

https://www.javatpoint.com/operators-in-java 17/18
5/18/22, 2:36 PM Operators in Java - Javatpoint

Control System Data Mining


Data
Warehouse
Tutorial
Data Warehouse

https://www.javatpoint.com/operators-in-java 18/18

You might also like