Arrays
are objects in java. Everything you create using new operator is an object in
java. .
What is the class of
the array?
Every
array type has corresponding classes with it.
Below
table summarizes the arrays and the class associated with them.
Primitive Array
|
Class
|
boolean[]
|
class
[Z
|
byte[]
|
class
[B
|
short[]
|
class
[S
|
int[]
|
class
[I
|
long[]
|
class
[J
|
float[]
|
class
[F
|
double[]
|
class
[D
|
char[]
|
class
[C
|
Object[]
|
class
[Ljava.lang.Object;
|
Employee[]
|
class
[Lcom.sample.test.Test$Employee;
|
Test.java
package com.sample.test; public class Test { private static class Employee{ } public static void main(String args[]) { System.out.println("Array Type\tClass Name"); System.out.println("boolean[]\t" + new boolean[0].getClass()); System.out.println("byte[]\t" + new byte[0].getClass()); System.out.println("short[]\t" + new short[0].getClass()); System.out.println("int[]\t" + new int[0].getClass()); System.out.println("long[]\t" + new long[0].getClass()); System.out.println("float[]\t" + new float[0].getClass()); System.out.println("double[]\t" + new double[0].getClass()); System.out.println("char[]\t" + new char[0].getClass()); System.out.println("Object[]\t" + new Object[0].getClass()); System.out.println("Employee[]\t" + new Employee[0].getClass()); } }
Output
Array Type Class Name boolean[] class [Z byte[] class [B short[] class [S int[] class [I long[] class [J float[] class [F double[] class [D char[] class [C Object[] class [Ljava.lang.Object; Employee[] class [Lcom.sample.test.Test$Employee;
What is class [Z in
above output?
[Z is the class name here. Single [ represents one dimensional array. [[ represent two-dimensional array.
[Z is the class name here. Single [ represents one dimensional array. [[ represent two-dimensional array.
For
example, below program display the classes of two-dimensional array.
Test.java
package com.sample.test; public class Test { private static class Employee { } public static void main(String args[]) { System.out.println("Array Type\tClass Name"); System.out.println("boolean[][]\t" + new boolean[0][0].getClass()); System.out.println("byte[][]\t" + new byte[0][0].getClass()); System.out.println("short[][]\t" + new short[0][0].getClass()); System.out.println("int[][]\t" + new int[0][0].getClass()); System.out.println("long[][]\t" + new long[0][0].getClass()); System.out.println("float[][]\t" + new float[0][0].getClass()); System.out.println("double[][]\t" + new double[0][0].getClass()); System.out.println("char[][]\t" + new char[0][0].getClass()); System.out.println("Object[][]\t" + new Object[0][0].getClass()); System.out.println("Employee[][]\t" + new Employee[0][0].getClass()); } }
Output
Array Type Class Name boolean[][] class [[Z byte[][] class [[B short[][] class [[S int[][] class [[I long[][] class [[J float[][] class [[F double[][] class [[D char[][] class [[C Object[][] class [[Ljava.lang.Object; Employee[][] class [[Lcom.sample.test.Test$Employee;
You may like
Can a private constructor prevent sub-classing of class?
How to bypass checked exception while creating object?
How to bypass checked exception while creating object?
No comments:
Post a Comment