Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Java Operators

  • Kishkindha Kand in Hindi | Ram meets Hanuman | किष्किंधा कांड
    Kishkindha Kand in Hindi | Ram meets Hanuman | किष्किंधा कांड Spiritual
  • Unicef day
    Unicef day is celebrated on December 11 | Speech on unicef day General Knowledge
  • Bhagat Singh Biography in Hindi (भगत सिंह का जीवन परिचय)
    Bhagat Singh Biography in Hindi (भगत सिंह का जीवन परिचय) Biography
  • World Red Cross Day and Red Crescent Day | विश्व रेडक्रॉस दिवस
    World Red Cross Day and Red Crescent Day | विश्व रेडक्रॉस दिवस General Knowledge
  • Balkand Ramayana story in Hindi | रामायण बाल कांड राम का जन्म
    Balkand Ramayana story in Hindi | रामायण बाल कांड राम का जन्म Spiritual
  • Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti
    Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti Spiritual
  • Christmas Day Celebration
    Christmas Day Celebration | Story about Christmas day Festival
  • Jagannath Rath Yatra History in Hindi | जगन्नाथ पुरी की कहानी
    Jagannath Rath Yatra History in Hindi | जगन्नाथ पुरी की कहानी Festival

Java Operators

Posted on August 15, 2021October 11, 2021 By GeekCer Education
Java Operators

Java Operators performs some operations on the variables and value like addition, subtraction, division etc.

In the programming language, operators are special symbols that perform specific operation on operands and then returns a result.

If an operator acts on a single variable then it is called unary operator, if operator acts on two variables it is called binary operator and operator acts on three variables then it is called ternary operator.

Table of Contents

  • Groups of Java Operators
    • 1. Arithmetic Operators
    • 2. Assignment Operators
    • 3. Bitwise and Bit Shift Operators
    • 4. Conditional Operators
    • 5. Equality and Relational Operators
    • 6. Type Comparison Operator
    • 7. Unary Operators
    • Example of Arithmetic Java Operators
  • Java Operators Precedence
    • Rules of operator precedence for Java Operators

Groups of Java Operators

There are following groups of java operators:

  1. Arithmetic Operators
  2. Assignment Operators
  3. Bitwise and Bit Shift Operators
  4. Conditional Operators
  5. Equality and Relational Operators
  6. Type Comparison Operator
  7. Unary Operators

1. Arithmetic Operators

We use the arithmetic operators to perform addition, subtraction, multiplication, division and modulus.

#NameOperatorExampleDetails
1Addition+a + bAdds two values
2Subtraction–a – bSubtracts two values
3Multiplication*a * bMultiplies one by another value
4Division/a / bDivides one by another
5Modulus%a % bReturns the division remainder

2. Assignment Operators

We assign value to variables using assignment operators. In other words, we assign the value to the right of the operand to the left of it.

#OperatorExample
1=z= x + y will assign value of x + y into z
2+=x += 10 is equivalent to x = x + 10
3-=x -= 10 is equivalent to x = x – 10
4*=x *= 10 is equivalent to x = x * 10
5/=x /= 10 is equivalent to x = x / 10
6%=x %= 10 is equivalent to x = x % 10
7&= x &= 10 is equivalent to x = x & 10
8|=x |= 10 is equivalent to x = x | 10
9^=x ^= 10 is equivalent to x = x ^ 10
10<<=x <<= 10 is equivalent to x = x << 10
11>>=x >>= 10 is equivalent to x = x >> 10

3. Bitwise and Bit Shift Operators

These operators act on bits 0 and 1 of the operands and act on byte, short, int and long.

#OperatorName
1~Unary bitwise complement
2<<Signed left shift
3>> Signed right shift
4>>>Unsigned right shift
5& Bitwise AND
6^Bitwise exclusive OR
7|Bitwise inclusive OR

4. Conditional Operators

#OperatorName
1&& Conditional AND
2||Conditional OR
3?:Ternary

