Operators in Java

Java has a variety of operators that can be employed depending on the situation. Variables and values can be operated upon using operators. They are categorized according to the features they offer. Among the varieties are arithmetic operators, relational operators, bitwise operators, logical operators, ternary operators, shift operators, etc. in this article, we will discuss these operators in depth along with the advantages of working with operators in Java.

What are Java Operators?

Variables and values can be operated upon using operators. Symbols known as operators carry out operations on values and variables. For instance, operator + is used for addition, whereas operator * is used for multiplication. The main operators in Java can be classified into 6 types which are:

  • Arithmetic operators
  • Assignment operators
  • Relational operators
  • Logical operators
  • Unary operators
  • Bitwise operators

                          Wish to make a career in the world of Java? Start with HKR'S Java Training

Let us talk about the operators in Java in detail:

1.Arithmetic operators:

To perform arithmetic operations on variables and data, arithmetic operators are used. For example: x+y;

Here, the 2 variables x and y are added using the + operator. Similar to this, Java has a number of different arithmetic operators. Let us discuss the operators in the arithmetic category and their operations:

  • Addition - ‘+’
  • Subtraction - ‘-’
  • Division - ‘/’
  • Modulo - ‘%

Let us see a Java example below to understand arithmetic operators better:

class Main {
public static void main(String[] args) {

int x = 10, y = 8;

System.out.println("x + y is= " + (x + y));

System.out.println("x - y is = " + (x - y));

System.out.println("x * y is = " + (x * y));
System.out.println("x / y is = " + (x / y));

System.out.println("x % y is = " + (x % y));
}
}

The output of the Java program is:

x + y is= 18
x - y is = 2x * y is = 80
x / y is = 1
x % y is = 2

2.Assignment operators:

Java uses assignment operators for assigning values to variables. For example:
Int age;
age=3;
The assignment operator, in this case, is =. The variable on the left is given the value on its right. Thus, the variable age is given the value of 5. Let us discuss the operators in the assignment category and their operations:
‘=’: a=b
‘+=’: a= a+b
‘-’: a=a-b
‘*=’: a= a*b
‘/=’: a= a/b
‘%=’: a=a%b

Let us see a Java example below to understand assignment operators better:

class Main {
public static void main(String[] args) {

int a = 6;
int x;

x = a;
System.out.println("The variable using =: " + x);

x += a;
System.out.println("The variable using +=: " + x);

x *= a;
System.out.println("The variable using *=: " + x);
}
}

The output of the Java program is:

The variable using =: 6
The variable using +=: 12
The variable using *=: 72

3.Relational operators:

To look into the relationship among two operands, utilize relational operators. Decision-making and looping both require relational operators. For example:
x < b
Here, the relational operator is < operator. It determines whether or not an is less than b.
Either true or false is returned as an output.
Let us discuss the operators in the relational category and their operations:
‘==’: is equal to
‘=!’: Not equal to
‘>’: greater than
‘<’: less than
‘>=’: greater than or equal to
‘<=’: less than or equal to

Let us see a Java example below to understand relational operators better:

class Main {
public static void main(String[] args) {

int x = 6, y = 13;

System.out.println("x is " + x + " and y is " + y);

System.out.println(x == y);

System.out.println(x != y);

System.out.println(x > y);

System.out.println(x < y);

System.out.println(x >= y);

System.out.println(x <= y);
}
}

The output of the Java program is:

x is 6 and y is 13
False
True
False
True
False
True

Java Certification Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

4.Logical operators:

These operators serve a similar purpose to the OR gate and AND gate in digital electronics by performing "logical AND" and "logical OR" operations. One thing to bear in mind is that if the first condition is false, the second one is not assessed, which has the effect of short-circuiting. frequently used to evaluate several possibilities before making a choice. The "Logical NOT" function in Java also returns true whenever the condition is fulfilled and vice versa. They are consulted when making decisions. For example:
x < b
Here, the relational operator is < operator. It determines whether or not an is less than b.
Either true or false is returned as an output.
Let us discuss the operators in the logical category and their operations:
‘&&’: Logical and
‘||’: Logical or
‘!’: Logical Not

Let us see a Java example below to understand logical operators better:

class Main {
public static void main(String[] args) {

System.out.println((4 > 3) && (6 > 5));
System.out.println((4 > 3) && (6 < 5));

System.out.println((4 < 3) || (6 > 5));
System.out.println((4 > 3) || (6 < 5));
System.out.println((4 < 3) || (6 < 5));

System.out.println(!(4 == 3));
System.out.println(!(4 > 3));
}
}

The output of the Java program is:

True
False
True
True
False
True
False

5.Unary operators:

Only one operand is required for unary operators. For instance, the unary operator ++ raises a variable's value by one. Therefore, ++5 will result in 6.
Let us discuss the operators in the unary category and their operations:
‘+’: Unary plus
‘-’: Unary minus
‘++’: Increment operator
‘--’: Decrement operator
‘!’: Logical Compliment operator

Let us see a Java example below to understand unary operators better:

