Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "length" in the char[]?

Tags:

java

char[] name = "VIKKYHACKS".toCharArray();
System.out.println(name.length);

In this program what is the "length" , If it were (new String("VIKKYHACKS")).length() then the length would be a method. But char[] is a datatype and cannot have fields or methods inside it. How does the second line of that program work ???

like image 892
vikkyhacks Avatar asked Dec 06 '22 08:12

vikkyhacks


1 Answers

char[] is not a primitive data type. it is an Object, and it has a public field 'length'.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

That is a good start.

Because Arrays are Objects, they have all the other items, like an equals() and hashCode() method too. (as well as all the treats like notify(), wait(), etc.)

like image 194
rolfl Avatar answered Dec 24 '22 00:12

rolfl