Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

Why is Double different from new Double()

Thread view: 
Bob Sparks - 31 Dec 2004 21:18 GMT
Here is the conundrum

I get this error message

   [javac] tpsNumberFormat.java:232: cannot resolve symbol
   [javac] symbol  : constructor Measure  (CustomUnit,double)
   [javac] location: class Measure
   [javac]                     Measure measure = new Measure(unit,  dNum );

Yet this line complies

Measure measure = new Measure(unit, new Double(dNum));

The actual constructor is defined as

public Measure(CustomUnit unit, java.lang.Number number)

Now Number is an abstract class so I can't actually construct one.

If I try to cast I get

   [javac] tpsNumberFormat.java:232: inconvertible types
   [javac] found   : double
   [javac] required: java.lang.Number
   [javac]     Measure measure = new Measure(unit, (Number) dNum );

Strange since a Double is in the list of "Direct Known Subclasses:" of Number.

The version is embarassingly 1.3.1_07

Thanks to those who expressed interest.

Bob
A. Bolmarcich - 31 Dec 2004 22:48 GMT
> Here is the conundrum
>
[quoted text clipped - 4 lines]
>     [javac] location: class Measure
>     [javac]                     Measure measure = new Measure(unit,  dNum );

The error message indicate that the variable named dNum has type double.

> Yet this line complies
>
> Measure measure = new Measure(unit, new Double(dNum));

The expression new Double(dNum) is of type Double, which is not the same as
type double.

> The actual constructor is defined as
>
> public Measure(CustomUnit unit, java.lang.Number number)

Because Number is an ancestor class of Double line that you previous gave
using this constructor.

> Now Number is an abstract class so I can't actually construct one.
>
[quoted text clipped - 6 lines]
>
> Strange since a Double is in the list of "Direct Known Subclasses:" of Number.

The error message inidcates that the variable named dNum has type double,
which is not the same as type Double.
Tony Morris - 31 Dec 2004 22:49 GMT
> Here is the conundrum
>
[quoted text clipped - 4 lines]
>     [javac] location: class Measure
>     [javac]                     Measure measure = new Measure(unit,
dNum );

> Yet this line complies
>
[quoted text clipped - 20 lines]
>
> Bob

double is a primitive type that is not assignable to or from a
java.lang.Double, which is a reference type (this statement applies to
compiler version < 1.5)
http://java.sun.com/tutorial/java/nutsandbolts/datatypes.html

Signature

Tony Morris
http://xdweb.net/~dibblego/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.