Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help  RSS feed

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the following programme was showing me the output 5
as far as the rules of pre increment, the value is incremented before being assigned and in post increment, the value is assigned to variable before being used. So why the output was 5 not 6?
Another agony,
Why the following programme compiling successfully?
Please help with suitable reason. Thanks in advance for help.
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jack back:
Why the following programme was showing me the output 5
as far as the rules of pre increment, the value is incremented before being assigned and in post increment, the value is assigned to variable before being used.



The post increment happens AFTER evaluating the value of the variable, and BEFORE assignment to the left hand side. The order is this:
a = 1;
EVAL:
Right Side = a = 1; [from a+=]
++a -> 1+a -> 2; [from ++a]
Right Side = 1 + 2; [from + ++a]
a++ -> a -> 2; [from a++]
Right Side = 1 + 2 + 2; [from + a++]
a++ -> a + 1 -> 3; [from a++]
Right Side = 5; [from a+= ++a + a++]
ASSIGN:
a = Right Side = 5;

So why the output was 5 not 6?
Another agony,
Why the following programme compiling successfully?



Because you are allowed to have public static inner classes inside an interface...
 
Sheriff
Posts: 21288
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Jack back",

Please read your private messages regarding an important announcement.

Thank you,

Rob
 
Marshal
Posts: 58345
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and, in future, please use a title which tells us what the thread is about; "Please Help" isn't very useful.
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve, thanks for your reply.
I still have one problem, why the a+= is not like a=a+1??
[ October 12, 2008: Message edited by: Jack Black ]
 
Author and ninkuma
Marshal
Posts: 66535
160
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Jack Black", did you check your private messages as Rob requested? Please check again.
 
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jack Black:
Thanks Steve, thanks for your reply.
I still have one problem, why the a+= is not like a=a+1??



a += means a = a + x
 
Rob Spoor
Sheriff
Posts: 21288
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
"Jack Black", did you check your private messages as Rob requested? Please check again.


Indeed. Unless you are a short pudgy actor / singer, either your name is not allowed or you need to show us that Jack Black really is your name.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No offense, but this sounds like a homework question... am I the only one thinking this?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jacob Steingart:
No offense, but this sounds like a homework question... am I the only one thinking this?



I think it is a question from homework - yes, but he isn't asking to have it done for him. He is asking for understanding the output, which I think is appropriate.

If it isn't homework (or studying for some test) then the correct answer is: Don't do that!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!