Java Programming for Beginners: From Introduction To Your First Java Project

Concepts:

Introduce arithmetic (+, -, *, /), assignment (=, +=), relational (==, !=, <), and logical operators (&&, ||).

Java Operators
Operators are used to perform operations on variables and values.

For example we use the + operator to add together two values:

int x= 50 + 20;

it can also be used to add together a variable and a value.

int x= 50 + 20; // 70 ( 50 + 20)
int y= x + 100; // 170 (70 + 100)
int z= y + y;     // 340 (170 + 170)

Comparison Operators

Compare two variables and, return value of a comparison is either true or false.

System.out.println(x > y);

Output: False

Code example:

public class OperatorsExample {
public static void main(String[] args) {
int a = 10;
int b = 3;
System.out.println(“Addition: ” + (a + b));// 13
System.out.println(“Division: ” + (a / b)); // 3 (integer division)
boolean result = (a > b) && (a != b);
System.out.println(“Logical result: ” + result); // true
}
}

Logical operators

&&  Logical and
||   Logical or
!     Logical not

 

Quiz: Write expressions using different operators and predict the outcome based on operator precedence.