Java Chapter 2 Exercise Lecture (2)

Posted by iffy on Sun, 30 Jun 2019 23:05:01 +0200

Chapter II Exercise Explanation (1)

3. Read or debug programs

(1) Run the following program on the computer, pay attention to the output results.

Java
public class E{
    public static void main(String args[]){
        for(int i=20302;i<=20302;i++){
            System.out.println((char)i);
        }
    }
}

(2) Debug the following programs on the computer, pay attention to the difference between System.out.print() and System.out.println().

Java
public class OutputData{
    public static void main(String args[]){
        int x=234,y=432;
        System.out.println(x+"<"+(2*x));
        System.out.print("I don't return after I output the results.");
        System.out.println("I output the results and automatically return to the next line.");
        System.out.println("x+y="+(x+y));
    }
}

(3) Debugging the following programs on the computer to understand the range of values of basic data types.

Java
public class E{
    public static void main(String args[]){
        System.out.println("byte Range of values"+Byte.MIN_VALUE+"to"+Byte.MAX_VALUE);
        System.out.println("short Range of values"+Short.MIN_VALUE+"to"+Short.MAX_VALUE);
        System.out.println("int Range of values"+Integer.MIN_VALUE+"to"+Integer.MAX_VALUE);
        System.out.println("long Range of values"+Long.MIN_VALUE+"to"+Long.MAX_VALUE);
        System.out.println("float Range of values"+Float.MIN_VALUE+"to"+Float.MAX_VALUE);
        System.out.println("double Range of values"+Double.MIN_VALUE+"to"+Double.MAX_VALUE);
    }
}

(4) What are the output results of [Code 1] and [Code 2] annotated by the following programs?

Java
public class E {
    public static void main(String args[]) {
        long[] a = { 1, 2, 3, 4 };
        long[] b = { 100, 200, 300, 400, 500 };
        b = a;
        System.out.println("array b Length:" + b.length);  //[Code 1]
        System.out.println("b[0]=" + b[0]);           //[Code 2]
    }
}

(5) What are the output results of [Code 1] and [Code 2] annotated by the following programs?

Java
public class E {
    public static void main(String args[]) {
        int[] a = { 10, 20, 30, 40 }, b[] = { { 1, 2 }, { 4, 5, 6, 7 } };
        b[0] = a;
        b[0][1] = b[1][3];
        System.out.println(b[0][3]);        //[Code 1]
        System.out.println(a[1]);           //[Code 2]
    }
}

Answer Analysis

1. The program uses a circular statement. The output is the value of (char)20302. The output is actually 20302 characters in the Unicode table. The result is "low".

2.
System.out.print() does not return after output, while System.out.println() automatically returns after output.

3. Understanding the range of values for basic data types

4. Answer: [Code 1] 4. [Code 2]: b[0]=1.
Resolution: Arrays are reference variables, so if two arrays of the same type have the same reference, they have exactly the same elements. So when b = a is executed, the system releases the elements initially allocated to b, and B and a point to the same memory address (with the same reference), so the length of B is a length of 4, b[0]=a[0]=1.
We have learned pointers in C language, pointers are addresses, and references in Java are very similar to pointers. The difference is that pointers can be modified, such as p++, while references cannot be modified. It improves the security of the program at the expense of flexibility.

5. Answer: [Code 1] 40 [Code 2] 7
int[] a = { 10, 20, 30, 40 }, b[] = { { 1, 2 }, { 4, 5, 6, 7 } };
Let's note that a is a one-dimensional array with 10,20,30,40 elements, while b is a two-dimensional array (equivalent to int[]b [] format). Each element in a two-dimensional array is a one-dimensional array. Java's characteristic is that the length of a one-dimensional array in a two-dimensional array is unlimited, so the first element is 1,2 and the second element is 4,5,6,7.
We can think of a two-dimensional array as a matrix with the first subscript representing the row and the second subscript representing the column.
So array b can be seen as

14250607

b[0]=a is equivalent to assigning a reference to b[0] (b[0] as a one-dimensional array), and the result becomes
104205306407

b[0][1] = b[1][3], so b becomes
10475306407

So the value of output b[0][3] is 30, so the question is, what is the value of a[1] 7 instead of 20? Because the value in a has also been changed, the reason is that the reference of a has been assigned to b[0], so b[0] and a point to the same memory address, then if the value in b[0] is changed, the value of a will change accordingly.

Topics: Java C