• 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

Getting the type of variable instance

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79261
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch , Matt Kaye
reply
    Bookmark Topic Watch Topic
  • New Topic