aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Book for Scjp 6 - Chapter 5 question 8 doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Book for Scjp 6 - Chapter 5 question 8 doubt" Watch "K&B Book for Scjp 6 - Chapter 5 question 8 doubt" New topic
Author

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

Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
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,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Srinivas Nagesh
Greenhorn

Joined: Oct 27, 2002
Posts: 19
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?
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
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.


SCJP 6
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

@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


SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
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)


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
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

Joined: Jul 23, 2008
Posts: 352
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.;



bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
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

Joined: Oct 13, 2009
Posts: 774
Thanks bhanu, that made a lot of sense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Book for Scjp 6 - Chapter 5 question 8 doubt