Java Beginner: Arrays, declarations, and initialization

Source: Internet
Author: User

Today we talk about the array in Java, the meaning of the array in the programming language, equivalent to the set of discrete mathematics, its meaning is almost identical, only a little difference, there are infinite sets in the discretization, for example, the real number set, but there is no infinite set in Java. In Java, an array is a collection of some data. As our rookie, most of the cases see the array, where the data belongs to the same type. In rare cases, which are not of the same type, there is a good chance that polymorphic phenomena will occur, and when accessing the methods of array elements, it is possible to generate dynamic bindings, which we'll talk about later.

Well, let's say that the elements in the array are all of the same type at the moment. So, how do you declare and initialize an array?

There are two common forms of declaring an array:

1. Type name [] Array name

2. Type an array group name []
Let us give an example, for example, if we want to declare an array of integers named Int_array, then it should be: int[] Int_array, or this: int int_array[]. Similarly, string[] sarray We have a string array called Sarray. Double[] Darray We have a floating-point array named Darray. Some programmers like the second way of declaring, but I like the first way of declaring, because the first way of declaring clearly tells us that it is an array, and immediately after the array name, the points are very clear. Moreover, the second way is more like C + +, I personally do not agree with the programmer to bring a program language habits into other programming languages, although sometimes this does not appear grammatical errors.

One thing to note is that, declaring an array, just declaring that you just have a name, you can assume that the computer opens up a new memory address for your array, but there is nothing in it. Since there is no initialization, your array is now without fragrance, and if you do not initialize the array immediately after the declaration, Java will make an error, such as this:

We see that this program cannot compile and observe the error message: Variabl A might not has been initialized, that is, the variable a may not be initialized yet. We know, not perhaps, that there is really no initialization. In fact, not only the array, any data, if you just declare, it is not directly used.

OK, let's take a look at how to initialize an array? The initialization of an array is common in the following ways:

1. Array name = new type name [array length] (this array has already been declared)

Array name = {Data 1, data 2, Data 3, ..., Data n} (this array has already been declared)

Array name = new type name []{data 1, data 2, Data 3, ..., Data n} (this array has already been declared)

2. Type name [] Array name = new type name [array length] (declared and initialized together)

Type name [] Array name = {Data 1, data 2, Data 3, ..., Data n} (declared and initialized together)

Type name [] Array name = new type name []{data 1, data 2, Data 3, ..., Data n} (Declaration and initialization)

3. Type an array group name [] = new type name [array length] (declared and initialized together)

Type an array group name [] = {Data 1, data 2, Data 3, ..., Data n} (declared and initialized together)

Type an array group name [] = new type name []{data 1, data 2, Data 3, ..., Data n} (Declaration and initialization)

4. Use loops or other operations to initialize or assign a value to an array, if it has already been declared.

Summing up, the last one, we do not mention, and the first three forms, in fact, when initializing an array, the left side of the equals sign has three formats, and the right side of the equal sign has three formats. For arrays, the most common use is this: type name [] Array name = new type name [array length], for example: double[] Floatarray = new double[100], so we declare and initialize a length of 100, An array of type double with the name Floatarray.

However, if we initialize an array in different ways, what is included in the array? If the length of the array is given only, and the data in the array is not displayed, then if the type is double, then the number is 0.0, if the array type is int, then the data inside is 0, if the array type is string, then the data inside is null (null If the array type is Boolean, then the data inside is false. But if the display gives the data, then we do not need to give the array length, Java will automatically get the length of the arrays, and the data are already there, we look at the following test program:

We see that, in addition to the last one, the contents of several other arrays have been initialized by default in Java, which seems to be the last way to be better, but in fact, the last one is not commonly used, because in general, the array is not deterministic at the beginning of the definition, It is often necessary to dynamically assign values in the program, so let Java give automatic assignment, actually just walk formality, let this array can be used only.

Well, today we're talking about the declaration of arrays and the basics of initialization, and we'll talk about arrays later. I hope that you beginners and the novice to work together to practice and common progress.

Java Beginner: Arrays, declarations, and initialization

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.