Expressions
Expressions are fundamental building blocks in Java programming. They represent a combination of variables, operators, and constants that produce a value. Understanding expressions is crucial for writing efficient and correct Java code. In this documentation, we will explore various types of expressions in Java along with suitable examples.
Types of Expressions
Arithmetic Expressions
Addition
Subtraction
Multiplication
Division
Modulus
Relational Expressions
Equal to
Not equal to
Greater than
Less than
Greater than or equal to
Less than or equal to
Logical Expressions
AND
OR
NOT
Assignment Expressions
Simple Assignment
Compound Assignment
Conditional (Ternary) Expression
Basic Ternary Operator
Nested Ternary Operators
Bitwise Expressions
AND (&)
OR (|)
XOR (^)
Bitwise NOT (~)
Left Shift (<<)
Right Shift (>>)
Unsigned Right Shift (>>>)
Instanceof Expression
Type Cast Expression
Method Call Expression
Array Access Expression
String Concatenation Expression
1. Arithmetic Expressions
Addition (+)
int result = 5 + 3; // result will be 8
Subtraction (-)
int result = 10 - 4; // result will be 6
Multiplication (*)
int result = 6 * 7; // result will be 42
Division (/)
double result = 15.0 / 4.0; // result will be 3.75
Modulus (%)
int result = 17 % 4; // result will be 1
2. Relational Expressions
Equal to (==)
boolean isEqual = (5 == 5); // isEqual will be true
Not equal to (!=)
boolean isNotEqual = (6 != 3); // isNotEqual will be true
Greater than (>)
boolean isGreaterThan = (10 > 8); // isGreaterThan will be true
Less than (<)
boolean isLessThan = (3 < 9); // isLessThan will be true
Greater than or equal to (>=)
boolean isGreaterOrEqual = (7 >= 7); // isGreaterOrEqual will be true
Less than or equal to (<=)
boolean isLessOrEqual = (4 <= 6); // isLessOrEqual will be true
3. Logical Expressions
AND (&&)
boolean result = true && false; // result will be false
OR (||)
boolean result = true || false; // result will be true
NOT (!)
boolean result = !true; // result will be false
4. Assignment Expressions
Simple Assignment (=)
int x = 10; // x is assigned the value 10
Compound Assignment (+=, -=, *=, /=, %=)
int y = 5;
y += 3; // y will be 8
5. Conditional (Ternary) Expression
Basic Ternary Operator
int age = 18;
String message = (age >= 18) ? "Adult" : "Minor";
Nested Ternary Operators
int score = 75;
String result = (score >= 90) ? "A" : (score >= 80) ? "B" : (score >= 70) ? "C" : "D";
6. Bitwise Expressions
AND (&)
int result = 5 & 3; // result will be 1
OR (|)
int result = 5 | 3; // result will be 7
XOR (^)
int result = 5 ^ 3; // result will be 6
Bitwise NOT (~)
int result = ~5; // result will be -6
Left Shift (<<)
int result = 5 << 2; // result will be 20
Right Shift (>>)
int result = 16 >> 2; // result will be 4
Unsigned Right Shift (>>>)
int result = -16 >>> 2; // result will be 1073741820
7. instanceof Expression
Object obj = new String("Hello");
boolean isString = obj instanceof String; // isString will be true
8. Type Cast Expression
double doubleValue = 5.67;
int intValue = (int) doubleValue; // intValue will be 5
9. Method Call Expression
String text = "Hello, World!";
int length = text.length(); // length will be 13
10. Array Access Expression
int[] numbers = {1, 2, 3, 4, 5};
int thirdNumber = numbers[2]; // thirdNumber will be 3
11. String Concatenation Expression
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName; // fullName will be "John Doe"
This documentation provides a comprehensive overview of various Java expressions with examples.
Last updated
Was this helpful?