9

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?

6
  • 6
    Python has a REPL. Java doesn't. So there's no need for such a feature. Jun 6, 2017 at 13:25
  • 1
    Not in Java, no. You could do it in C++, with some effort.
    – Bathsheba
    Jun 6, 2017 at 13:26
  • 2
    In Python's REPL the last return value is put into the _ global, which is not present in the Python interpreter for programs
    – cat
    Jun 6, 2017 at 15:51
  • 5
    @OliverCharlesworth Java9 comes with a REPL. The specifier to be able to access the sum in that case would be $n where n is a sequence Jun 6, 2017 at 17:47
  • 1
    @Jean-FrançoisSavard that is... if it ever comes out! :( Jun 6, 2017 at 19:07

5 Answers 5

6

No. It does not have anything like this. You have to assign the computed value to a variable, otherwise it will be lost and consequently collected by the garbage collector.

The best option is to use a special operator so not to use an extra variable but assign the result to an old one. This is a Shorthand operator.

Variable += NewInput
1
  • Really? thats probably why i couldnt find anything about it.
    – Jason V
    Jun 6, 2017 at 13:27
5

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.

4

You can for sure do:

variable + newInput

but the result of that operation must be assigned to a variable, if not, it will get lost...

the most you can get is

variable += newInput

whihch is similar to

variable = variable + newInput
1
  • the first half is what i was really asking, if the result will be lost or if there is some designated variable java saves them to. Thank you!
    – Jason V
    Jun 6, 2017 at 13:38
2

The point is: the + operator in Java simply takes two operands and returns a result (either numerical, or as string concatenation).

Without assigning this result to something (like returning it from a method; or as shown in your example) ... it is like: the operation never takes place.

This operation doesn't have any side effects on its operands; and there is no way of accessing this result.

Beyond that, there is no operator overloading in Java. So it is also not possible to do some black magic that somehow stores the result of operation as side effect. You could theoretically add an agent to the JVM, that intercepts at runtime to do something upon an add operation, but that is more like: "technically possible", but nothing you would do in practical reality.

Other JVM languages, like Scala for example might use it implicitly - the last expression in a method is always returned, even when leaving out the return statement (in scala).

0

The statement you show is evaluated and nothing is done with it. Unless you bind a variable to the result, the evaluation occurs without effect.

This will not be flagged as an error by the compiler.

6
  • You might get an NPE if one is a boxed type and the other a primitive. So it's not necessarily a no-op.
    – Bathsheba
    Jun 6, 2017 at 13:30
  • A NullPointerExceptin is a runtime error, not a compiler error. Jun 6, 2017 at 13:32
  • Which statement are you referring to? There is no valid java statement in the question.
    – Hulk
    Jun 7, 2017 at 8:02
  • The statement variable + newInput Jun 7, 2017 at 14:54
  • While this is a valid expression (jls-15), it is not a valid statement as far as I understand (jls-14.5).
    – Hulk
    Jun 8, 2017 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.