Handling Operators in Java

Last updated on Dec 23 2022
Prabhas Ramanathan

Operator in Java is a symbol which 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.

Table of Contents

Java Operator Precedence

Operator Type Category Precedence
Unary postfix expr++ expr
prefix ++exprexpr +exprexpr~ !
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.:
• incrementing/decrementing a value by one
• negating an expression
• inverting the value of a boolean

Java Unary Operator Example: ++ and —

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

Output:
10
12
12
10

Java Unary Operator Example 2: ++ and —

1. class OperatorExample{ 
2. public static void main(String args[]){ 
3. int a=10; 

4. int b=10; 
5. System.out.println(a++ + ++a);//10+12=22 
6. System.out.println(b++ + b++);//10+11=21 
7. 
8. }}

Output:
22
21

Java Unary Operator Example: ~ and !

1. class OperatorExample{ 
2. public static void main(String args[]){ 
3. int a=10; 
4. int b=-10; 
5. boolean c=true; 
6. boolean d=false; 
7. System.out.println(~a);//-11 (minus of total positive value which starts from 0) 
8. System.out.println(~b);//9 (positive of total minus, positive starts from 0) 
9. System.out.println(!c);//false (opposite of boolean value) 
10. System.out.println(!d);//true 
11. }}

Output:
-11
9
false
true

Java Arithmetic Operators

Java arithmatic operators are used to perform addition, subtraction, multiplication, and division. They act as basic mathematical operations.
Java Arithmetic Operator Example

1. class OperatorExample{ 
2. public static void main(String args[]){ 
3. int a=10; 
4. int b=5; 
5. System.out.println(a+b);//15 
6. System.out.println(a-b);//5 
7. System.out.println(a*b);//50 
8. System.out.println(a/b);//2 
9. System.out.println(a%b);//0 
10. }}

Output:
15
5
50
2
0

Java Arithmetic Operator Example: Expression

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

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

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

Output:
40
80
80
240

Java Right Shift Operator

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

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

Output:
2
5
2

 

Java Shift Operator Example: >> vs >>>

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

Output:
5
5
-5
1073741819

Java AND Operator Example: Logical && and Bitwise &

The logical && operator doesn’t check second condition if first condition is false. It checks second condition only if first one is true.
The bitwise & operator always checks both conditions whether 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. }}

Output:
false
false

Java AND Operator Example: Logical && vs Bitwise &

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

Output:
false
10
false
11

Java OR Operator Example: Logical || and Bitwise |

The logical || operator doesn’t check second condition if first condition is true. It checks second condition only if first one is false.
The bitwise | operator always checks both conditions whether 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);//true || true = true 
7. System.out.println(a>b|a<c);//true | true = true 
8. //|| vs | 
9. System.out.println(a>b||a++<c);//true || true = true 
10. System.out.println(a);//10 because second condition is not checked 
11. System.out.println(a>b|a++<c);//true | true = true 
12. System.out.println(a);//11 because second condition is checked 
13. }}

Output:
true
true
true
10
true
11

Java Ternary Operator

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

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

Output:
2

Another Example:

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

Output:
5

Java Assignment Operator

Java assignment operator is one of the most common operator. It is used to assign the value on its right to the operand on its left.
Java Assignment Operator Example

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

Output:
14
16

Java Assignment Operator Example

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

Output:
13
9
18
9

Java Assignment Operator Example: Adding short

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

Output:
Compile time error

After type cast:

1. class OperatorExample{ 
2. public static void main(String args[]){ 
3. short a=10; 
4. short b=10; 
5. a=(short)(a+b);//20 which is int now converted to short 
6. System.out.println(a); 
7. }}

Output:
20

So, this brings us to the end of blog. This Tecklearn ‘Handling Operators in Java’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn Java and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Java and JEE Training

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

• Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
• Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
• Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

• Java Fundamentals
• Introduction to Java Basics
• Features of Java
• Various components of Java language
• Benefits of Java over other programming languages
• Key Benefits of Java

Installation and IDE’s for Java Programming Language

• Installation of Java
• Setting up of Eclipse IDE
• Components of Java Program
• Editors and IDEs used for Java Programming
• Writing a Simple Java Program

Data Handling and Functions

• Data types, Operations, Compilation process, Class files, Loops, Conditions
• Using Loop Constructs
• Arrays- Single Dimensional and Multi-Dimensional
• Functions
• Functions with Arguments

OOPS in Java: Concept of Object Orientation

• Object Oriented Programming in Java
• Implement classes and objects in Java
• Create Class Constructors
• Overload Constructors
• Inheritance
• Inherit Classes and create sub-classes
• Implement abstract classes and methods
• Use static keyword
• Implement Interfaces and use it

Polymorphism, Packages and String Handling

• Concept of Static and Run time Polymorphism
• Function Overloading
• String Handling –String Class
• Java Packages

Exception Handling and Multi-Threading

• Exception handling
• Various Types of Exception Handling
• Introduction to multi-threading in Java
• Extending the thread class
• Synchronizing the thread

File Handling in Java

• Input Output Streams
• Java.io Package
• File Handling in Java

Java Collections

• Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
• Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
• Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

• Introduction to SQL: Connect, Insert, Update, Delete, Select
• Introduction to JDBC and Architecture of JDBC
• Insert/Update/Delete/Select Operations using JDBC
• Batch Processing Transaction
• Management: Commit and Rollback

Java Enterprise Edition – Servlets

• Introduction to J2EE
• Client Server architecture
• URL, Port Number, Request, Response
• Need for servlets
• Servlet fundamentals
• Setting up a web project in Eclipse
• Configuring and running the web app with servlets
• GET and POST request in web application with demo
• Servlet lifecycle
• Servlets Continued
• Session tracking and filter
• Forward and include Servlet request dispatchers

Java Server Pages (JSP)

• Fundamentals of Java Server Page
• Writing a code using JSP
• The architecture of JSP
• JSP Continued
• JSP elements: Scriptlets, expressions, declaration
• JSP standard actions
• JSP directives
• Introduction to JavaBeans
• ServletConfig and ServletContext
• Servlet Chaining
• Cookies Management
• Session Management

Hibernate

• Introduction to Hibernate
• Introduction to ORM
• ORM features
• Hibernate as an ORM framework
• Hibernate features
• Setting up a project with Hibernate framework
• Basic APIs needed to do CRUD operations with Hibernate
• Hibernate Architecture

POJO (Plain Old Java Object)

• POJO (Plain Old Java Object)
• Persistent Objects
• Lifecycle of Persistent Object

Spring

• Introduction to Spring
• Spring Fundamentals
• Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

0 responses on "Handling Operators in Java"

Leave a Message

Your email address will not be published. Required fields are marked *