Re: Are primitives objects?

From: Fred (Fred_at_isp.com)
Date: 01/09/04


Date: Fri, 09 Jan 2004 14:59:34 GMT

Thanks Igor L.

Wrapper classes are indeed handy for the primitives.
The way I got to this question was from looking at the docs for the Class
class,
and the getName method in particular. It says

The primitive Java types (boolean, byte, char, short, int, long, float, and
double), and the keyword void are also represented as Class objects
If I do something like

String str = "Hello";
System.out.println( str.getClass().getName() );

then java.lang.String does indeed print out. But

int i = 3;
System.out.println( i.getClass().getName() );

fails to compile because primitives can't be dereferenced as they aren't
references to objects.

>From the docs for Class, method getName;
getName
public String getName()Returns the name of the entity (class, interface,
array class, primitive type, or void) represented by this Class object, as a
String.
If this class object represents a reference type that is not an array type
then the binary name of the class is returned, as specified by the Java
Language Specification, Second Edition. If this class object represents a
primitive type or void, then the name returned is the name determined by the
following table. The encoding of element type names is as follows:

 B byte
 C char
 D double
 F float
 I int
 J long
 Lclassname; class or interface
 S short
 Z boolean
 V void

Question:
How does one use this with primitives? Or how should System.out.println(
.....getName() );
be structured in order to have it print out an "I" , or "J", or
Ljava.lang.string ?

"Igor L" <patak_dacha@yahoo.com.hk> wrote in message
news:btlnr8$kg4$1@sunce.iskon.hr...
> No, primitives are not objects. Yet, if you need an object (for example to
> put it in a hashtable or something) you can do the following:
>
> int i = 3;
> Integer object_i = new Integer(i);
>
> now you have an object and you can do
>
> System.out.println(object_i.getClass());
>
> Every primitive type has it's own corresponding object type.
> int -> Integer, double -> Double, etc.
>
> See documentation on this classes.
>
>
>
> "Fred" <Fred@isp.com> wrote in message
news:OssLb.3817$na.5853@attbi_s04...
> > Are primitives objects?
> >
> > int i = 3;
> > System.out.println(i.getClass());
> >
> > doesn't compile. Get an error message
> > "int can't be dereferenced"
> >
> > But yet the docs for class Object say:
> > Class Object is the root of the class hierarchy. Every class has Object
> as
> > a superclass.
> >
> >
> >
>
>



Relevant Pages

  • Re: Are primitives objects?
    ... > Wrapper classes are indeed handy for the primitives. ... and the keyword void are also represented as Class objects ... > If this class object represents a reference type that is not an array type ... All of the wrapper classes have a static final Class variable named TYPE ...
    (comp.lang.java)
  • Re: [patch] x86: some lock annotations for user copy paths
    ... the prefetch stuff does get_user inside preempt_disable. ... With the new might_lock primitives, ... const void __user *from, unsigned long n) ...
    (Linux-Kernel)
  • Re: Are primitives objects?
    ... int i = 3; ... >> Wrapper classes are indeed handy for the primitives. ... >> array class, primitive type, or void) represented by this Class object, ...
    (comp.lang.java)
  • Re: Are primitives objects?
    ... > Wrapper classes are indeed handy for the primitives. ... > and the getName method in particular. ... > array class, primitive type, or void) represented by this Class object, as ...
    (comp.lang.java)
  • Re: How to devide Integer value
    ... wrapper classes) objects are immutable. ... unbox and box primitives into their respective wrappers. ... and let Java worry about the rest. ... Alternately, you could use int ...
    (comp.lang.java.programmer)