• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reflection problem

 
Tal Goldstein
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
supose i have a String: "int".
i would like to get the class of what inside the string.
i can't use Class.forName() in this case because the string contains a type which is a primitive (int).
and of course if i'll do "int".getClass() i will get the class of java.lang.String which is not what i want.

any ideas?

thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34669
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tal,
"int" is not a class; it is a primitive. This means it is built in and there isn't a class that defines it. There is a logical Object equivalent - Integer.

If you want this, you could write a method to see if the String contains a primitive and map to the appropriate value yourself. There aren't too many of these so you could list them out in the code (or better yet in a Map.) If the type you are looking for is not in the list, you could then proceed to use Class.forName.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, there is a Class for primitive "int". It is "Integer.TYPE". This is not the same as "Integer.class", which is the Class for Integer.
 
Tal Goldstein
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:
Tal,
"int" is not a class; it is a primitive. This means it is built in and there isn't a class that defines it. There is a logical Object equivalent - Integer.

If you want this, you could write a method to see if the String contains a primitive and map to the appropriate value yourself. There aren't too many of these so you could list them out in the code (or better yet in a Map.) If the type you are looking for is not in the list, you could then proceed to use Class.forName.


as someone already said, although int is a primitive type, there's still a class representing it. try: int.class
an you'll get that class.
Peter - thanks for the Integer.TYPE soulution
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer.TYPE can also be referenced as int.class. These two mean the same thing, a Class instance referring to the primitive type int. As opposed to Integer.class, which refers to the wrapper Integer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic