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

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

Asked by: Agata676 | Posted: 23-01-2022

Answer 1

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

Answered by: Dominik108 | Posted: 24-02-2022

Answer 2

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.

Answered by: Kimberly174 | Posted: 24-02-2022

Answer 3

According to the specification, it's an int

Answered by: Byron753 | Posted: 24-02-2022

Answer 4

The data type is int, not long. Same as the index.

See http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html, 10.4

Answered by: Miller708 | Posted: 24-02-2022

Answer 5

In JavaCard array indexes are shorts, but JavaCard is odd like that. Everywhere else, int like everyone else says.

Answered by: Dainton542 | Posted: 24-02-2022

Similar questions

array length property in java

please don't mind this is just a simple question on array length property. As a beginner in Java I came across Constants and final keyword, which is described as: Constants are non-modifiable variables, declared with keyword final. Their values cannot be changed during program execution. Also, constants must be initialized during declaration. For examples: final double PI = 3.1415926; //...

java - How do I inject a single property value into a string using spring 2.5.x?

I would really like to annotate a method with a reference to a single property in a property file for injection. @Resource("${my.service.url}") private String myServiceUrl; Of course, this syntax does not work ;) Thats why I'm asking here. I am aware that I can inject the full properties file, but that just seems excessive, I dont want the property file - I want the configured valu...

java - JSP bean tag for property that might not exist

In JSP I can reference a bean's property by using the tag ${object.property} Is there some way to deal with properties that might not exist? I have a JSP page that needs to deal with different types. Example: public class Person { public String getName() } public class Employee extends Person { public float getSalary() } In JSP I want to display a table of people with colum...

"Holds value of property" in Java code

I got some legacy code with that caption spread out as comment almost in every field. Also appearing is "Setter for property xyz". Just out of curiosity, which Java tool generated this stubs?

java - read property value in Ant

I need to read the value of a property from a file in an Ant script and strip off the first few characters. The property in question is path=file:C:/tmp/templates This property is store in a file that I can access within the ant script via <property file="${web.inf.dir}/config.properties"/> I have two questions: How do I read the single...

java - How to assign bean's property an Enum value in Spring config file?

I have a standalone enum type defined, something like this: package my.pkg.types; public enum MyEnumType { TYPE1, TYPE2 } Now, I want to inject a value of that type into a bean property: <bean name="someName" class="my.pkg.classes"> <property name="type" value="my.pkg.types.MyEnumType.TYPE1" /> </bean>

Absence of property syntax in Java

C# has syntax for declaring and using properties. For example, one can declare a simple property, like this: public int Size { get; set; } One can also put a bit of logic into the property, like this: public string SizeHex { get { return String.Format("{0:X}", Size); } set { Size = int.Parse(value, NumberStyles.HexNumber); } } ...

java - A better class to update property files?

Though java.util.properties allows reading and writing properties file, the writing does not preserve the formatting. Not surprising, because it is not tied to the property file. Is there a PropertyFile class out there -- or some such -- that preserves comments and blank lines and updates property values in place?

java - In Eclipse RCP, how do I disable a save toolbar button according to the "dirty" property in editor

In my eclipse RCP 3.3 application, I would like to enable or disable a 'save' toolbar button according to current editor dirty flag. I'm trying to use the <

java - Enforce service layer business rules on entity property changes, or hide entity property from clients but not service?

Let's say I have a class Customer, which has a property customerType. I have another class called something like SpecialContract, which has a Customer and some other properties. If customer.customerType == SPECIAL, then there is a SpecialContract which has a reference to this particular Customer. Now I realize that this is a bit hoaky, but I do not want to maintain a relationship from Customer

java - Replace the property value using RegEx

I have config.properties file containing properties in Java Properties format. I need to replace the value of a property with a known name with a new value. The comments and formatting of the file should be preserved. My current approach is to use RegEx to match the property name and then replace its value. However, Java Properties supports multi-line values, which I have hard time matching. Here is an exam...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)