Crazy Java: basic skills of programmers breakthrough Lesson 16 - Gang edited study notes (to be continued)

Programmers breakthrough basic skills (Lesson 16)

Array

  • Static language: data types can be determined at compile time language, mostly static language requirements must be declared before use variable data types (with a few modern languages ​​derive strong ability to do)
  • Dynamic languages: determine the data type of language the program is running, no need to declare variables before data type

java is a static language, you declare a variable type before use.

//声明一个可存放数据类型为String,长度为5的数组
 String[] arr = new String[5];

Array initialization

There are two ways to initialize an array

  • Static initialization: initialization, a programmer displays the initial value of each element of the array of the specified array length is determined by the system
//静态初始化,方法一
String[] arr1 = new String[]{
    "hello",
    "world",
    "hello",
    "java"
};
//静态初始化方法二
String[] arr2 = {
    "xixi",
    "haha"
};
  • Dynamic initialization: the programmer to specify the length of the array initialization, the initial value is assigned by the system as an array
//动态初始化方法
String[] arr3 = new String[5];

Dynamic initialized, assigned default initialization value according to the following rules

1. 数组元素类型为基本类型中的整数(byte, short, int, long),默认初始值是0。
2. 数组元素类型为基本类型中的浮点数(float, double)默认初始值是0.0。
3. 数组元素类型为基本数据类型中的字符型(char),默认初始值是'\u0000'。
4. 数组元素类型为引用类型(类,接口,数组),默认初始值是null。

Once the array initialization is complete, you can not change the length of the array

Array necessarily need to initialize it?

  • You do not need to initialize the array variable
  • Array object references need to initialize the array variable
public static void main(String[] args) {
    String[] buff = new String[]{
            "v1",
            "v2"
    };
    //声明数组
    String[] movies;
    movies = buff;
    System.out.println(Arrays.toString(buff));
}

result:

[v1, v2]

Movies may only be made on the (variable does not need to initialize the array, but this reference the referenced object needs to be initialized).

Initialization of basic data types

    public static void main(String[] args) {
        int[] iArr; //1

        iArr = new int[]{ //2
            1,
            2
        };
    }
  1. The first step when declaring variables, but in the stack area declared a variable iArr, this time changing the amount has not yet point to any of the referenced object.
  2. The second step, when the heap area opened up a continuous memory space, length of 4, this time iArr point to heap memory space continuum.

Tips: Not all of the fundamental data types are stored in the stack area, more specifically in this case to say as "1,2 stored in the stack area, all the local variables are stored in a stack memory (including basic data types and variable references. type variable), but reference type variable 所引用的对象is always stored in the heap memory.

Reference type of data type initialization

class Person {
    int age;
    String name;

    public Person() {

    }

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }
}

public class ArrDemo {

    public static void main(String[] args) {
        Person[] students = new Person[2]; //1
        System.out.println(students.length); //2
        Person amber = new Person(1,"amber");//3
        Person nick = new Person(3, "nick"); //4
        students[0] = amber; //5
        students[1] = nick; //6
        System.out.println(amber); //7
        System.out.println(students[0]); //8
    }
}

Unfinished

Guess you like

Origin www.cnblogs.com/amberbar/p/11610381.html