Length and length in Java () deep analysis _java

Source: Internet
Author: User
Tags class definition

Consider one of the following questions before starting this article

How do I get the length of an array without using the IDE with automatic completion? And, how do I get the length of a string?

This question I have asked different levels of programmers, including the primary and intermediate level. Neither of them can answer the question accurately and confidently (if you can answer the question with confidence and accuracy, then prove that you have a better command of this knowledge than most mid-level programmers). Because many Ides now have code completion, this makes developers understand the superficial aspects of many issues.

The correct answer to the above question posture should be this:

int[] arr = new int[3];
System.out.println (arr.length); the degree to which the array is fetched using length

String str = "abc";
System.out.println (Str.length ()),//Use length () to get string lengths

So the question is, why does the array have the length attribute, and the string doesn't? Or, why does the string have a length() method while the array does not?

Why does the array have a length property?

First, the array is a container object that contains a fixed number of values of the same type. Once the array is created, his length is fixed. The length of the array can be used as the length of the final instance variable. Therefore, the length can be treated as an array of properties.

There are two ways to create an array:

1. Create an array of array expressions.

2, create an array by initializing the value.

Whichever way you use it, once the array is created, its size is fixed.

You use an expression to create an array as follows, which indicates the element type, the dimension of the array, and the length of the array of at least one dimension.

The declaration is in accordance with the requirement because he specifies the length of a dimension (the type of the array is int, the dimension is 2, and the first dimension is 3)

int[][] arr = new int[3][];

You need to provide all the initial values when you create an array by using array initialization. The form is to use {and} to enclose all the initial values and separate them.

Int[] arr = {1,2,3};

Note:

There may be a doubt here, since the size of the array is the initialization of the good, so the int[][] arr = new int[3][];定 meaning of the array does not give the size of the second dimension of the group, then the length of the arr is how to "set good"?

In fact, the length of the ARR is 3. In fact, all the arrays in Java, no matter how many dimensions, are actually one-dimensional arrays. Arr, for example, allocates 3 spaces, each of which holds the address of a one-dimensional array, thus becoming a "two-dimensional" array. But for arr, his length is 3.

Int[][] A=new int[3][];
System.out.println (a.length);//3
int[][] b=new int[3][5];
System.out.println (b.length);//3

Why not define a string-like array class in Java

Because arrays are also objects, the following code is also legal:

Object obj = new INT[10];

The array contains all the methods inherited from object (the inheritance of arrays in Java), except for Clone (). Why don't you have an array class? There are no Array.java files in Java. A simple explanation is that it is hidden (note: The array in Java is somewhat similar to the basic data type, is an builtin type, and there is no actual class corresponding to him). You can think of a problem--what would it look like if you had an array class? It will still need an array to hold all the array elements, right? Therefore, it is not a good idea to define an array class. (Translator Note: This may be a bit around, the truth is somewhat similar to: chicken eggs, eggs and chicken problems, may not be the analogy is very appropriate, please the reader's own understanding)

In fact, we can get the class definition of the array by using the following code:

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

Output:

class [I

"Class [I" represents the signature of the class object Run-time type of "array of member type is int")

Why does string have a length () method?

The data structure behind string is a char array, so it is not necessary to define an unnecessary attribute (because the property is already available in char values). Unlike C, an array of char in Java is not equal to a string, although the internal mechanism of a string is implemented by a char array. (Note: In the C language, there is no string class that defines char string[6] = "hollis"; the form that the string is usually used for)

Note: to convert char[] to a string there are the following ways:

char []s = {' A ', ' B ', ' C '};
String string1 = s.tostring ();
String string2 = new string (s);
String string3 = string.valueof (s);

Summarize

The above is the entire content of this article, I hope the content of this article for everyone to learn or use Java can help, if you have questions you can message exchange.

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.