| Author |
Primitive vs Wrapper
|
A Kumar
Ranch Hand
Joined: Jul 04, 2004
Posts: 973
|
|
Hi all, int is a primitive class and Integer is its wrapper class... similiarly for double .... Now Consider this..... The output is..: The val are double The val are double The val are class java.lang.Double now double here is primitive and the Double is wrapper..but they both are assigned to the "Class" variables.....How is that...??? Is that both double and Double(itz already a class) are classes... Double is in java.lang package... what abt double? Tx  [ August 12, 2005: Message edited by: A Kumar ]
|
 |
Stephen Huey
Ranch Hand
Joined: Jul 15, 2003
Posts: 618
|
|
|
I'm not sure about what you're trying to ask. double's class is double, and Double's class is Double. But the TYPE member/variable of the class Double is of type double (the primitive one). Another way to think about it is this: TYPE is just a static member, meaning it's a variable belonging to the class Double. It could be called BILLYBOB or NOTHING or MYVARIABLE. That variable is being used to store a Class object representing the primitive double (the same thing you get when you call double.class and get a Class object back from that).
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
|
What I think the OP was referring to (it was certainly new to me) was that double.class appears to be dereferencing a primitive type.
|
Joanne
|
 |
Steve Morrow
Ranch Hand
Joined: May 22, 2003
Posts: 657
|
|
|
Class literal.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
|
Thanks. i really must study the JLS, rather than just dipping in and out when I need to know something specific.
|
 |
Philip Heller
author
Ranch Hand
Joined: Oct 24, 2000
Posts: 119
|
|
Joanne said, "... double.class appears to be dereferencing a primitive type." Absolutely right! The issue here is reflection, an extremely nifty feature of Java that is used extensively by the engines that run EnterpriseJavaBeans and the rest of e-commerce. The mechanism requires a way to programatically represent the argument list of a method. This is done with an array of instances of the Class class. So given these methods: The arg list of m1 is represented by an array containing Double.class, String.class, and Thread.class. But how do you represent that first arg of m2? The inventors of Java created a class that represents the double primitive type (and also one for int, byt, and the other primitives). You and I can't do much with these classes. In fact, if you're not programming with reflection they aren't any use at all. They don't have any methods or data. We refer to them as "double.class", "byte.class", etc. Which certainly looks like dereferencing a type name as if it were an object reference. Here's an application that uses reflection to peek inside the Double and Double classes. Copy, paste, enjoy! -- Phil
|
Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
Originally posted by A Kumar: Hi all, int is a primitive class and Integer is its wrapper class... similiarly for double .... Now Consider this..... The output is..: The val are double The val are double The val are class java.lang.Double now double here is primitive and the Double is wrapper..but they both are assigned to the "Class" variables.....How is that...??? Is that both double and Double(itz already a class) are classes... Double is in java.lang package... what abt double? Tx [ August 12, 2005: Message edited by: A Kumar ]
double itself is NOT a class; it's a primitive. However, as mentioned above, there is an instance of the Class class that represents the primitive type for use by any frameworks that use reflection. I think the confusion here is a misunderstanding of the difference between a Class object and the type of a variable. If you can understand this, then you should be okay. Layne
|
Java API Documentation
The Java Tutorial
|
 |
Rick O'Shay
Ranch Hand
Joined: Sep 19, 2004
Posts: 531
|
|
|
All the primitive types are associated with a class to support serialization.
|
 |
 |
|
|
subject: Primitive vs Wrapper
|
|
|