Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the data type for length property for Java arrays?

Tags:

java

types

limits

I want to find out if length property for Java arrays is an int/long or something else.

like image 670
cheeming Avatar asked Oct 17 '08 07:10

cheeming


People also ask

What type is array length in Java?

The theoretical maximum Java array size is 2,147,483,647 elements. To find the size of a Java array, query an array's length property. The Java array size is set permanently when the array is initialized. The size or length count of an array in Java includes both null and non-null characters.

What is data type of array length?

By definition, the length property of an array is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array. The value of the length is 232. It means that an array can hold up to 4294967296 (232) elements.

How do you call the length of an array in Java?

To find the length of an array, use array data member 'length'. 'length' gives the number of elements allocated, not the number inserted. Write a class with a main method that creates an array of 10 integers and totals them up.

Is length a property in Java?

Answer: The 'length' property is a part of the array and returns the size of the array. The method length() is a method for the string objects that return the number of characters in the string. Q #3) What is the length function in Java?


2 Answers

It is an int. See the Java Language Specification, section 10.7.

like image 92
Greg Hewgill Avatar answered Oct 03 '22 22:10

Greg Hewgill


In Java Language spec, Arrays you can see in 10.4:

Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion and become int values. An attempt to access an array component with a long index value results in a compile-time error.

I could not find the type of the length attribute, but it is at least an int; and if it's a long then you can not access elements beyond the max integer length.

So I guess it's a (final) int.

like image 38
extraneon Avatar answered Oct 04 '22 00:10

extraneon