Java foundation of array types

For Java, in front of some basic concepts not really want to write, or looked at from the array began to write it (after all, the first array is a reference type, relatively complicated), I also learned not long JAVA, JAVA is currently reading the basic video, as well as JAVA crazy handouts most of the book with the basis of relevant content, but after reading the study found that before a lot of things are forgotten (too anxious, too little hands-on practice), so I want to learn framework before two brush again, to recap the basics of points. By the way writing a blog to record my JAVA learning process, as well as knowledge focused as I understand ~!

 

On Array

  The first array is a reference type we encounter in the learning process of JAVA, it basic types (int, flout, String) until we know different, data storage array is stored in the heap memory, and we basically type of data is stored in the stack memory (for the stack pop will be described in detail later). Then began our journey array ~

 

Overview array

  JAVA, we require that the array of data must be stored in the same type of data, so we can not store an array of a plurality of data element types (different from Javascript). When the end of our array initialization, the length of our array was determined, the length of the array is fixed (the difference between the set: a set of variable length). The lists are cleared when the space it occupied is still reserved.

 

Array definition

  Definitions: Data Type [] = new Array name Data type [or the number of elements in the array length];

  For the description of the array definition: 1) Data Type: The data type stored in the array elements.

            2) [] represents a mean of the array.

            3) the name of a custom variable identifier.

            4) new keyword to create a container.

            5) Data Type: The data type stored in the array elements

            6) [] represents a mean of the array.

            7) the number of elements is an array, the number of data can be stored (a constant, fixed length)

  An array is a container: storage to each element in the array has its own automatic numbering. Smallest index is 0, the maximum index value (an array of length - 1). Access to an array of storage elements, must rely on indexes: array name [index].

  Arrays offer a property to obtain an array of length: length property. We assume that the array so called arr, on the length of the array is: arr.length, the first element of the array is on arr [0], the last element of the array is on arr [arr.length-1].

 

JVM understanding

  JVM JAVA virtual machine is equivalent to connection with the operating system, popular understanding is, java is developed on the JVM.

  JVM own memory is divided into five areas: (1) Register: between memory and CUP. . (2) native method stacks: JVM calls the functions in the system. . (3) methods and data sharing: the local class file into the running of the period. . (4) Method stack: When running all the way into the memory. (5) Bulk: the storage containers and objects

              

Array initialization

  Initialize an array of two ways: static and dynamic initialization initialization.

(1) static initialization: Specifies initial value of each array element by the programmer, the system determined by the length of the array.

 

int[] array = {1,2,3,4,5};

(2) Dynamic Initialization: the programmer to specify only the length of the array, the value assigned by the system for the array elements.

 

int[] array = new int[5];

  The value assigned to the system have few several: 1) .byte, short, int, long type, an array is assigned to zero.

               2) .float, double type, an array is assigned to 0.0.

                 3) .char type, the array is assigned to the "\ u0000".

               4) .boolean type, an array is assigned to false.

               5) A reference types (classes, interfaces, arrays), the array is assigned to null.

 

增强的for循环---foreach循环

  在Java5之后,Java提供了一种新的循环:foreach循环。foreach循环会自动遍历数组和集合的每个元素。

foreach的语法格式如下:

for(type arrName : array||collection){

    //自动迭代访问的每个元素
}    

 

type:数组元素或者集合元素的类型。arrName:一个形参名。array:要遍历的数组名。collection:要遍历的集合名。

下面举一个例子来使用foreach方法:

public class fun {
public static void main(String[] args) {
    String[] array = {"ABC","abc","sdf","gvc","sf"};
    for(String arr : array){
        System.out.println(arr);
    }
}
}
//最终的结果是:
ABC
abc
sdf
gvc
sf

 

但是值得注意的是:使用foreach循环的时候,不能在循环中对数组元素进行赋值,否则得出的结果是不正确的,这里面不详细说明。

 

深入了解数组

  上面我们了解了数组的一些基本的功能以及使用方法,但是数组在内存中的运行机制是什么呢?之前提到的基本类型以及引用类型的存储在哪里呢?堆存储和栈存储的区别是什么呢?接下来我一一讲述。

我们首先要明确的一个概念就是:数组是一个引用的数据类型,数组的引用变量只是一个引用,数组元素和数组变量在内存中是分开的。

数组变量其实是一个引用(相当于C语言里面的指针),我们通过这个数组变量指向有效的内存来访问元素。因此我们如果要访问数组里面的变量,我们只能使用数组的引用变量来访问它的元素。

接下来是一个数组的内存图,通过这个图来理解内存中的数组:

                

 

  首先我们要了解的就是,在JVM中,栈和堆都是存储的容器。每一个方法在执行的时候,该方法都会建立一个自己的内存栈,在这个方法内定义的变量将会逐个的放在这块栈里。随着这个方法的结束,这个栈会自动的销毁,因此,所有方法中的局部变量都是存储在栈内存中的。当程序中创建一个对象的时候,这个对象将被保存在运行时的数据区中,以便可以反复的利用(因为创建对象的成本大),这个运行时的数据区就是堆内存,堆内存中的对象不会因为方法的结束而被销毁,因为即使这个方法结束后,该对象也可能被另一个引用变量引用,只有当一个对象没有任何引用变量引用它的时候,它才会被系统的垃圾回收器在合适的时候销毁。有时候我们会让系统回收一个数组所占的空间,那么我们只需要将数组变量赋值为null,这就切断了数组引用变量和实际数组之间的关系,数组就被回收了。

  如上所示,我们将数组的引用放在栈内存中(arr),将数组的对象放在堆内存中(new int[3]),我们数组的引用实际上是一个指向它的对象的指针,当我们想在数组中取第一个元素的时候,我们只需要将这个指针指向第一个元素,我们便可以获得第一个元素的值:arr[0]。

 

二维数组

所谓二维数组,通俗的来说就是将一维数组存储到了一维数组中.。我们将二维数组的定义为:

type[][] arrName = new type[][];
或者
type[][] arrName = [[1,2,3],[2,3,4],[3,4,5]];

 

其中第一个代表行数,第二个代表列数。

二维数组的内存图如下所示(与一维数组差不多,就是在一维数组中存储了一维数组而已):

                  

我们可以把二维数组理解成一维数组。向3维,4维......n维数组,暂且可以不用看,一般二维就已经够了,了解了内存的存储方式,其实也挺好理解的。

Guess you like

Origin www.cnblogs.com/liuzengzhi/p/11510278.html