The assignment of the string "gone" to s occurs after the first argument to print has been evaluated. If evaluation of an argument expression completes abruptly, no part of any argument expression to its right appears to have been evaluated.
public class StringEvaluation {
public static void main(String[] args) {
String s = "going";
print(s, s = "gone");
}
static void print(String a, String b) {
System.out.println(a + " , " + b);
}
}
hello. I could not understand explanation . The link also did not give me idea.
so please explain why does it return "going, gone" instead of "gone, gone". The value of variable s becomes "gone" after argument evaluation and in body of method it should print that value.
@fariz.siracli
These code lines:
String s = "going";
print(s, s = "gone");
resolves like this:
1. Java detected statement print(s, s = "gone");
2. Java resolves first operand s.
Resolved: print("going", s = "gone")
3. Java resolves second operand s = "gone"
Second operand is an expression itself.
3.1. Java resolves that expression s = "gone". Variable s is asigned "gone"
3.2. Java now has resolved all arguments. The call is print("going", "gone")
4. Java executes the call print("going", "gone").
Thinking more abstract.
People in math lessons are tend to learn to resolve complex expressions starting at most nested parenthesis.
Java works differently. Java resolves expressions starting from left to right.
And then, similar as in math, java works acording to operator precedence rules.