Java Operators Overview
Java operators are categorized into several types:
- Assignment operator: =
- Arithmetic operators: +, -, *, /
- Increment/decrement: ++, --
- Relational operators: ==, !=, equals()
- Logical operators: !, &&, ||, &, |
Assignment Operator
The assignment operator stores a value into a variable. The value on the right side of the equals sign is assigned to the variable on the left.
public class AssignmentExample {
public static void main(String[] args) {
// Primitive assignment
int number = 15;
// Reference assignment
String text = "Sample text";
// Object assignment
CustomObject obj = new CustomObject();
}
}
Primitive and String assignments are straightforward. For object assignment, CustomObject is the type, obj is the variable, and new CustomObject() creates an object instance assigned to the variable.
Arithmetic Operators
Arithmetic operators perform mahtematical calculations.
public class ArithmeticDemo {
public static void main(String[] args) {
int x = 50;
int y = 25;
int result;
// Addition
result = x + y;
System.out.println("x + y: " + result);
// Subtraction
result = x - y;
System.out.println("x - y: " + result);
// Multiplication
result = x * y;
System.out.println("x * y: " + result);
// Division
result = x / y;
System.out.println("x / y: " + result);
// Modulus
result = x % y;
System.out.println("x % y: " + result);
}
}
Output:
x + y: 75
x - y: 25
x * y: 1250
x / y: 2
x % y: 0
Important considerations:
- Ensure the result variable can accommodate the calculated value, especially for multiplication operations
- Operator precedence determines calculaiton order
public class PrecedenceExample {
public static void main(String[] args) {
int a = 4;
int b = 6;
int c = 12;
int d = 5;
int output1 = a + b * c - d;
int output2 = (a + b) * (c - d);
System.out.println("output1 = " + output1 + " output2 = " + output2);
}
}
// Execution result
// output1 = 71 output2 = 70
Increment and Decrement Operators
Pre-increment/decrement (++var, --var) perform the operation before using the value. Post-increment/decrement (var++, var--) use the value first, then perform the operation.
public class IncrementDemo {
public static void main(String[] args) {
int value1 = 150;
// Pre-increment
int result1 = ++value1;
System.out.println("Pre-increment:");
System.out.println(result1);
System.out.println(value1);
// Post-increment
int value2 = 80;
int result2 = value2++;
System.out.println("Post-increment:");
System.out.println(result2);
System.out.println(value2);
}
}
/* -------------------- Execution Result -------------------- */
Pre-increment:
151
151
Post-increment:
80
81
Relational Operators
Relational operators compare values and return boolean results. Operators include: >, <, >=, <=, !=, ==, equals(). They can also work directly with boolean variables.
public class ComparisonExample {
public static void main(String[] args) {
int num1 = 5;
int num2 = 5;
System.out.println(num1 == num2);
System.out.println(num1 != num2);
}
}
//---------------- Execution Result -----------------
true
false
Key differences between == and equals():
- == compares primitive values or object references (memory addresses)
- equals() compares object content (when properly overridden)
String class overrides equals() to compare content rather than memory addresses.
Logical Operators
&& and & are AND operators. && is short-circuiting - if the first condition is false, the second condition isn't evaluated.
|| and | are OR operators. || is short-circuiting - if the first condition is true, the second condition isn't evaluated.
! is the NOT operator, which inverts the boolean value.