class Main {
public static void main(String[] args) {

int x = 12, y = 12;
int res1, res2;

System.out.println("Value of x: " + x);

res1 = ++x;
System.out.println("After increment: " + res1);

System.out.println("Value of y: " + y);

res2 = --y;
System.out.println("After decrement: " + res2);
}
}


The output of the Java program is:

Value of x: 12
After increment: 13
Value of y: 12
After decrement: 11

The ++ and — operators are used as prefixes in the aforementioned program (++a, —b). These operators (a++, b++) can be used as postfix as well.

6.Bitwise operators:

Java uses bitwise operators to execute operations on individual bits. The bitwise operator, in this case, is ~. Every bit's value is inverted (0 to 1 and 1 to 0).
Let us discuss the operators in the bitwise category and their operations:
‘~’: Bitwise compliment
‘<<’: Left shift
‘>>’: Right shift
‘>>>’: Unsigned Right shift
‘&’: Bitwise AND
‘^’: Bitwise exclusibe OR

Let us see a Java example below to understand bitwise operators better:

class Main {
public static void main(String[] args) {

int x = 12, y = 12;
int res1, res2;

System.out.println("Value of x: " + x);

res1 = ++x;
System.out.println("After increment: " + res1);

System.out.println("Value of y: " + y);

res2 = --y;
System.out.println("After decrement: " + res2);
}
}


The output of the Java program is:

Value of x: 12
After increment: 13
Value of y: 12
After decrement: 11

The ++ and — operators are used as prefixes in the aforementioned program (++a, —b). These operators (a++, b++) can be used as postfix as well.

 There are a lot of other operators in Java too. Let us have a look at them too:

Java instance of operator:

The instanceof operator determines whether an object belongs to a specific class. Let us understand this with the help of a Java example:

class Main {
public static void main(String[] args) {

String str = "HKR Trainings";
boolean res;

res = str instanceof String;
System.out.println("Is str an object of String or not? " + res);
}
}

The output of the Java code is:

Is str an object of String or not?
true

 In this case, str is a String class instance. Consequently, the instanceof operator gives a true result.

Java Ternary Operator:

The if-then-else sentence is denoted by the ternary operator, often known as a conditional operator.
It works as:
Expression1 is assigned to the variable if the expression returns true.
Expression2 is assigned to the variable if the expression returns false.
Let us understand this with the help of a Java example:

class Java {
public static void main(String[] args) {

int Days_in_feb = 29;
String res;

res = (Days_in_feb == 28) ? "Leap year" : "Not a Leap year";
System.out.println(res);
}
}

The output of the Java code is:

Leap year

                                                            Top 30 frequently asked JAVA Interview Questions !

Subscribe to our youtube channel to get new updates..!

Operators' Precedence in Java 

When dealing with hybrid situations including more than one kind of operator, precedence and associative principles are applied. As there may be various evaluations for the exact equation in such instances, these guidelines specify which component of the equation should be taken into account first. The precedence of operations is shown in the table below as magnitude, with the highest precedence at the top and the lowest precedence at the bottom.

Let us understand t precedence of operators with the help of the image below:

Operators' Precedence in Java

Precedence and Associativity

When it concerns hybrid equations, which are equations with numerous operators, there is frequently confusion. Which component to address first is the issue. In these circumstances, there is a golden guideline to remember. Solve the operator with the highest precedence first if the operators are different. Resolve the problem as per the associativity, that really is, whether from left to right or right to left or , if they have the same precedence.

Let us understand this in a better way with the help of the example below:

public class operators {
public static void main(String[] args)
{
int x = 20, y = 10, z = 0, e = 20, f = 40, g = 30;

System.out.println("x+y/e = " + (x + y / e));

System.out.println("x+y*f-f/g = "
+ (x + y * e - f / g));
}
}

The output of the Java code is:

x+y/e = 20
x+y*f-f/g = 219

Java Certification Training

Weekday / Weekend Batches

 Conclusion

In this article, we have understood the use of Java operators and various types of operators in Java along with some Java examples associated with them. Java has a variety of operators that can be employed depending on the situation. Variables and values can be operated upon using operators. Various types of java operators are arithmetic operators, relational operators, bitwise operators, logical operators, ternary operators, shift operators, etc. We have also talked about the precedence and associativity of the operators.

Related articles:

Find our upcoming Java Certification Training Online Classes

  • Batch starts on 2nd Oct 2022, Weekend batch

  • Batch starts on 6th Oct 2022, Weekday batch

  • Batch starts on 10th Oct 2022, Weekday batch

Global Promotional Image
 

Categories

Request for more information

Amani
Amani
Research Analyst
As a content writer at HKR trainings, I deliver content on various technologies. I hold my graduation degree in Information technology. I am passionate about helping people understand technology-related content through my easily digestible content. My writings include Data Science, Machine Learning, Artificial Intelligence, Python, Salesforce, Servicenow and etc.

Arithmetic operators.

Assignment operators.

Comparison operators.

Logical operators.

Bitwise operators.