This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explain

 
Maria Garcia
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you run this? The answer is -10, not 2.
 
Maria Garcia
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice!
if
k = k++ + -i++ - ++i - j++;
The answer is -10
if
k = k++ + -i++ + ++i - j++;
The answer is 2
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maria Garcia
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is not a typo. i checked the program for any misprints but apparently, there wasn't any.
 
zarina mohammad
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maria Garcia
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much for all your responses.
 
Bindesh Vijayan
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
zarina mohammad
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is S.o.p here?
k++ at the time of S.o.p ,that makes k=-10
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s.o.p is a short way to say System.out.println.
 
swarna kumar
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi corey,
k= 1+(-4)-5-2= -10. or
k=1+(-4)-6-2
which is correct how?,
regds
swarrna
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks corey ,
now i get the result

regds
swarna
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic