Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, is a char[] an object?

I'm new to Java but if I understand correctly, a char is a primitive.

Doing char temp and temp.hashCode() won't compile but doing a char[] temp2 = new char[2] and temp2.hashCode() will compile and execute.

Does this mean somehow a char[] is an object???

like image 605
TSplk Avatar asked Nov 29 '22 19:11

TSplk


1 Answers

a char is a primitive, but an array of type char is an object

one way to tell is by dynamically instantiating it:

final Object charArray = Array.newInstance(Character.TYPE, 5);
System.out.println(charArray.getClass().getComponentType());

Output:

char

(Character.TYPE is a reference to the primitive class char. Another way to access that class is through char.class)

like image 185
Sean Patrick Floyd Avatar answered Dec 06 '22 11:12

Sean Patrick Floyd