• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to test for type  RSS feed

 
Logan Knecht
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering how to test for the type of a variable. for instance say you have variable x, and you want to catch something before you perform a step that needs a specific type, such as int, or float. So rather than it failing when hitting that step it tests to make sure a string isn't there instead of an int.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are using the variable, then you yourself know the type.
If someone else is using your class and if he uses incorrect type then you can throw the IllegalArgumentException.
 
Logan Knecht
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but that still doesn't answer if there is a way to compare types to see if it is a certain type so I can execute something. I just want to know if there is a way to check the type through a command, or compare types.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, if somebody else uses an incorrect type they will either get a compiler error or a ClassCastException.

You ought to declare your objects of such a type that you don't have to check which runtime type they are. For example, for most uses of List ("List" here means the java.util.List interface) the methods in the List interface are used, so you declare your Lists of type List and you declare the parameters to your methods and their return types of type List.
There are however a few methods in some of the classes which aren't in List. For example there is an ensureCapacity(int) method in some implementations.

I don't like the use of the instanceof operator, but this is how it can be used, which will probably explain your query.A very awkward and clunky bit of programming, but it shows the use of instanceof.

[edit]Sorry, I forgot to include the class cast. And I've got to get rid of smilies.[/edit]
[ July 23, 2008: Message edited by: Campbell Ritchie ]
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Campbell I think the original poster means primitive types and not object types, that's why I did not come up with the instanceof.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Logan, can you give a code example where you would need to do that?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit Ghorpade:
@ Campbell I think the original poster means primitive types . . .
Thank you.

In which case I don't think it's possible. The compiler does all primitive typing. It is probably possible to do something analogous to getClass().getName() even on a primitive, but I can't remember what it is.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome
I think that either the requirement needs rework or one can think of Wrapper classes if its really needed.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if the idea was to prevent use of incorrect types, the compiler will check all primitives anyway.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if the idea was to prevent use of incorrect types, the compiler will check all primitives anyway.


But the poster wants to test the type on his own, and I dont think its possible without the compiler's help.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure the poster is either so new to Java that he hasn't tried to compile any code, or is actually talking about Javascript.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, only java.lang.reflect.Field can be used to determine the exact type for all fields, even primitives. The getType() method returns a class that will turn out to be int.class (also available as Integer.TYPE) for int etc.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!