In Java, an expression is a combination of literals, variables, operators, and method calls that evaluates to a value. Expressions are used to compute values or produce side effects in Java programs.
Here are some examples of expressions in Java:
1 + 2 // addition expression "Hello, " + "World!" // string concatenation expression x > y // relational expression Math.sqrt(x) // method call expression
In the first example, an addition expression is used to add the values 1 and 2. In the second example, a string concatenation expression is used to concatenate the strings "Hello, " and "World!". In the third example, a relational expression is used to compare the values of x
and y
. In the fourth example, a method call expression is used to call the sqrt()
method of the Math
class and calculate the square root of x
.
Expressions can be combined to create more complex expressions, and they can be used in various contexts, such as assignment statements, control statements, and method calls.