What is the "length" in char []?

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

In this program, "length", if it were (new String("VIKKYHACKS")).length() , then length would be a method. But char [] is a data type and cannot contain fields or methods in it. How does the second line of this program work?

+4
source share
7 answers

char [] is not a primitive data type. it is an object, and it has the length of an open field.

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

This is a good start.

Since arrays are objects, they have all the other elements, for example, the equals () method and hashCode (). (as well as all calls, such as notify (), wait (), etc.)

+6
source

Arrays are objects in Java. According to JLS, section 10.3 , length is a " final instance variable" that gives the length of the array.

+2
source

An array is a container object that contains a fixed number of values โ€‹โ€‹of the same type. The length of the array is set when the array is created. After creation, its length is fixed. You can use the built-in property length to determine the size of any array. See Also: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

+2
source

First you have the line "VIKKYHACKS". Then you turn it into an array with the following

 char[] name = "VIKKYHACKS".toCharArray(); 

"char [] name =" part, assigns a variable name to our char array. What has a char array (char [])

Arrays have a variable named length referenced using .length. Used in the second line.

 name.length 
+2
source

each array has an instance of variable length containing the size of the array (talked about java :))

+1
source

length is the public final field of the Array class. Its value is initialized after the array is created.

+1
source

Since the name is an array of characters, and arrays have a property called length, which will select the length of the array. In the case of a string, length () is a method that gets the length of the string.

+1
source

All Articles