• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reflection problem

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic