Re: Are primitives objects?
From: Fred (Fred_at_isp.com)
Date: 01/09/04
- Next message: nos: "Re: Clear the Screen"
- Previous message: Risto Lankinen: "Re: Order of evaluation and better declaration of functions"
- In reply to: Igor L: "Re: Are primitives objects?"
- Next in thread: Raymond DeCampo: "Re: Are primitives objects?"
- Reply: Raymond DeCampo: "Re: Are primitives objects?"
- Reply: Igor L: "Re: Are primitives objects?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
> >
> >
> >
>
>
- Next message: nos: "Re: Clear the Screen"
- Previous message: Risto Lankinen: "Re: Order of evaluation and better declaration of functions"
- In reply to: Igor L: "Re: Are primitives objects?"
- Next in thread: Raymond DeCampo: "Re: Are primitives objects?"
- Reply: Raymond DeCampo: "Re: Are primitives objects?"
- Reply: Igor L: "Re: Are primitives objects?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|