File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 24166

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: 3165
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: 13868

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 7 API documentation
Scala Notes - My blog about Scala
I agree. Here's the link:
subject: what syntax for getting instanceof an array?
Similar Threads
Multidim. array garbage collection
array doubt
Two dimensional Vector
java.lang.ArrayIndexOutOfBoundsException - null
Array reflection in Java