wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What King of Class is a Array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What King of Class is a Array?" Watch "What King of Class is a Array?" New topic
Author

What King of Class is a Array?

Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

Hi everyone,

i want to know something...

What King of Class is a Array? I mean a variable with the [].

I clarify my question with the next example:



But if I want to test this array (a) exactly with the kind of Class of this kind (Array)?

Somebody know?

Thankyou.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You can test

For a primitive type array, that's really the only thing that would apply. For a reference type array, you could also test arrays of supertypes, e.g.


"I'm not back." - Bill Harding, Twister
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

There are two ways.

One is to test if an object is an array of any type:

The second one can be used if you want to test for a specific type of array:

This works because all arrays are objects themselves, and so they can be used wherever objects can be used.
Similarly, just as you can use Object.class or String.class to get the Class object for "String", you can use int[].class to get the Class object for "array of int", and even complex stuff like String[][][].class.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

Thank you for the fast answer.

Thank you Jim Yingst and Rob Prime
 
Don't get me started about those stupid light bulbs.
 
subject: What King of Class is a Array?