aspose file tools*
The moose likes Beginning Java and the fly likes Getting the type of variable instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting the type of variable instance" Watch "Getting the type of variable instance" New topic
Author

Getting the type of variable instance

Matt Kaye
Greenhorn

Joined: Sep 26, 2010
Posts: 2
First post on JavaRanch, I'm sure it won't be the last. This one is quick and easy:



What I expect to happen when i run this code is that the 2nd if statement is false, so it should just be skipped. But that's not what happens. I get this runtime error:



Why?

Regards,
Matt
John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
I believe this shows as a compile error because failing the relation check is trivial.

If a cast of the RelationalExpression to the ReferenceType would be rejected as a compile-time error, then the instanceof relational expression likewise produces a compile-time error. In such a situation, the result of the instanceof expression could never be true.


http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#80289
Matt Kaye
Greenhorn

Joined: Sep 26, 2010
Posts: 2
Ok, think i get it. I found this explanation on the Oracle site that was much easier to understand:

http://download.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.


So i'm not dealing with an object. "s" is just an instance variable. But then how does one test for the type of primitive they're dealing with?
How can I tell the difference between String x = "2" and int x = 2? They look identical on the command line.

Regards,
Matt
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

As you know which type it is declared as when you wrote the code, why would you need to test this at run-time? It's pretty much a non-sensical scenario. What's the motivation behind the question?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Matt Kaye wrote:How can I tell the difference between String x = "2" and int x = 2? They look identical on the command line.

Ah, I think I see what you're getting at. All command line parameters to programs are passed as Strings -- there's nothing special about the way java.exe / javaw.exe treat the parameters they receive from the OS.

In case of a Java application, the command line parameters are available in the String[] array (or String... varargs, but you probably haven't covered that yet) that is the only parameter to the public void main method.


luck, db
There are no new questions, but there may be new answers.
John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
I haven't thought about instanceof that much, I use it in equality checks.
Here's a code that illustrates the uses of instanceof.



The output is



Hopefully this example illustrates the good and bad of instanceof.
As you pointed out, the instanceof doesn't compile when string / integer are mixed.
And, we can't write class A extends Integer to test A instanceof Integer because class Integer was declared final.

Also, you may want to look at pages on this site: JavaExamples
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36590
    
  16
And welcome to JavaRanch , Matt Kaye
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting the type of variable instance
 
Similar Threads
a program on wrapper classes
scope of local variables
Var args ... explanation needed
compile error
instanceof operator