For working professionals
For fresh graduates
More
6. JDK in Java
7. C++ Vs Java
16. Java If-else
18. Loops in Java
20. For Loop in Java
46. Packages in Java
53. Java Collection
56. Generics In Java
57. Java Interfaces
60. Streams in Java
63. Thread in Java
67. Deadlock in Java
74. Applet in Java
75. Java Swing
76. Java Frameworks
78. JUnit Testing
81. Jar file in Java
82. Java Clean Code
86. Java 8 features
87. String in Java
93. HashMap in Java
98. Enum in Java
101. Hashcode in Java
105. Linked List in Java
109. Array Length in Java
111. Split in java
112. Map In Java
115. HashSet in Java
118. DateFormat in Java
121. Java List Size
122. Java APIs
128. Identifiers in Java
130. Set in Java
132. Try Catch in Java
133. Bubble Sort in Java
135. Queue in Java
142. Jagged Array in Java
144. Java String Format
145. Replace in Java
146. charAt() in Java
147. CompareTo in Java
151. parseInt in Java
153. Abstraction in Java
154. String Input in Java
156. instanceof in Java
157. Math Floor in Java
158. Selection Sort Java
159. int to char in Java
164. Deque in Java
172. Trim in Java
173. RxJava
174. Recursion in Java
175. HashSet Java
177. Square Root in Java
190. Javafx
In Java, operators are the essential tools that allow you to manipulate data, control program flow, and perform calculations. Mastering these operators in Java helps you write cleaner and more concise code and ensures that your program runs efficiently and correctly.
Java programming language provides various operators, each designed to handle specific tasks, from simple arithmetic operations to complex bitwise manipulations. For instance, arithmetic operators let you perform basic calculations, while logical operators control the flow of conditional logic.
Whether you're performing simple calculations, comparing values, or working with low-level bitwise operations, understanding Java operators will give you the flexibility and control needed to create robust applications.
Let’s delve into the details!
Want to master Java and beyond? Explore this comprehensive Software Engineering course to level up your skills.
An operator is a symbol or keyword representing an action or a computation performed on one or more operands (variables or values). In Java, operators allow you to manipulate data and control the flow of your program. They are classified into several categories based on their functionality, such as arithmetic, relational, logical, and bitwise operations.
Upgrade your skills with these high-impact tech programs:
• Full Stack Development Bootcamp – upGrad
• AI Full Stack Development – IIIT Bangalore
• Master of Design in User Experience by Jindal Global School
Arithmetic operators perform basic mathematical operations like addition, subtraction, multiplication, division, and modulo (remainder).
Operator | Description | Example |
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulo (Remainder) | a % b |
Example:
int a = 10;
int b = 3;
System.out.println("Addition: " + (a + b)); // Output: 13
System.out.println("Division: " + (a / b)); // Output: 3
System.out.println("Modulo: " + (a % b)); // Output: 1
Important Pitfall: In Java, dividing an integer by zero results in a runtime exception (ArithmeticException). Always ensure the divisor is non-zero before performing division.
Also read: Data Types in Java
These operators are used to compare two values. They return a boolean value (true or false), indicating whether the comparison holds.
Operator | Description | Example |
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal | a >= b |
<= | Less than or equal | a <= b |
Example:
int x = 10;
int y = 5;
System.out.println(x == y); // Output: false
System.out.println(x > y); // Output: true
Logical operators are used to combine conditional statements and return boolean values. These are commonly used in if, while, and for loops.
Operator | Description | Example |
&& | Logical AND | a && b |
` | ` | |
! | Logical NOT | !a |
Short-Circuit Evaluation: Java's && (AND) and || (OR) operators use short-circuit evaluation. This means that if the first condition is sufficient to determine the result, the second condition won’t be evaluated.
For example:
boolean a = true;
boolean b = false;
if (a && b) {
// This block won't execute because b is false
}
Example:
boolean a = true;
boolean b = false;
System.out.println(a && b); // Output: false
System.out.println(a || b); // Output: true
System.out.println(!a); // Output: false
Must explore: Types of Variable in Java
Assignment operators are used to assign values to variables. The basic assignment operator is =, but Java also provides shorthand operators for combined operations.
Operator | Description | Example |
= | Assignment | a = 10 |
+= | Add and assign | a += 5 |
-= | Subtract and assign | a -= 3 |
*= | Multiply and assign | a *= 2 |
/= | Divide and assign | a /= 2 |
%= | Modulo and assign | a %= 3 |
Example:
int a = 5;
a += 3; // a = a + 3; now a = 8
a *= 2; // a = a * 2; now a = 16
System.out.println(a); // Output: 16
Must read: Loops in Java
Unary operators operate on a single operand. They are commonly used to increment, decrement, or negate values.
Operator | Description | Example |
++ | Increment (increase by 1) | a++ |
-- | Decrement (decrease by 1) | a-- |
+ | Unary plus (positive value) | +a |
- | Unary minus (negate value) | -a |
Example:
int a = 5;
System.out.println(++a); // Output: 6 (pre-increment)
System.out.println(a--); // Output: 6 (post-decrement)
Also check: Control Statements in Java
Bitwise operators manipulate the individual bits of integer types. They are often used in low-level programming, such as optimizing memory usage or in networking protocols.
Operator | Description | Example |
& | Bitwise AND | a & b |
` | ` | Bitwise OR |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~a |
<< | Left shift | a << 2 |
>> | Right shift | a >> 2 |
>>> | Unsigned right shift | a >>> 2 |
Example:
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
System.out.println(a & b); // Output: 1 (0001 in binary)
System.out.println(a | b); // Output: 7 (0111 in binary)
The ternary operator is a shorthand for the if-else statement. It is often used when you need to return one of two values based on a condition.
Operator | Description | Example |
? : | Ternary conditional | condition ? value1 : value2 |
Example:
int age = 18;
String result = (age >= 18) ? "Adult" : "Minor";
System.out.println(result); // Output: Adult
Operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated first.
For example:
int result = 10 + 2 * 3; // Multiplication has higher precedence than addition
System.out.println(result); // Output: 16
Operator associativity defines the direction in which operators of the same precedence are evaluated. For most operators, the associativity is left-to-right, but for the assignment operator (=), it is right-to-left.
int max = Integer.MAX_VALUE;
System.out.println(max + 1); // Output: -2147483648 (overflow)
Java operators are essential tools for performing operations on variables and values in any Java program.
From arithmetic and relational operations to logical and bitwise manipulations, understanding the different types of operators—and their precedence and associativity—is key to writing clean, efficient, and error-free code.
By mastering operators, you'll be able to tackle complex programming challenges, handle data efficiently, and avoid common pitfalls. Keep practicing with real-world examples, and you'll soon find yourself using operators like a pro!
The = operator is the assignment operator, used to assign a value to a variable, e.g., int a = 5;. On the other hand, == is the equality operator, which is used to compare two values or variables to check if they are equal, e.g., if (a == 5). Confusing these two can lead to unexpected behavior in your code.
Short-circuit evaluation is a feature of the logical operators && (AND) and || (OR) in Java, where the second condition is evaluated only if necessary. For example, in the expression a && b, if a is false, Java will not evaluate b because the result is already determined, thus improving performance and avoiding unnecessary operations.
Dividing an integer by zero in Java results in an ArithmeticException. This is a runtime exception that occurs because division by zero is undefined for integer types, so it's important to always check for zero before performing division to avoid crashing your program.
The ++ and -- operators are used to increase or decrease a variable's value by 1, respectively. They can be used as prefix (e.g., ++a) or postfix (e.g., a++), where the prefix version increments the value before the expression is evaluated, and the postfix version increments the value after the expression is evaluated.
Yes, the & operator can be used with boolean values in Java, but it works differently from the && operator. While && uses short-circuit evaluation, & evaluates both operands regardless of the first operand’s value, which can be less efficient but useful when both conditions need to be checked.
Bitwise operators in Java perform operations at the binary level. They are typically used for low-level programming tasks, such as manipulating flags, optimizing memory usage, or performing fast arithmetic in performance-critical applications. For example, &, |, ^, <<, and >> allow you to manipulate the individual bits of data.
The ternary operator is a concise alternative to an if-else statement and is written as condition ? value1 : value2. If the condition evaluates to true, it returns value1; otherwise, it returns value2. This operator is often used to simplify conditional assignments in code.
The += operator is a shorthand for adding a value to a variable and then assigning the result back to the variable. For example, a += 5; is equivalent to a = a + 5; and helps simplify code, especially in loops or when updating a value multiple times.
Operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated first. For example, multiplication (*) has higher precedence than addition (+), so in the expression 10 + 2 * 3, the multiplication will be performed first, resulting in 16.
The << operator performs a left shift, which shifts the bits of a number to the left, effectively multiplying it by powers of 2. Conversely, the >> operator performs a right shift, shifting bits to the right, which divides the number by powers of 2. These operations are efficient for certain calculations, especially when working with large datasets.
In Java, the == operator compares object references, not their contents. This means that it checks whether two references point to the same object in memory. To compare the actual values of objects, you should use the .equals() method, which is designed to compare the content of objects, such as strings or custom classes.
Take the Free Quiz on Java
Answer quick questions and assess your Java knowledge
Disclaimer
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.