Length field and Length () method in Java

Source: Internet
Author: User
Tags array definition

This article mainly describes the length field in Java and the length () method, the need for friends can refer to the following, more Java tutorial knowledge, please visit the Crazy Software Education website.

First of all, can you answer the following questions quickly?

How do I get the length of an array and string without the auto-complete functionality of the IDE (integrated development environment)?

I asked different levels of developers (beginner and intermediate) the same questions, and they were hard pressed to give an accurate and assured answer. Although the IDE provides the code auto-complete functionality is very convenient, but also brings a "surface understanding" problem. In this article, I'll explain some of the key concepts of Java arrays.

The answers to the above questions are as follows:

int[] arr = new INT[3];

System.out.println (arr.length);//The Length property of the array

String str = "ABC";

System.out.println (Str.length ());//The length () method of the string

The question now is, why does the array have the length property and the string does not? Or, why does the string have the length () method and the array does not?

Question 1. Why does an array have the length property?

First, an array is a container object that holds a certain number of values for a certain type. When an array is created, its length is no longer changed. The length of the array is visualized as a final instance variable. Therefore, the length can be considered as an attribute of the array definition.

There are two ways to create an array: 1) array creation expressions, 2) array initialization. Once the array is created, its length is fixed.

The following array declaration, while only indicating the length of the first dimension in the array, is legal.

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

The initialization of an array creates an array and assigns an initial value to each of these elements, which is made up of a comma-delimited number of expressions surrounded by "{" and "}", as shown in the following example:

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

Question 2. Why isn't there an "Array" class defined like "String"?

Because the array is an object, the following code is legal.

Object obj = new INT[10];

The array contains all the members (except clone) in the object class. Why can't you define an array like a class? We can't find a Array.java file. A relatively simple explanation is that they are hidden. Consider this question: If you have an array class, what is it like? We still need an array to hold the array data, don't we? Therefore, it is unwise to define a similar one.

In fact, we can get an array class in the following way:

int[] arr = new INT[3];

System.out.println (Arr.getclass ());

The result of the output is:

Class[]

"Class[" is the run-time type signature of the "array of type int" class object.

Question 3. Why does the string have the length () method?

A data structure that holds character data in a string is an array of characters. Therefore, it is not necessary to define a field that is not required in the actual application. It doesn't work with C, in Java, a character array is not a string.

Crazy Java Training focus on software development training, improve the employability of students, focus on improving practical hands-on ability. Technical knowledge precipitation deep teacher, let you feel the charm of Java, inspire your love of programming, let you in half a year in the time to master 8.1 million of the code, Master Java core Technology, become a true technical master, through a lot of real enterprise projects crazy training, rapid accumulation of project experience. Let you become a skilled modern high-end talent, quickly get well-paid employment! Course related Study Please consult qq:544627560 Tel: 020-28309378 qq:707552864, or ask Crazy Software education Center, address: Guangzhou Tianhe District Chei Wang Building 3 floor

Length field and Length () method in Java

Related Article

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.