5. Equality and Relational Operators

The equality and relational operations check whether one operand is greater than, less than, equal to, or not equal to other operand.

#OperatorName
1==equal to
2!=not equal to
3>greater than
4>=greater than or equal to
5 < less than
6 <= less than or equal to

6. Type Comparison Operator

This operator checks whether the given type is compatible with another type. In other words, whether specified the instance is of a specified type(interface, class, subclass).

#OperatorName
1instanceofCompares an object to a specified type

7. Unary Operators

#OperatorName
1+Indicates positive value (Unary plus operator)
2–Negates an expression (Unary minus operator)
3++Increments a value by 1 (Increment operator)
4—Decrements a value by 1 (Decrement operator)
5!Inverts the value of a boolean (Logical complement operator)

Example of Arithmetic Java Operators

In addition to the explanation here is an example of arithmetic operators that shows how to perform operations using these types of operators.


public class ArithmeticOperations {
  public static void main(String[] args) {
    int result = 4 + 2;
    // result is now 6
    System.out.println("4 + 2 = " + result);
    int originalResult = result;

    result = result - 1;
    // result is now 5
    System.out.println(originalResult + " - 1 = " + result);
    originalResult = result;

    result = result * 2;
    // result is now 10
    System.out.println(originalResult + " * 2 = " + result);
    originalResult = result;

    result = result / 2;
    // result is now 5
    System.out.println(originalResult + " / 2 = " + result);
    originalResult = result;

    result = result + 8;
    // result is now 13
    System.out.println(originalResult + " + 8 = " + result);
    originalResult = result;

    result = result % 7;
    // result is now 6
    System.out.println(originalResult + " % 7 = " + result);
  }
}

Java Operators Precedence

When several operators are used in a statement then it is important to know which operator will execute first and which will come next. Operators with higher priority are evaluated before operators with lower priority. But sometimes operators with similar expressions may appear in the same expression.

Rules of operator precedence for Java Operators

To determine this, here are some rules of operator preference:

  • Firstly the contents inside () and [] braces will be executed.
  • ++ and — will come afterwards
  • And then *, / and % will execute.
  • Next, + and – will come
  • Then Relational operators
  • Then Boolean an bitwise operators
  • Logical operators will come afterwards
  • Then ternary operator.
  • At last, assignment operator are executed.
OperatorsPrecedence
postfixexpr++ expr--
unary++expr --expr +expr -expr ~ !
multiplicative* / %
additive+ -
shift<< >> >>>
relational< > <= >= instanceof
equality== !=
bitwise AND&
bitwise exclusive OR^
bitwise inclusive OR|
logical AND&&
logical OR||
ternary? :
assignment= += -= *= /= %= &= ^= |= <<= >>= >>>=

In Java Assignment operators are evaluated from right to left and the binary operators are evaluated from left to right.

In conclusion, you learned that operators make programming easier by helping the programmer to perform any operation by simply mentioning the symbol.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Java

Post navigation

Previous Post: Java Keywords
Next Post: Java If-else Statement

More Related Articles

Java If-else Statement Java If-else Statement Java
Java 8 interview questions and answers, Java Stream, Optional Java 8 interview questions and answers, Java Stream, Optional Important
Java 8 Stream map example | Java 8 map(function) parameter Java 8 Stream map example | Java 8 map(function) parameter Java
Stack Vs Heap in Java Stack Vs Heap in Java | Memory Allocation Section in Java Differences
Java Keywords Java Keywords Java
Difference between Comparable and Comparator Difference between Comparable and Comparator Differences
  • Java Home
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Keywords
  • Java Operators
  • Java If-else Statement
  • Java Switch
  • Java Loop
  • Java Arrays
  • Method Overloading in Java
  • Java OOP
  • Java Collections
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Multithreading in java
  • Thread Synchronization
  • Exception Handling
  • Java JDBC Driver
  • Java Database Connectivity steps
  • Lambda Expressions
  • Concurrent Collections
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved