• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

please explain

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you run this? The answer is -10, not 2.
 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is not a typo. i checked the program for any misprints but apparently, there wasn't any.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out the following post which discusses a good technique for handling such mixed expressions:
https://coderanch.com/t/190825/java-programmer-SCJP/certification/Array
 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much for all your responses.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s.o.p is a short way to say System.out.println.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks corey ,
now i get the result

regds
swarna
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic