jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please explain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please explain" Watch "please explain" New topic
Author

please explain

Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
public class AddOpTest {
public static void main (String arg []){
int i = 4, j=2, k= 1;
k = k++ + -i++ - ++i - j++;

System.out.println("The value of k is " + ++k);
}
}
The answer is 2... and Im very confused


SCJP 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Have you run this? The answer is -10, not 2.


SCJP Tipline, etc.
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
i run this already. it really is 2.
i found an explanation from one of my study notes and is says that the reason lies on the assignment operator having the lowest precedence.
can anyone help me with this ?
Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41
Notice!
if
k = k++ + -i++ - ++i - j++;
The answer is -10
if
k = k++ + -i++ + ++i - j++;
The answer is 2


Val SCJP2 1.2<br />====>SCJD
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
A Ha!! Nice catch, Val! It looks like a typo.
Anyway, the key to these types of problems is that each operand is evaluated from left to right and the assignment is done last.
Do a search through this forum - you'll find questions just like this one have been discussed many times over. There are lots of examples all worked out so you should get a good feel for how to do these.
If you have problems, let me know.
Corey
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Maria Garcia:
i run this already. it really is 2.
i found an explanation from one of my study notes and is says that the reason lies on the assignment operator having the lowest precedence.
can anyone help me with this ?

If you would like to try 12 more questions on the topic of operators, then you might want to try the following
Single Topic Mock Exams. Just click on the Operator question and answer links near the bottom of the page.
If you want to book mark my mock exam, then you should use only the URL contained in my signature since the other pages are updated frequently.
There are twelve questions in the Operator Exam. Nine of the answers include detailed explanations.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
it is not a typo. i checked the program for any misprints but apparently, there wasn't any.
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
Maria,
i executed the code

guess what, i too got the value of k as -10.
k= 1+(-4)-5-2= -10.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Check out the following post which discusses a good technique for handling such mixed expressions:
http://www.coderanch.com/t/190825/java-programmer-SCJP/certification/Array


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86
thank you very much for all your responses.
Bindesh Vijayan
Ranch Hand

Joined: Aug 21, 2001
Posts: 34
guess what, i too got the value of k as -10.
k= 1+(-4)-5-2= -10.

I think it should be like this:
k=1+(-4)-6-2
k=-11;
and then
k++ at the time of S.o.p ,that makes k=-10
Correct me if wrong
Thanks
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
yes i think bindesh is right,
k = k++ + -i++ - ++i - j++;
k = 1+(-4)-(6)-2
i++ .. post increment,
in the 3rd step ++i , i is already 5 (because of previous i++),so ++i of 5 makes it 6.
let me know if otherwise..


please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
what is S.o.p here?
k++ at the time of S.o.p ,that makes k=-10
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
s.o.p is a short way to say System.out.println.
swarna kumar
Greenhorn

Joined: Nov 11, 2001
Posts: 17
hi,
i got little bit confusion ,
once we solve the expression we get k=-10,
then
System.out.println("The value of k is " + ++k);
again here is one expression ++k right, if we solve this we get -9,
how we get -10,
correct me this,pls
regds
swarna
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by swarna kumar:
hi,
i got little bit confusion ,
once we solve the expression we get k=-10,

No, the value of k after the major expression is evaluated is -11. Only after the pre-increment in the println line is the value changed to -10.
Corey
swarna kumar
Greenhorn

Joined: Nov 11, 2001
Posts: 17
hi corey,
k= 1+(-4)-5-2= -10. or
k=1+(-4)-6-2
which is correct how?,
regds
swarrna
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The expression evaluates to this:
k = 1 + (-4) - 6 - 2
Therefore, k = -11.
Finally, when you increment k in the output line, you get -10 as output.
Corey
swarna kumar
Greenhorn

Joined: Nov 11, 2001
Posts: 17
thanks corey ,
now i get the result

regds
swarna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please explain
 
Similar Threads
++ Operators
three for Loops?
simple q but just to confirm
Local Variables
Fibonacci Sequence Problem