GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Not clear which is right Please clarify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Not clear which is right Please clarify" Watch "Not clear which is right Please clarify" New topic
Author

Not clear which is right Please clarify

Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
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

Joined: Jun 09, 2009
Posts: 327
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.


SCJP6
Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
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

Joined: Jun 09, 2009
Posts: 327
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).
 
GeeCON Prague 2014
 
subject: Not clear which is right Please clarify