Re: Are primitives objects?

From: Igor L (patak_dacha_at_yahoo.com.hk)
Date: 01/09/04


Date: Fri, 9 Jan 2004 16:39:47 +0100


"Fred" <Fred@isp.com> wrote in message
news:qlzLb.6384$5V2.10893@attbi_s53...
> 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 ?

does it have to be getName() method? create a new method (static?) in some
helper class which would call getName method, than look in some table and
return B,C,D,F....
it seems a reasonable solution to me, maybe it isn't, depends on the
structure of your code.

>
> "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: 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?
    ... No, primitives are not objects. ... Every primitive type has it's own corresponding object type. ... int -> Integer, double -> Double, etc. ... > Class Object is the root of the class hierarchy. ...
    (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?
    ... Wrapper classes are indeed handy for the primitives. ... and the keyword void are also represented as Class objects ... Language Specification, Second Edition. ...
    (comp.lang.java)