开发者

In Java, is a char[] an object?

开发者 https://www.devze.com 2023-01-24 15:56 出处:网络
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]开发者_JAVA技巧 and temp2.hashCode() wi

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]开发者_JAVA技巧 and temp2.hashCode() will compile and execute.

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


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)


Yes. All arrays are objects, even arrays of primitive types.


Yes, all arrays are Objects in Java.


Yes, every Array of every type is an object.


Yes arrays are objects in java.


An array isn't just several primitive types, it also has a "length" field. Primitive types don't have fields. Another thing that sets arrays appart from primitive types is they are references and thus have to be garbage collected.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号