• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not clear which is right Please clarify

 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one programme of Whizlab 1.4 which says given code is wrong as it should return primitive (double)
Double d1=Double.parseDouble(s1);

while this code runs fine with Eclipse with jdk1.6 compiler

public class WrapperDemo
{
public static void main(String args[])
{
String s1="1.42";
String s2="2.68";
byte b=7;

Double d1=Double.parseDouble(s1);
Double d1=Double.parseDouble(s2);

System.out.println(b+d1.doubleValue()+d2.doubleValue());
}
}
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using a Whizlabs 1.4 test, but a Java 1.6 compiler? Hmm...OK then.

Did Java 1.4 have autoboxing?
Once you know the answer to that, you should be able to answer the question yourself.

Also, your code will not compile as you have declared the member "d1" twice.
 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thks Jason
I got the answer so i want to clarify one thing (I know this is somewhat silly question but still i think clarifying doubt is important) that
the return type double is Autoboxed to Double in line

Double d1=Double.parseDouble(s1);
and that is the reason i m not getting compile time error in 1.6

and it will not compile if i use 1.4 compiler....
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly Rajesh.

"parseDouble()" returns a "double". The 1.6 compiler immediately autoboxes it to a "Double" object on that line of code. The 1.4 compiler on the other hand, has a nervous breakdown.

You can see what happens using "java -source 1.4 -target 1.4 YourClass.java" from the command line (or change options in your IDE).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic