• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » DataType » Java Operators – Explain Operator in Java
Next →
← Prev

Java Operators – Explain Operator in Java

By Dinesh Thakur

Operator is a symbol that represents some operation that can be performed on data. The operators are applied to operands (onto which processing is desired). These operands can be literals, variables etc. The operators supported by Java are as follows:

Java Operators

We’ll be covering the following topics in this tutorial:

  • Operator Precedence
  • Arithmetic Operators
  • Unary Operators
  • Assignment Operators
  • Short Circuit Logical Operators
  • Relational Operators

Operator Precedence

Operator Precedence in java

Every Java operator has an associated precedence. Following is a list of all the Java operators from highest to lowest precedence. In this list of operators, all the operators in a particular block have equal precedence. The precedence level of each row decreases from top to bottom. This means that the ++ operator has a higher precedence than the * operator, but the same precedence as the — operator.

The following is a mnemonic to help you remember the precedence order, from highest to lowest: PUMAS.

P  Parentheses ( ), postfix a++ a–

U  Unary operators (except postfix) + (positive) – (negative) ++a –a

M  Multiplication, division and Modulus * / %

A  Addition and subtraction + –

S  Special assignments = += -= *= /= %=

We apply this precedence order to evaluate the values of expressions. The following example should help clarify this:

d = a * b + ++ c;

Adding parentheses to this statement will make it clear as to how it should be evaluated. The steps are as follows:

1. The ++operator is the most important of the operators in this expression. Add parentheses around b++:

d = a * (b++) + c;

2. Among the remaining operators *, +, and =, * has greater importance. Add parentheses around * and the two variables that use this operator:

d = (a * (b++)) + c;

3. The operator + has higher importance than =, so add parentheses as follows:

d = ((a * (b++)) + c);

4. The expression is now fully parenthesized. Next, the values in the inner parentheses must be evaluated before those in the outer parentheses. Assume that a = 5, b = 3, and c = 1; then the value of d is (5 * 3) + 1 = 16

First, you must parenthesize an expression fully using the operator precedence order. Then, evaluate the expression starting from the innermost parentheses and moving toward the outermost parentheses. If there are multiple operators with the same priority or importance in the-expression, evaluate these from left to right.

Arithmetic Operators

Arithmetic operators are used for simple arithmetic operations. The various operators are represented by the symbols:

Description               Operator

Addition           +          1 + 1 = 2

Subtraction        –          2 – 1 = 1

Multiplication     *          2 * 3 = 6 (same as 2 X 3 = 6) 

Division           /          6 / 2 = 3

Modulus            %          7 % 2 = 1

The modulus operator gives the remainder when a division operation is performed.

When the operands of an arithmetic operator are of different types, they must be changed to the same type before the operation is performed. Type promotion automatically converts the data type of the narrower operand to the type of the wider operand. For example, if d and i are operands of type double and int, respectively, i is promoted to double in the following expression:

double v = d * i;

This example shows how the minus arithmetic operator is used. The program prompts the user to enter two numbers on the console and reads them in using the scanner. The second number is subtracted from the first, and the result is displayed on the console.

import java.util .Scanner;
publicclass Calculator {
     publicstaticvoid main(String[] args){
Scanner scanner =newScanner(System.in);
            System.out.print(“Enter a number: “);
            double numberl = scanner.nextDouble();
            System.out.print(“Enter another number:”);
            double number2 = scanner.nextDouble();
            System.out.println(“The result is:”+(numberl – number2));
}
}
The program output is:
Enter a number:10
Enter another number:30.5
The result is:-20.5

The output shows that the user entered the numbers 10 and 30.5, and the program displayed the result: -20.5.

The -+- operator can be used in a different context in print statements-to concatenate different types. For example, the following statement concatenates the string “The result is:” with the result of the subtraction number1 – number2:

“The result is:” + (number1 – number2)

You can also pass primitive types as arguments to the print and print.ln methods. Therefore, the following arguments are all valid:

int x = 10, Y = 20; String s = “30”;

System.out.println(x); // prints out 10

System.out.println(x + y + x); // prints out 40

System.out.println(s + x + x); // prints out 301010

In the last statement, the + operator performs a concatenation because the first argument s is of type String.

Modify the program so that it uses a different arithmetic operator and rerun it. Use different input values and check the program output.

Unary Operators

Whereas arithmetic operators take two operands, unary operators take a single operand. The unary operators are:

++ increment

— decrement

+ plus

– minus

