• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

obj instanceof (String [] )

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !
I wish to write the function where argument
can be String or String [] ( like C printf ;-) )
void function(Object obj){
if( obj instanceof String ) // OK
.....
else if( obj instanceof (String []) ) // Syntax
.... // error
else throw new IllegalArgumentException();
---------------------------------------------
What's wrong here ?
I understand that exists some workaround like
if( obj.getClass() == .... )
But it don't seem elegant enough.

Thank you.
 
Igor Ko
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems a little strange syntax here, because
String str = (String) obj;
String [] str = (String [])obj;
Works well.
----------------------------------------------
if( obj instanceof String )
... // OK
if( obj instanceof (String [] ) // Wrong syntax
...
Is Java compiler a little strange ???
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What compiler error do you get?
BTW, you should think about simply overriding the method:

or
 
Igor Ko
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I found my error:
-----------------------------------------------
void function(Object obj){
if( obj instanceof String )
...
else if( obj instanceof (String[]) )
... ^
Syntax error - illegal start of type
-----------------------------------------------
After error fix:
else if( obj instanceof String[] )
it passed compilation.
----------------------------------------------
And you are right about overriding - it's more
simple solution, without any dynamic type checking
Thank you !
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to clarify, the concept you are suggesting is overloading, not overriding.
Overloading is providing different signatures for the same method name.
Overriding is writing a new method with the same signature and name as one in a superclass.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lance Finney:
Just to clarify, the concept you are suggesting is overloading, not overriding.


Ooops...
Thanks for the correction!
 
Igor Ko
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overloading, not overriding. Thanks.
 
Sheriff
Posts: 9103
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Igor K
"K" is not a valid last name. Please modify your displayed name by clicking on the "my profile" link at the top of the page.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic