• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help  RSS feed

 
Pawan Arora
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.
 
Steve Luke
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...
 
Rob Spoor
Sheriff
Posts: 21131
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
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • 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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
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.
 
Rene Larsen
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: 21131
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.
 
Jacob Steingart
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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!