Sep 11, 2025
This lecture explores Java expressions in detail, including their components, types of operators, precedence rules, type conversion, and assignment variations. It also provides practical code examples to illustrate key concepts.
42
, 3.14
, 'A'
, "Hello"
)x
, total
)Math.sqrt(9)
)a + b * c
)Math.PI
(Ï€), Math.E
(e)Integer.MAX_VALUE
, Integer.MIN_VALUE
Double.MAX_VALUE
, Double.MIN_VALUE
, Double.POSITIVE_INFINITY
, Double.NEGATIVE_INFINITY
, Double.NaN
+
(addition), -
(subtraction), *
(multiplication), /
(division), %
(remainder).byte
, short
, int
, long
, float
, double
) and char
(treated as an integer).double result = 37.4 + 10; // 10 is converted to 10.0
int x = 7 / 2; // x is 3
double y = 7.0 / 2; // y is 3.5
%
operator computes the remainder:
int r = 34577 % 100; // r is 77
int even = (N % 2 == 0) ? 1 : 0; // checks if N is even
%
also works with real numbers: double d = 7.52 % 0.5; // d is 0.02
-x
) negates a value; unary plus (+x
) exists but has no effect.+
operator concatenates strings with other values:
String s = "Score: " + 42; // s is "Score: 42"
++
increments and --
decrements a variable by 1. Can be used as prefix or postfix:
x++; // same as x = x + 1;
--y; // same as y = y - 1;
int y = x++; // y gets old value of x, x is incremented
int z = ++x; // x is incremented, z gets new value
++
or --
inside larger expressions to prevent confusion:
x = x++; // x remains unchanged
char
as well:
char ch = 'A'; ch++; // ch is now 'B'
==
(equal), !=
(not equal), <
, >
, <=
, >=
if (score >= 60) { ... }
char
types (comparison based on Unicode value):
'A' < 'a' // true
equals()
or compareTo()
:
if (str1.equals(str2)) { ... }
Double.NaN
is not equal to anything, even itself:
if (x == Double.NaN) // always false
Double.isNaN(x)
to check for NaN.&&
(logical "and"): if (x > 0 && y > 0) { ... }
||
(logical "or"): if (x == 0 || y == 0) { ... }
!
(logical "not"): if (!done) { ... }
&&
and ||
are short-circuited:
if (x != 0 && y / x > 1)
, if x
is 0, y / x
is never evaluated.booleanExpr ? expr1 : expr2
int next = (N % 2 == 0) ? (N / 2) : (3 * N + 1);
// If N is even, next = N/2; else, next = 3*N+1
=
assigns a value; assignment can be used as an expression:
if ((A = B) == 0) { ... } // assigns B to A, then checks if A is 0
int a = 17; double x = a; // OK
short b = a; // Error: no automatic conversion from int to short
short b = (short)a; // Explicit cast
(int)7.9453 // 7
int roll = (int)(6 * Math.random()) + 1;
char
and numeric types can be converted:
(char)97 // 'a'
(int)'+' // 43
(char)('A' + 2) // 'C'
String.valueOf(42) // "42"
Integer.parseInt("10") // 10
Double.parseDouble("17.42e-2") // 0.1742
x += y; // x = x + y;
x -= y; // x = x - y;
x *= y; // x = x * y;
x /= y; // x = x / y;
x %= y; // x = x % y;
String str = "tire";
str += 'd'; // str is now "tired"
++
, --
, !
, unary -
, unary +
, type-cast*
, /
, %
+
, -
<
, >
, <=
, >=
==
, !=
&&
||
?:
=
, +=
, -=
, *=
, /=
, %=
A * B / C
is evaluated as (A * B) / C
A = B = C
is evaluated as A = (B = C)
(A + B) * C
vs. A + (B * C)
3.14
, "Hello"
).++
/--
) that add or subtract 1 from a variable.&&
, ||
, !
).?:
) that selects between two expressions based on a condition.(Type)
.