Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java assignment operator execution

In Java, I understand that assignment evaluates to the value of the right operand, so statements like x == (y = x) evaluate to true.

This code, however, outputs false.

public static void main(String[]args){     String x = "hello";     String y = "goodbye";     System.out.println(x.equals(x = y)); } 

Why is this? In my understanding, it first evaluates (x = y), which assigns x the value of y, and then returns the value of y. Then x.equals(y) is evaluated, which should be true since x and y should share the same references now, but instead, I get false.

Screenshot showing the source and that the output is "false"

What is happening here?

like image 531
Sam Avatar asked Jun 21 '18 13:06

Sam


People also ask

What actions does the assignment operator execute?

The assignment operators return the value of the object specified by the left operand after the assignment. The resultant type is the type of the left operand. The result of an assignment expression is always an l-value. These operators have right-to-left associativity.

How does assignment operator work in Java?

Assignment operators are used in Java to assign values to variables. For example, int age; age = 5; Here, = is the assignment operator.

What are the two steps that take place when an assignment statement is executed in Java?

What are the two steps that take place when an assignment statement is executed? Store the value in the variable, evaluate the expression.

What are assignment operators in Java?

Java Assignment Operators are classified into two categories, such as simple and compound assignment operators. Assignment operators, as the name says, are used for assigning the values to the variables involved in the operations.

What is simple assignment operator in C++?

Simple Assignment Operator: The Simple Assignment Operator is used with the “=” sign where the left side consists of the operand and the right side consists of a value. The value of the right side must be of the same data type that has been defined on the left side. 2.

Which operators are used to assign values to a variable?

These operators are used to assign values to a variable. The left side operand of the assignment operator is a variable, and the right side operand of the assignment operator is a value.

What is the use of divide and assignment operator?

It multiplies right operand with the left operand and assigns the result to the left operand. Divide AND assignment operator. It divides left operand with the right operand and assigns the result to the left operand.


1 Answers

First of all: that's an interesting question, but should never come up in "real code", as assigning to the variable you call in the very same line is confusing even if you know how it works.

What happens here is these 3 steps:

  1. figure out which object to call the method on (i.e. evaluate the first x, this will result in a reference to the String "hello")
  2. figure out the parameters (i.e. evaluate x = y, which will change x to point to the String "goodbye" and also return a reference to that String)
  3. call the method equals on the result of #1 using the result of #2 as the parameter (which will be references to the Strings "hello" and "goodbye" respectively).

Looking at the byte code produced for that method makes it clear (assuming you're fluent in Java bytecode):

     0: ldc           #2                  // String hello      2: astore_1      3: ldc           #3                  // String goodbye      5: astore_2      6: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;      9: aload_1     10: aload_2     11: dup     12: astore_1     13: invokevirtual #5                  // Method java/lang/String.equals:(Ljava/lang/Object;)Z     16: invokevirtual #6                  // Method java/io/PrintStream.println:(Z)V     19: return 

Line #9 is step 1 above (i.e. evaluates x and remembers the value).

Line #10-12 is step 2. It loads y, duplicates it (once for assigning, once for the return value of the assignment expression) and assigns it to x.

Line #13 invokes equals on the result computed in Line #9 and the result of Lines #10-12.

like image 169
Joachim Sauer Avatar answered Nov 15 '22 14:11

Joachim Sauer