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 8Subtraction (-)
int result = 10 - 4; // result will be 6Multiplication (*)
int result = 6 * 7; // result will be 42Division (/)
double result = 15.0 / 4.0; // result will be 3.75Modulus (%)
int result = 17 % 4; // result will be 12. Relational Expressions
Equal to (==)
boolean isEqual = (5 == 5); // isEqual will be trueNot equal to (!=)
boolean isNotEqual = (6 != 3); // isNotEqual will be trueGreater than (>)
boolean isGreaterThan = (10 > 8); // isGreaterThan will be trueLess than (<)
boolean isLessThan = (3 < 9); // isLessThan will be trueGreater than or equal to (>=)
boolean isGreaterOrEqual = (7 >= 7); // isGreaterOrEqual will be trueLess than or equal to (<=)
boolean isLessOrEqual = (4 <= 6); // isLessOrEqual will be true3. Logical Expressions
AND (&&)
boolean result = true && false; // result will be falseOR (||)
boolean result = true || false; // result will be trueNOT (!)
boolean result = !true; // result will be false4. Assignment Expressions
Simple Assignment (=)
int x = 10; // x is assigned the value 10Compound Assignment (+=, -=, *=, /=, %=)
int y = 5;
y += 3; // y will be 85. 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 1OR (|)
int result = 5 | 3; // result will be 7XOR (^)
int result = 5 ^ 3; // result will be 6Bitwise NOT (~)
int result = ~5; // result will be -6Left Shift (<<)
int result = 5 << 2; // result will be 20Right Shift (>>)
int result = 16 >> 2; // result will be 4Unsigned Right Shift (>>>)
int result = -16 >>> 2; // result will be 10737418207. instanceof Expression
Object obj = new String("Hello");
boolean isString = obj instanceof String; // isString will be true8. Type Cast Expression
double doubleValue = 5.67;
int intValue = (int) doubleValue; // intValue will be 59. Method Call Expression
String text = "Hello, World!";
int length = text.length(); // length will be 1310. Array Access Expression
int[] numbers = {1, 2, 3, 4, 5};
int thirdNumber = numbers[2]; // thirdNumber will be 311. 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?