Start with length and length () Java__java

Source: Internet
Author: User
Tags array length

Translation staff: Anchor

Translation time: November 23, 2013

Original link: Start from length & Length () in Java


First, please look at the following question, can you answer it quickly?

Assuming there is no integrated development environment and auto complement, how do I get the length of the array? How do I get the length of the string?

I will answer this question for the novice and the developer between the level of the medium level:. Because many of them do not answer correctly, or have no confidence in their own answers ( translator: This is true of foreigners ' junior programmers ).

Although the IDE provides easy and quick code completion functionality, it also comes with a drawback, known as surface Understandin.

This article explains some of the important concepts of Java arrays.

The answer to the above question is:

int[] arr = new int[3];
System.out.println (arr.length);//Array length
 
String str = "abc";
System.out.println (Str.length ());//String length
The problem is why the array has a length property field and string does not. Or why the string object has a length () method and the array does not?

1. Why does the array have a length property?
First, the array is a container object that holds a fixed number of values for a single type. After the array is created, its length is fixed. So the length property of the array can exist as a final domain.

Therefore length can be used as the defining property of an array (defining attribute).

There are two ways to create an array:

1) new Array creation expression

2 an array initializer in curly brace form (array initializer)

The size of an array is specified when it is created.

In the example above, an array creation expression (an array creation expression) is used, which specifies the type of the element, the dimension of the array, and the minimum size to be specified for the topmost dimension.

The following declaration is also legal because it indicates the dimensions of the array and the size of the top-level dimension.

int[][] arr = new int[3][];
Array Initializers(array initializer) creates an array by specifying all the elements contained, and a semicolon (,)-separated list of expressions in the curly braces "{}". For example:

Int[] arr = {1,2,3};
2. Why not define an "Array" class like string??
Now that the array is an object, the following code is legal:

Object obj = new INT[10];
The array object contains all the members inherited from the object class ( exceptClone method). Why don't you define a class for an array? Nor can we find a class (file) called Array.java.
A rough even this class is not visible to developers.

Can you think of that? --If you have an explicit array class, what should it look like?  It still needs an array to hold the array elements, right? It is therefore not reasonable to define such a class.

We can actually get the Class (property) of an array, and the code looks like this:

int[] arr = new int[3];
System.out.println (Arr.getclass ());
Output results:

class [I
"Class [I" represents the run-time signature of "an array of member types that are int".)
3. Why string has a length () method??
The data structure behind a string object is an array of char characters and does not need to define a less-used property field, unlike the C language, where a char-type array is not a string.

reference materials :

[1] Arrays
[2] JLS Array

Related reading :

How to Convert Array to ArrayList in Java? Example of sorting Arrays Java method to Shuffle a Array top Methods for Java Arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.