This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes what syntax for getting instanceof an array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what syntax for getting instanceof an array?" Watch "what syntax for getting instanceof an array?" New topic

what syntax for getting instanceof an array?

Chris Jones

Joined: Feb 18, 2008
Posts: 12
I want to write a conditional expression that checks if an object is an array. Can I use instanceof for this? How is the syntax? I imagine something along these lines:

// code fragment:

but eh ... that's evidently not how things are, so what do I instead need to do to determine type of an array object?

all help much appreciated!
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

Actually, your code snippet is fine (given that it appears inside a method, of course.) That's exactly the right thing to do. Maybe whatever you actually tried was a little bit different from this?

[Jess in Action][AskingGoodQuestions]
qingwu wang
Ranch Hand

Joined: Feb 19, 2003
Posts: 147
I don't think it is a good way to solve your question. only as a referrence.
String str = someArray.getClass().getName();
System.out.println("This is an array");

Thanks...qingwu<br />When I open my eyes,I see your pretty face.
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
Originally posted by Chris Jones:

If you're not worried about what type of array it is you could do

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15045

You can try something different, as qingwu and Joanne show, but still: the syntax you are trying to use is correct and should work. Have a good look at your source code again. Isn't there something else you overlooked? Maybe a missing brace or semi-colon?

Try copying and pasting this example, and compiling it: it works without errors.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: what syntax for getting instanceof an array?
It's not a secret anymore!