This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please Help: bizarre output

 
Dibbo Khan
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I have no idea why the output is 343 instead of 344, this is question 65 of the epractice exams for J2SE 5.0, could someone please help explain why this is the case to me, I think all the autoboxing syntax is correct??? Thank you to everyone for your anticipated help.

public class X {


static long story;

public static void main(String[] args) {
if(story == 0)
{
Long tale = 343L;
story = go(tale);
}

System.out.print(story);
}

static long go(Long t)
{
return t++;
}

}
 
nagaraj raja
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai,
here u used post increment ie return i++ means first it will perform operation then i will print the increment value here if u use instead of i++ use ++i then it will increment by 1 and print ok
 
K Krishna Kishore
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Line 8 calls go method supplying the value of 343 to variable "t".

Line 15 returns the value of t to the main method. Now since the post increment operator is used it first returns the value and the incremets the value of t. So variable story in the main method gets the value of 343 and then varibale t becomes 344 but dies soon after due to its scope. If you want the output to be 344 you need to use the pre increment opetator (++t) which would first increment the value of the variable "t" to 344 and then return it.

Hope this helps.
 
Dibbo Khan
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both very much for your help
 
Mahesh Kumar Kumar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It Doesn't Compile
 
Dibbo Khan
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does compile, just make sure your class name and file name are the same.

public class X {

static long story;

public static void main(String[] args) {
if(story == 0)
{
Long tale = 343L;
story = go(tale);
}

System.out.print(story);
}

static long go(Long t)
{
return t++;
}

}

This was compiled using Netbeans 4.1
 
Kayalvizhi Umashankar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will compile if "Long" is changed to "long"

Kayal
 
Dharmesh Gangani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, It will compile only if "Long" is changed to "long".
 
Dibbo Khan
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is for SCJP 5.0,

Long is valid because of autoboxing, I used Netbeans 4.1 which uses JDK 5
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic