• 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

K&B Book for Scjp 6 - Chapter 5 question 8 doubt

 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

I am copying this code from Kathy and Sierra book for SCJP 6. It is from chapter 5. Self test question 8


I completely understand the code and it's logic but i am unable to understand why the value of x at first iteration used is 9 rather then 8.
To me if you are post incrementing, it means first use the previous value and then increment. Here following sequence i understood.

1- x was initialized with 7
2- As class loaded static initializer block is executed and x is post incremented to 8.
3- After this point inside the loop where x is again incremented, i think x should be incremented to 9 which happens but shouldn't first value
8 be used and then in next iteration value 9 used.

I am sure, i am making some very silly mistake. But confused

Best Regards,
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I havent reached that part in the book but I think this is what happens when you execute the program

1. x is initialized to 7 in line 4
2. x is incremented to 8 in line 32
3. x is incremented to 9 in line 10
4. when the switch stmt is executed for the first time, the value of x is 9. which means case 9 would be resolved.

I am not sure why would you say x would have a value of 8 in the first iteration of the loop?
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:
3- After this point inside the loop where x is again incremented, i think x should be incremented to 9 which happens but shouldn't first value
8 be used and then in next iteration value 9 used.



Perhaps you were thinking about the loop behavior of a for statement that looks like this:

This code will postpone the increment to after the first iteration is done.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@prithvi
friend there is very very simple solution whenever we want to find out answers like this
why don't you print out the value of x whenever you are confused at what line what is happening
see the following code
that will explain a lot
compile and run it

and about the post increment
you a little misunderstood
see the following example

 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Srinivas Nagesh wrote:I havent reached that part in the book but I think this is what happens when you execute the program

1. x is initialized to 7 in line 4
2. x is incremented to 8 in line 32
3. x is incremented to 9 in line 10
4. when the switch stmt is executed for the first time, the value of x is 9. which means case 9 would be resolved.

I am not sure why would you say x would have a value of 8 in the first iteration of the loop?




Ignore the loop as a first reason, it is due to the class static initialization order. When a class first loads (not as an instance object) its static members are initialized first, (in this case x is assigned the value 7) next any static blocks are executed, in this case the static block x++ is run. Thus, by the time main runs, x is initialized to 8, hence after the first loop initialization x is once more incremented by the post increment operator, which ads one to the value of x (which is 8 following class loading) so it becomes 9 and then used in the switch statement, hence the output of 9.

Class load statics:

1.Static members
2.Static members (blocks)
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

I completely understand the logic and everything in this question, my doubt may be i am unable to clarify. Let say for suppose.



At line 1, first the value of j is printed which is 9 and then incremented. My question was shouldn't when this block of code is reached



Shouldn't in the above code snippet even first value of x which is 8 should be used and and at next iteration 9 to be used.
Or there is some difference between these two statements


Because in line 2 first previous value of x is used and then incremented.

I know i am stuck at a very silly confusion but still any pointers in this regard will be helpful.
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:Guys,

I completely understand the logic and everything in this question, my doubt may be i am unable to clarify. Let say for suppose.



Because in line 2 first previous value of x is used and then incremented.

I know i am stuck at a very silly confusion but still any pointers in this regard will be helpful.




I think there are two things here at work. First is static initialization:

In your above code : you set the following static variables:



so at class load time, before any instance variables, before main is run etc i = 5, j = 7;

In addition you have a static initializer block:



This runs following the static variable initialization but still before main is run or any instance objects are created. So at run time you have:



Then in your loop, you first increment j again (j++). So J becomes 9 which is what is passed to the switch statement, when main is finally run



In your second query you ask about the difference in x++ and System.out.println(x++);

There is no difference in the post increment operator, but when chained with the print statement, the print statement will always print out the value of x before incrementation thus;



If you wish to print the incremented value of x either use a second print statement as shown use one print statement with the pre-increment operator e.g.;



 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:Guys,


At line 1, first the value of j is printed which is 9 and then incremented. My question was shouldn't when this block of code is reached



Shouldn't in the above code snippet even first value of x which is 8 should be used and and at next iteration 9 to be used.
Or there is some difference between these two statements


Because in line 2 first previous value of x is used and then incremented.

I know i am stuck at a very silly confusion but still any pointers in this regard will be helpful.



Post increment does not mean that the value will be incremented after the for loop or switch statement.



The above code will print 7 because when you are printing the value the end of statement is not reached, once the line of execution passes the println statement the value will be incremented to 8.



When it comes to line three the value is already incremented eventhough it is a post increment operator.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks bhanu, that made a lot of sense.
 
It was the best of times. It was the worst of times. It was a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic