Home > Net >  What value does a Method Invocation Expression for a function with void as a return type evaluate to
What value does a Method Invocation Expression for a function with void as a return type evaluate to

Time:09-11

The oracle java tutorial states: (emphasis by me)

An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value.

Given a function

public void doNothing() { }

what values does the Method Invocation Expression doNothing() evaluate to?

I'm wondering this because in my understanding void suggests that this function doesn't return anything.

CodePudding user response:

doNothing() is a method invocation, not an expression. As you quoted, an expression is a construct made up of variables, operators, AND method invocations... that evaluates to a single value. It would be the same as asking if a word, or a phrase is a sentence.

Consider the Map#put(K,V) method. The invocation of this method by itself, even when it returns a value, does not represent an expression in itself.

1  Map<String, Integer> numbers = new HashMap<>();
2  Integer oldVal = numbers.put("one", 1); // not an expression
3  boolean notNull = oldVal != null); // an expression

The main reason why the code on line 2 is not an expression is because no evaluation is being done. Line 2 is a simple method invocation, even though it returns a value. In contrast, line 3 is an expression because an evaluation is being done (the comparison of the notNull variable against null.

1  int value1 = 1   2; // an expression
2  int value2 = 5 * 2; // an expression
3  if (value1 == value2) {...} // an expression

In the above snippet, all 3 lines contain an expression. The obvious is line 3 (value1 == value2). The other two are mathematical expressions. Why? Because each one is a construct that has two operands, and an operator.

Going back to the method doNothing(), the invocation of this method cannot be used to construct an expression because it doesn't return a value. Therefore, it cannot be used to build an evaluation.

I hope this helps clarify what an expression is.

CodePudding user response:

The Java Language Specification is the authoratative source, rather than a tutorial.

When an expression in a program is evaluated (executed), the result denotes one of three things:

A variable (§4.12) (in C, this would be called an lvalue)

A value (§4.2, §4.3)

Nothing (the expression is said to be void)

Thus your doNothing() call is an expression, and your intuition is correct - it returns nothing.

Void expressions are subject to limitations:

An expression denotes nothing if and only if it is a method invocation (§15.12) that invokes a method that does not return a value, that is, a method declared void (§8.4). Such an expression can be used only as an expression statement (§14.8) or as the single expression of a lambda body (§15.27.2), because every other context in which an expression can appear requires the expression to denote something.

  •  Tags:  
  • java
  • Related