Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting the type of variable instance

 
Matt Kaye
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65220
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Vorwald
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch , Matt Kaye
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic