Page 1 of 1
About Question enthuware.ocpjp.v7.2.1379 :
Posted: Sun Sep 15, 2013 2:05 am
by The_Nick
Hi everyone,
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.
Does it mean that if it was:
Code: Select all
public static void main(String[] args) { String s = "going"; print(s="gone", this.getString()); } static void print(String a, String b) { System.out.println(a +", "+ b ); } String getString(){return "on the run" + (1/0) ;}
Getting an ArithmeticException on the second argument of print would erase the assignment "s="gone" of the first argument right?
The_Nick.
Re: About Question enthuware.ocpjp.v7.2.1379 :
Posted: Sun Sep 15, 2013 5:55 am
by admin
Did you try it out?
-Paul.
Re: About Question enthuware.ocpjp.v7.2.1379 :
Posted: Sun Sep 15, 2013 6:08 am
by admin
You might want to go through
http://docs.oracle.com/javase/specs/jls ... l#jls-15.6 for understanding the details.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1379 :
Posted: Sun Sep 15, 2013 9:04 am
by The_Nick
Yep I got what you mean, I have tried it out sorry for asking I could have avoided it.
The_Nick.
Re: About Question enthuware.ocpjp.v7.2.1379 :
Posted: Sun Aug 09, 2015 1:26 am
by alkour
It tried this code.
Code: Select all
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);
}
}
It compiles and run without errors with output
Why the correct answer is "It will not compile"?
Re: About Question enthuware.ocpjp.v7.2.1379 :
Posted: Sun Aug 09, 2015 2:17 am
by admin
The correct option is option 1, "going, gone".
Re: About Question enthuware.ocpjp.v7.2.1379 :
Posted: Wed Nov 18, 2015 2:10 pm
by fariz.siracli
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.
Re: About Question enthuware.ocpjp.v7.2.1379 :
Posted: Sun Dec 06, 2015 4:54 am
by Etruskas
@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.