The increment operator is used to increase the value of a variable by 1, and the decrement operator to decrease a variable’s value by 1.

This statement increases the value of alpha by 1:

++alpha; // increment operator used in prefix position

This is the same as writing:

alpha = alpha + 1;

When the operator appears before the variable name, it is known as prefix notation. The statement can also be written as follows:

alpha++; // increment operator used in postfix position

Conversely, when the operator is applied after the variable name, the notation is called postfix notation. In this case as well, the value of alpha is increased by 1.

The following statements use the decrement operator. Each statement decreases the value of variable beta by 1:

beta = beta – 1; // reduce the value of variable beta by 1

–beta; // decrement operator used in prefix position

beta–; // decrement operator used in postfix position

All three statements are equivalent.

When the decrement and increment operators are used in assignment statements, the position of the operator affects the value of the expression. For example, suppose that the variable 1ength has the increment operator placed in the postfix position:

float breadth = length++ – 5.0f;

This means that 1ength is incremented after the expression is evaluated using its original value. Hence, the preceding statement is equivalent to the following two statements:

float breadth = length – 5.0f;

length = length + 1;

When the operator appears in the prefix, position as in the following statement, it means that the variable 1ength is incremented first, and the value of the expression is evaluated using this incremented value. The statement shows the operator in prefix notation:

float breadth = ++length – 5.0f;

The preceding statement is equivalent to the following two statements:

length = length + 1;

float breadth = length – 5.0f;

The unary minus operator is prefixed before an expression to negate it:

int x = 10;

int y = -(x – 5);

The expression in parentheses is a single operand (x – 5) with the value 5. The value of y is – 5. This is the same as writing:

int x = 10;

int y = -x + 5;

The unary plus operator has no effect on an expression:

int x = 10;

int y = +(-(x – 5));

The value of y is still – 5.

Assignment Operators

Earlier you used the assignment operator =. This operator can be combined with the arithmetic operators +, -, *, /, and % to give a new set of assignment operators: +=, -=, *=, 1=, and %=. These new operators are used to represent assignment statements in an abbreviated form. For example, the following two statements are equivalent, and you can use either one of them:

length = length + 5;

length += 5;

The following two statements are also equivalent:

length = length * 10;

length *= 10; // multiply length by 10

The advantage of the second statement is that it is shorter because 1ength appears only once instead of twice. The other operators are used similarly:

radius /= 5.5; // radius = radius / 5.5;

x %= 10; // x = x % 10;

Note that if the variable on the right is different from the variable on the left, the abbreviated form cannot be used. The following statement uses two different variables:

size = breadth * 10;

This statement cannot be written as:

size *= 10; // incorrect, as it does not contain variable breadth

Short Circuit Logical Operators

When two logical expressions are concatenated with operators: AND and OR, the OR operator results in true when A is true, no matter what B is. Similarly, the AND operator results in false when A is false, no matter what B is. If we use the operators II and &&, instead of the operators I and &, Java will not bother to evaluate the right hand operand when the outcome of the expression can be determined by the left operand alone. This is very useful when the right hand operand depends on the left one being true or false in order to function properly.

if (physics >=60 && chemistry >=60)

Since the short circuit form of AND (&&) is used, control will just check first condition, Le. if physics marks >=60, it won’t check the marks of chemistry at all. If instead of &&, if we use single & version of AND, both sides would have to be evaluated Le. it will check that physics >=60 and also chemistry >=60.

Relational Operators

Relational operators are used to find out the relationship between two operands. Table shows you the different relational operators used in java programming.

                      relational operators

Example of Program using relational operators

class ro {
    public static void main(String args[ ]) {
        int a=10, b=5;
        System.out.println(“The a<b Value is:”+(a<b ));
        System.out.println(“The a>b Value is:”+(a>b ));
System.out.println(“The a<=b Value is:”+(a<=b));
        System.out.println(“The a>=b Value is:”+(a>=b));
        System.out.println(“The a==b Value is:”+(a==b));
        System.out.println(“The a!=b Value is:”+(a!=b ));
    }
}

You’ll also like:

  1. What is Operators? Explain Scope Resolution Operator and Operators Precedence.
  2. Explain purpose of relational operators and logical operator
  3. Explain Order of Precedence of Operators for Calculations
  4. Write A C++ Program To Explain The Use of Increment And Decrement Operator (Prefix).
  5. Write A C++ Program To Explain The Use of Increment And Decrement Operator (Postfix).
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW