I know the Operator Precedence list, but I just cannot figure out what is the execution precedence in this code in "LINE 1". What Object is created before? For example: the My String or the new Precedence()? How can we apply the Operator Precedence rule in this example?
public class Precedence {
public String s;
public static void main (String ... args){
String a = new Precedence().s="My String"; // LINE 1
System.out.println(a);
}
}
OUTPUT:
My String
This
is a local variable declaration statement with an initialization expression.
ais the declarator. It's evaluated to produce a variable (itself). Then the initialization expression is evaluated.This
is an assignment expression. The left hand side of the operator is evaluated first to produce a variable, so
new Precedence()is evaluated first, instantiates the classPrecedence, producing a reference to an object. Then the right hand side of the assignment is evaluated, theStringliteral"My String", so a reference to aStringobject is produced. Then the assignment happens assigning the reference to theStringobject to the variablesof the object referenced by the value returned by the new instance creation expression.Finally, since
The value that was assigned to the field
sof thePrecedenceobject is also assigned to the variablea.