• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Doubt with ++operator on Wrapper objects

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Taken from Sun e-Practice for JAVA 5 SCJP Mock Exam :



Which is true?

A. The output is 344.
B. Compilation fails due to an error at line 5.
C. Compilation fails due to an error at line 11.
D. At line 8, an object is eligible for garbage collection





Answer : D (Because the object lifeline of tale only exist within the if statment)

What's surprising me is that why wasnt the output 344?

1. A value of 343 was passed into the go() method.
2. t++; un-wraps the Long object, increments, and returns 344?
3. The value of 344 is assigned to variable story.
4. System.out.print value out, but the value was actually 343.

Why is that so? I always believed that incrementing ++ wrapper objects adds one to the value.

Please enlighten me !
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just To Explain things I have modified the code as :

public class Story {
static long story;
public static void main(String [] args) {
if(story==0) {
Long tale = 343L;
story = go(tale);
}
// do stuff9.
System.out.print(story);
}
static long go(Long t) {
t++;
System.out.println(t);
return t;
}

}

Now you will get O/P as
344
344


The Reason the Postfix ++ for t is used for the second time and increments the value of t however in your code t never got incremented
 
Abhishek khare
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also try this :

public class Story {
static long story;
public static void main(String [] args) {
if(story==0) {
Long tale = 343L;
story = go(tale);
}
// do stuff9.
System.out.print(story);
}
static long go(Long t) {
return ++t;
}

}

The O/P now is 344
 
Brandon Bay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh.. does it mean if i do

t++;

t would get incremented after it is returned?

because ++t; incremented it.

Am I correct?
 
Abhishek khare
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Becuase t++ would be incremented only when it is used again and since it is the last statement in the menthod it will never be used again and hence will not be incremented.
 
Brandon Bay
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh wow. Didnt know that. Haha, thanks a million
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


No Becuase t++ would be incremented only when it is used again and since it is the last statement in the menthod it will never be used again and hence will not be incremented.



I am affraid that this affirmation is incorrect. The variable is actually incremented inmediatelly. It is just that the return statement does not use that incremented value, but the original value of the variable.

I can explain it with a couple of examples and by means of showing you the underlying bytecodes.

If your code were like this:



The bytecodes would look like this:



If we invoked the function getTale(10L) let's see what would be the state of stack AFTER every statement:



As you can see, the variable x is actually incremented (line #4) by the time the result statement is executed (line #5). It is just that the return uses a remaining value of the variable in the stack (frame #0).

Therefore your affirmation that the variable is incremented until it is used again is incorrect.
[ September 14, 2007: Message edited by: Edwin Dalorzo ]
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic