• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion about Primitive Types  RSS feed

 
Jaikant Singh
Greenhorn
Posts: 16
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi,
I am viewing the source code of java.lang.Class class.
I could not understand the statement
" The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects."
Can anyone elaborate the exact meaning of above line.

Thanks in advance
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikant Singh wrote:
I am viewing the source code of java.lang.Class class.
I could not understand the statement
" The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects."
Can anyone elaborate the exact meaning of above line.



Well, you kinda took that sentence out of context somewhat -- but basically...

Instances of the Class class can be use to represent *all* data types. The complete paragraph list everything. And the last sentence (this one), says that it there are also Class instances for the primitives types too. Kinda makes sense if you think about it; otherwise, how would you use the reflection libraries for components that use primitive data types?

Henry
 
Jaikant Singh
Greenhorn
Posts: 16
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry,

But I know there are Wrapper Classes for converting into Objects then why I need to use Class instances for primitive types apart from using it in reflection library for components.
Do you have an example of that?

JAIKANT

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikant Singh wrote:
But I know there are Wrapper Classes for converting into Objects then why I need to use Class instances for primitive types apart from using it in reflection library for components.
Do you have an example of that?


Do you actually need any other reason? Reflection would be arguably broken without this capability.


Also, this isn't about converting primitive types into objects. The Class class is used to represent types. This is about the ability of the Class class to represent primitive types.

Henry
 
Balaji Vankadaru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a simpler way primitive types are the data types which are provided by java to declare variables of respective types. void is a key word of java which is used in implementing a method which does not intend to return any value.



eg:-

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!