By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,557 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,557 IT Pros & Developers. It's quick & easy.

Are primitives objects?

P: n/a
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.

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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" <Fr**@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.

Jul 17 '05 #2

P: n/a
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" <pa*********@yahoo.com.hk> wrote in message
news:bt**********@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" <Fr**@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.


Jul 17 '05 #3

P: n/a
Fred wrote:
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 ?


All of the wrapper classes have a static final Class variable named TYPE
that holds the "Class instance representing the primitive type." So
you could do:

System.out.println(Integer.TYPE.getName());

which would print:

int

Which brings me to my second pint, it appears you have misread (and
misquoted) the documentation for Class.getName(). The chart you posted
above only applies to array types.

Consider the following:

public class ClassGetName
{
public static void main(String args[])
{
String str = "";
int intArr[] = new int[5];
String strArr[] = new String[5];
System.out.println("Integer.TYPE.getName(): "
+ Integer.TYPE.getName());
System.out.println("str.getClass().getName(): "
+ str.getClass().getName());
System.out.println("intArr.getClass().getName(): "
+ intArr.getClass().getName());
System.out.println("strArr.getClass().getName(): "
+ strArr.getClass().getName());
}
}

The output is:

[~/foo]$ java -cp . ClassGetName
Integer.TYPE.getName(): int
str.getClass().getName(): java.lang.String
intArr.getClass().getName(): [I
strArr.getClass().getName(): [Ljava.lang.String;

Ray

Jul 17 '05 #4

P: n/a

"Fred" <Fr**@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" <pa*********@yahoo.com.hk> wrote in message
news:bt**********@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" <Fr**@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.



Jul 17 '05 #5

P: n/a
Thanks Ray and Igor L.

Reading the doc again, it does say it is only for classes which rules out
primitives, so using something like
int i = 3;
i.getName() isn't supported. (or correct)
"Raymond DeCampo" <rd******@spam-I-am-not.twcny.rr.com> wrote in message
news:pR********************@twister.nyroc.rr.com.. .
Fred wrote:
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 ?


All of the wrapper classes have a static final Class variable named TYPE
that holds the "Class instance representing the primitive type." So
you could do:

System.out.println(Integer.TYPE.getName());

which would print:

int

Which brings me to my second pint, it appears you have misread (and
misquoted) the documentation for Class.getName(). The chart you posted
above only applies to array types.

Consider the following:

public class ClassGetName
{
public static void main(String args[])
{
String str = "";
int intArr[] = new int[5];
String strArr[] = new String[5];
System.out.println("Integer.TYPE.getName(): "
+ Integer.TYPE.getName());
System.out.println("str.getClass().getName(): "
+ str.getClass().getName());
System.out.println("intArr.getClass().getName(): "
+ intArr.getClass().getName());
System.out.println("strArr.getClass().getName(): "
+ strArr.getClass().getName());
}
}

The output is:

[~/foo]$ java -cp . ClassGetName
Integer.TYPE.getName(): int
str.getClass().getName(): java.lang.String
intArr.getClass().getName(): [I
strArr.getClass().getName(): [Ljava.lang.String;

Ray

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.