Java Quick Start--3

array

In java, any type can be defined as an array type. For example, we have learned the int type before, int arr[] = new int[10]; at the same time, if we have learned objects, we can define an array of objects, for example, Person p[] = new Person[10], an array of int type, It will be automatically initialized to 0, and the array of boolean type will be automatically initialized to false.

//给数组赋值成0-9
int arr[] = new int[10];
for(int i = 0;i<10;i++){
    a[i] = i;
}

It is worth noting that in an array, it starts from 0, then 0 starts, and the 10th position from the back is 9, so it is up to 9 here
. If we assign values ​​to the entire array first, then we only need to use The length property can be used.

int arr[] = new int[10];
for(int i = 0;i<arr.length;i++){
    arr[i] = i ;
}

If you assign a small amount, then you can use the initialization method so far

int arr[] = {1,2,3};
//循环打印
for(int i = 0;i<arr.length;i++){
    System.out.println(arr[i]);
}

function

If we have a large number of code blocks that need to be reused, then we can encapsulate it together and define a function in java. For example, I want to output the prime numbers within 1-100

public static boolean isPrime(int i ){
    if(i<= 1){
        return false;
    }
    for(int q = 2 ;q<=Math.sqrt(i);q++){
        if(i%q == 0) return false;
    }
    return true;
}

public static void main(String args[]){
    for(int i = 1;i<= 100;i++){
        if(isPrime(i)){
            System.out.println(i);
        }   
    }
}

In this way, if we write and write and find that we have to judge the prime number again, at this time, we only need to call the isPrime() function, instead of writing the contents of isPrime() again.

function and array

In connection with the above array, if we want to initialize the elements of this array to 1, then we can also define a function separately

public staitc void init(int arr[]){
    for(int i = 0;i<arr.length;i++){
        arr[i] = 1;
    }
}

In this way, we don't generate an array in the future, we just need to pass the array to this function to execute, for example

int arr[] = new int[200];
init(arr);

Functions are associated with primitive types

The array is initialized above, so can we do this if we want to initialize the variables of the basic data type, and put it in a function to initialize it.

    public static void main(String[] args) {
        int a = 0; 
        init(a);
    }
    public static void init(int i ){
        i = 2;
    }

If we also want to initialize like an array, is it possible to see the above way of writing? The answer is no. The above writing method cannot change the value of a. The reason is that the variable a here is a basic data type, which is placed on the stack. We cannot directly modify the value in the stack. We know that the stack It is last-in, first-out. Here, the value of a is assigned to i in init, that is, the actual parameter is passed to the formal parameter, but it is just a copy. In the inti function, no matter how we operate, we will is the content of i of the operation, not the content of a.

pass by reference and pass by value

The above two examples are the best explanations of pass by reference and pass by value. The array is placed in the heap, and we can directly modify the data in the heap by reference, and the basic data type is stored in the stack. We know that the stack is backward. First out, we cannot directly modify the contents of the stack, and the JVM will not allow this.
If you have learned C language or C++, you can understand it as a pointer in C, and you can understand it as a reference in C++. We will talk about objects later. In fact, the passing of objects is also passed by reference. We can put this object into another function to initialize it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325463472&siteId=291194637