In my Java book, it says that “an expression is a statement that can convey a return value.” This is different than my traditional understanding. I thought an expression DOES return a value. Not CAN return a value.
this is from Sams Teach Yourself Java in 21 Days.
A mathematical expression always returns something, but a Java expression doesn’t have to. The Java Specification defines what exactly is meant by the term expression in the Java language. Another difference is that expressions can, and often do, have side effects in Java. A side effect is pretty much anything that happens other than returning a value.
Quoting the Java Language Specification:
For example
system.out.println("Hello World");doesn’t return a value, but it does printHello Worldto the output stream. This process of outputting data is a side effect of callingprintln. Functional languages, in contrast, attempt to minimize dependence on side effects and stick more closely to the mathematical definition of an expression.Quoting from the JLS again, here is the BNF grammar for an expression:
You can see that a
MethodInvocationis an expansion ofPrimaryNoNewArray, which is an expansion ofPrimary(expression).