This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes obj instanceof (String [] ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "obj instanceof (String [] ) " Watch "obj instanceof (String [] ) " New topic
Author

obj instanceof (String [] )

Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
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

Joined: Jun 24, 2002
Posts: 90
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

Joined: Jul 11, 2001
Posts: 14112
What compiler error do you get?
BTW, you should think about simply overriding the method:

or


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
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

Joined: Apr 26, 2001
Posts: 133
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jun 24, 2002
Posts: 90
Overloading, not overriding. Thanks.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: obj instanceof (String [] )
 
Similar Threads
Getting size of an array thru EL
Incorrect equals() implementation...
instanceof operator
deserialization and casting
data type in java