How does Java understand arrays as a class in Java? __c language

Source: Internet
Author: User
Tags null null

The use syntax for Java arrays is similar to that of C and C + +, but its essence is a Java class, but this class is more specific, so it is easily misunderstood.

As you know, Java data types can be divided into two types: basic data types and reference data types. So, what kind of array does it belong to? Obviously, an array is not a basic data type, because the base type is only those 8, so he should belong to the reference data type, which means that each instance of the group is an object, which belongs to a class.

First, take a look at how to use a Java array, which has the following steps:

1. Declaring arrays, using a reference class to manipulate an array

2. Use the new keyword to create an array of memory space required for arrays, return a referenced value, and assign an initial number to the {} symbol Levy array.

3. Use the array subscript to manipulate the values of the pairs.

The sample code is as follows:

public class Test2 {public

	static void Main (string[] args) {
		
		int [] arr=new int[]{1,2,3,4,5};//defines an int array variable
		S Tring []arr2=new string[3];//defines a String array variable for
		(int i:arr) {
			System.out.println (i);
		}
		for (String i:arr2) {
			System.out.println (i);
		}
		
	}
}

The above code creates two arrays, an array of int, initializes the data, and the other is a string array, specifying the length of the array as 3~.

The results of the operation are as follows:

1
2
3
4
5
null
NULL NULL

Two types of arrays have a common feature, which is that they must know a length when they are created. In fact, this is because when an array is created, it dynamically knows the number of member variables for these types and assigns an initial value to those member variables. However, Java does not allow us to manipulate these member variables like ordinary classes, but rather through [] notation, using subscript to access the data. In addition, Java also provides a length property for each array to know how long the array is.

Note: The subscript for the array starts at 0.

So, what is the class name of the array? In fact, the type of the array is related to the data type it stores, their class names can be obtained by getting the class type of the array, and the following code is used to get the names of the two arrays of the previous example:

SYSTEM.OUT.PRINTLN ("int arr type name:" +arr.getclass (). GetName ());

System.out.println ("String arr2 type name:" +arr2.getclass (). GetName ());

The results are as follows:

int arr type Name:[i

String arr2 type name:[ljava.lang.string;

As you can see, the array class name always starts with the left bracket, followed by a different label. For example, an array of type int is an i-character, and an array of string types is Ljava.lang.String. With these markings, we can see what type of data the array stores.

Summarize:

The array nature of the

   java is a class that also holds information about the data type. The class holds the data through the form class of the member variable, and the subscript is used to access the data through the [] symbol. When working with basic data types such as int, the array holds the value of the variable. If we do not provide the initial value, the array will have the initial value of these variables 0, while processing the reference type (such as String), the array holds the reference to the data, if we do not initialize, the array will be the initial value of the variable is null.

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.