This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

obj instanceof (String [] )

 
Igor Ko
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 ???
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 !
 
Lance Finney
Ranch Hand
Posts: 133
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overloading, not overriding. Thanks.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic