Master Java expressions with comprehensive guide covering arithmetic, relational, logical, assignment, conditional, and ternary expressions with real examples and practice questions.
Used for calculations.
int a = 10;
int b = 3;
int sum = a + b; // 13
int diff = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3 (integer division)
int remainder = a % b; // 1
👉 a + b, a * b, a % b are expressions.
Used to compare values. Result is true or false.
int x = 5;
int y = 8;
boolean result1 = x < y; // true
boolean result2 = x == y; // false
boolean result3 = x != y; // true
Used to combine conditions.
int age = 20;
boolean hasID = true;
boolean canEnter = (age >= 18) && hasID; // true
Operators:
&& → AND|| → OR! → NOTAssign values to variables.
int num = 10;
num += 5; // same as num = num + 5 (15)
num *= 2; // same as num = num * 2 (30)
Operate on a single value.
int count = 5;
count++; // 6 (increment)
count--; // 5 (decrement)
boolean isOpen = false;
boolean status = !isOpen; // true
A shortcut for if-else.
int marks = 75;
String result = (marks >= 50) ? "Pass" : "Fail";
Used with text.
String firstName = "Yasir";
String lastName = "Bhutta";
String fullName = firstName + " " + lastName;
// Yasir Bhutta
Combining different types.
int a = 5;
int b = 2;
double result = a / (double) b; // 2.5
System.out.println()Very common for beginners.
System.out.println(10 + 5); // 15
System.out.println("Sum = " + (10 + 5)); // Sum = 15
Q1. What will be the output?
System.out.println(8 + 2 * 5);
Q2. What is the value of result?
int a = 6;
int b = 4;
boolean result = a >= b;
Q3. What will be printed?
int x = 10;
System.out.println(x++ + 5);
Q4. Identify the expression in the statement:
double avg = (a + b + c) / 3.0;
Q5. What is the output?
System.out.println(15 % 4);
Q6. What will be printed?
int num = 5;
System.out.println(num > 3 && num < 10);
Q7. What is the value of status?
int marks = 45;
String status = (marks >= 50) ? "Pass" : "Fail";
Q8. Which operator is used to reverse a boolean value?
boolean flag = false;
Q9. What will be the output?
System.out.println("Result: " + 10 + 20);
Q10. What is the result of this expression?
int result = 20 / 4 + 3 * 2;
Write a Java program that:
++ and --marks"Pass" if marks ≥ 50, else "Fail"age and hasIDAllow entry if:
principal, rate, timeFormula:
SI = (P × R × T) / 100
+int result = 10 + 2 * 3 - 4 / 2;
| [Difference Between Expressions and Statements in Java | Java Basics Explained](/java/docs/expressions/expressions-statements.html) |