Does Java automatically save an 'evaluate' statement in a predefined Java variable?

Jason V :

Say I perform a simple add/concatenation statement:

variable + newInput

Without setting the calculated value to a new variable, as in:

variable = variable + newInput

or

variable += newInput

Does Java have some sort of specifier to be able to use the computed sum or concatenated string?

Apparently in Python it is automatically saved in the implicit global variable _ -which is implementable like

Print(_)

Is there anything like this in Java?

user2357112 supports Monica :

More than just not saving the result, Java will outright refuse to compile your program if it contains such a line, precisely because the result would be unsaved and unusable if it was allowed:

public class Main
{
    public static void main(String[] args)
    {
        1+2;
    }
}

Result:

Main.java:5: error: not a statement
        1+2;
         ^
1 error

Java does not allow arbitrary expressions as statements, and addition expressions are not considered valid Java statements.

The expressions that are allowed as statements by themselves are listed in the JLS:

ExpressionStatement:
  StatementExpression ;

StatementExpression:
  Assignment 
  PreIncrementExpression 
  PreDecrementExpression 
  PostIncrementExpression 
  PostDecrementExpression 
  MethodInvocation 
  ClassInstanceCreationExpression

Assignment, increment, decrement, method calls, and new Whatever(), all things with side effects or potential side effects. Barring possible side effects of an implicit toString() call, + cannot have side effects, so to catch probable errors, Java forbids addition expressions from being statements.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=442760&siteId=1