Cattle off network do problems 5.26

5.26

(1)a = Integer.parseInt("1024");

         b = Integer.valueOf("1024").intValue();

The following statement is correct

a和b都是整数类型变量并且它们的值相等。

intValue () is the Integer object type into the base data type of int; 
the parseInt () is the basic data type of the String into int; 
valueOf () is converted to the String object type Integer; (JDK version now supports autoboxing unpacking a).
the title: parseInt was the basis for the data type int, valueof obtained is boxed data type Integer, and then converted by int valueInt, D is selected so

(2) single-mode embodiment, two basic points are 

构造函数私有
唯一实例

Typically singleton pattern in Java language, there are two construction methods:

1. lazy way. It refers to the global singleton instance constructed when first used.


// lazy-style
public class Singleton2 {
Private static Singleton2 INSTANCE = null; // In addition such an approach may block in multithreaded, so there is an optimized writing, is to use the volatile modification INSTANCE, then getInstance () method in the if (INSTANCE == null) do it again if the determination is determined.

Private Singleton2 () {}

public static Singleton2 the getInstance () {
if (INSTANCE == null) {
INSTANCE = new new Singleton2 ();
}
return INSTANCE;
}
}

2. A hungry man way. It refers to the global singleton instance of class loading when constructed.


//饿汉式
public class Singleton {
private final static Singleton INSTANCE = new Singleton();

private Singleton(){}

public static Singleton getInstance(){
return INSTANCE;
}
}

(3) The following have the right

call by value不会改变实际参数的数值
call by reference不能改变实际参数的参考地址
call by reference能改变实际参数的内容

Reference data type is passed by reference (call by reference), the value passed is the basic data type (call by value)

Value transfer can not change the content and address of the original variable --- "shape because of the java method of transmission parameters are transmitted a copy of the original variables, it is changed in the copy of the value of the process, not suitable for the raw variables

Passed by reference address can not change the original variables, but you can change the contents of the original variables --- "The reason is to change the reference when a copy of the original variable references has not changed, when a copy of the changed content, due to the copy of the reference points it is the address space of the original variable, so the contents of the original variable changes.

 

Conclusion: 1. The value is passed and can not change the contents of the address of the original variables;

           2. The reference delivery address can not change the original variables, but you can change the contents of the original variables;

(4) Java involved byte, short, and char types can be coerced to int

 

(5) public static void main (String [] args) OR public static void main (String args []). This is the Java specification, which is determined by JVM. When a class java implementation of the program, the main method is performed first, and implementation is the class name + method name, it must be public static, specifications must pass a String [] as an argument, the name does not matter String array this command line array used to hold incoming main method parameters, for example: java HelloWord 1 2 3, then the args.length == 3. Specific mechanisms need to have some knowledge of the JVM, JVM all must be a considerable degree of learning in order to better grasp the Java operating mechanism

 

(6)

class Car extends Vehicle

{

    public static void main (String[] args)

    {

        new  Car(). run();

    }

    private final void run()

    {

        System. out. println ("Car");

    }

}

class Vehicle

{

    private final void run()

    {

        System. out. println("Vehicle");

    }

}

Which of the following description for the code to run the result is correct?

Car

Resolution:

The parent class method this question are private modification, it is not visible to subclasses, subclasses can not override. Therefore, the parent class and subclass methods are two methods.

Expansion: If the parent class method private instead public what will happen?

        Will complain because the parent class methods are final modified, it can not be overwritten.

(7) output is quickly descending row below

//看我的注释这种快排的思路就很清晰了,这是只从一个方向遍历的快排

public class A2 {

    public static void main(String[] args) {

        int[] a = { 2, 4, 6, 8, 3, 6, 9, 12 };

        quickSort(a, 0, a.length - 1);

        for (int i = 0; i <= a.length - 1; i++)

            System.out.print(a[i] + " ");

    }

 

    private static void quickSort(int[] a, int start, int end) {

        if (start < end) {

            int p = core(a, start, end);

            quickSort(a, start, p - 1);

            quickSort(a, p + 1, end);

        }

    }

 

    private static int core(int[] a, int start, int end) {

        int x = a[end];

        int i = start;  //记录遍历完后最后一个数应该放在的位置,初始就是start,因为如果前面没有数比最后一个数大,那么下面遍历完后最后一个数就应该放在start的位置

        for (int j = start; j <= end - 1; j++) { //遍历的目的是把参与排序的这轮数中比最后一个数大的数都放到最后一个数前面

            if (a[j] >= x) {

                swap(a, i, j);  

                i++;  //每遇到一个比最后一个数大的数,最后一个数应该放的位置就+1

            }

}

        swap(a, i, end); //这里一交换后就把最后一个数放在了正确的位置,这样左边的数都比最后一个数大,右边的数都比最后一个数小

        return i;

    }

 

    private static void swap(int[] a, int i, int j) {

        int tmp = a[i];

        a[i] = a[j];

        a[j] = tmp;

    }

}

(8)

In the switch (expr1), expr1 can only be an integer expression or enumeration constant (larger font) , integer expression can be a primitive type int or Integer type of packaging because, byte, short, char can be implicitly converted int, and so these types, and these types of packaging types are also possible. Obviously, long, float, double type does not conform to the syntax specified switch, and can not be implicitly converted to int type, so they can not act on the swtich statement.
Note: String types are supported Java7 start.

(9)

1. For an external class is concerned, it can also be modified using the access control characters, but only outside of class, there are two levels of access control: public and default. Because the outer class is not in any kind of internal, there is no sub-class of two ranges inside it in class, where the class, so private and protected access control at no significance outside class.

2. The program unit on an inner class external class, having four scopes: same class (private), (protected), and any position (public) the same package.

3. Because the scope of local members is a method where the other program units can never access another method of local variables, so all the members can not use the local access control modifier modified.

 

(10) has one of the following objects:

public class DataObject implements Serializable{

    private static int i=0;

    private String word=" ";

    public void setWord(String word){

        this.word=word;

    }

    public void setI(int i){

        Data0bject. i=I;

     }

}

Creating a way of following DataObject: 

DataObject object=new Data0bject ( );

object. setWord("123");

object. setI(2);

This target sequence into a file, and read files in another JVM, deserialized, what value Data0bject objects in this case the read word and i, respectively:

"123", 0

Resolution:

Java serialization is not in the instance variables of static and transient modification of variables, because the members of the class representatives static, transient temporary data representative of an object, is declared both types of data members can not be serialized.

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_41673515/article/details/90572598