This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Refactoring for Software Design Smells: Managing Technical Debt this week in the OO, Patterns, UML and Refactoring forum!

# Dont�t understand the result

Stephan Deve
Greenhorn

Joined: Sep 21, 2004
Posts: 13
Hello,
i have a problem with the following question (from javacertivicate.com):

Meanwhile i know that the answer is 31. So i can choose the right answer everytime i see the question, but i still don�t understand what is happening.
After x += x << 3; x is 54
After y += z << 2; y should be 25
If i add 54 and 25 i think this should be 79. So why is 31 correct ??

greetings
Stephan
Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105
Hi,

Carefully look at test method.

SCJP 1.4
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
a bit tricky

do so :-- public void test(int d(or anyother identifier except x), int z)

do:-- public void test(int x, int z)
you get x= 6, z = 25
Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105
Hi,

Sorry for incomplete previous post.

Look at

Within the test method parameter variable x is redeclared, So only the local variable x is modified and not static variable x defined in class Pass.

But variable y is not declared within test and y += z << 2, modified y to 25.

So in the System.out.println(x+y) is 6 + 25 = 31 (x has the value assigned to 6 in main() before calling test())

The variable x in test is said to shadowed the same variable declared in class.

- Rajasekar
Celso Serrano Araujo
Greenhorn

Joined: Dec 10, 2004
Posts: 3
Note que x += x << 3; is assingment 54 in the automatic variable x and not variable member static x
Stephan Deve
Greenhorn

Joined: Sep 21, 2004
Posts: 13
I got it now.
Thanks a lot.

Changing

to

helped a lot to make clear how this code works.

greetings
Stephan

I agree. Here's the link: http://aspose.com/file-tools

subject: Dont�t understand the result