Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Determine type of a primitive

 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to determine the type of a primitive?
 
Vishy Karl
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think No there is method for objects but I don't think there is for primitive.
Others do correct if you feel I am wrong,
Bye
Vishal.
 
Ed Tse
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you post the scenario? when do u need that?
 
Danijel Kucak
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishal Kalra:
I think No there is method for objects but I don't think there is for primitive.
Others do correct if you feel I am wrong,
Bye
Vishal.

i think there is no method for objects, it's operator instanceof
Bye,
Danijel
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Test {
public static void main(String[] args) {
int i = 0 ;
System.out.println(/** print type of i **/)
}
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How's this?
[ September 19, 2003: Message edited by: Steve Lovelace ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve. That is great. But I was expecting some like this in API. By the way what is by byte.class. Never seen that before.
Thanks
Barkat
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt very much there is one.
Primitive types are not objects. You cannot do introspection on them.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about if Steve's code is part of AP1 so that every client does not have write it?
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Appears the VM builds for itself a Class object for each of the primitive types. The syntax for accessing these is <type>.class, so, byte.class, int.class, etc. Alton's right that introspection (reflection?) won't get you anywhere: Somebody know offhand what introspection is, is it different from reflection?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reflection is the ability of a program to use an object whose class file was not present at the time of its compilation. The program cannot name the real type of such object, it cannot name its methods or fields because the compiler would complain. Instead the program uses java.lang.reflect
I think introspection is either a synonym, or a process that uses reflection discovering JavaBeans members for a Gui Builder.
JavaBean specification
[ September 21, 2003: Message edited by: Jose Botella ]
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects that are introspective(or reflective) has the ability to describe itself. Programming languages that support introspection allows objects to be examined, like, what class it came from, what are its fields and methods, etc.
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a note about Typist. It really isn't anything more than a kinda cute demo of overloading and the existence of Class objects for primitives. Fact is, you can always see the declaration of a primitive variable - it's somewhere in your code, and whatever its declared to be, that's what it is. So Typist has no real purpose in life.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic