Java basic interview questions and answers (d)

reflection

57. What is a reflection?

Primary reflector means can access the program, the ability to detect and modify its own state or behavior

Java reflection:

In the Java Runtime Environment, for any class, you can know what properties and methods of this class have? For any object can call any of its methods

Java reflection mechanism mainly provides the following functions:

  • A determination object belongs to any class at runtime.

  • At runtime class of an object in any configuration.

  • Analyzing any class has member variables and methods at runtime.

  • Call any of the object's method at run time. 

 

58. What is java serialization? You need to serialize under what circumstances?

It is to simply save state (i.e. the instance variable, not a method) of various objects in memory, and can save the state of the object read out. Although you can use your own variety of ways to save the object states, but Java provides a mechanism should give you a good state of preservation of the object than yourself, and that is serialized.

Under what circumstances need to be serialized:

a) When the state of the object you want to save into memory in a file or in a database when;
B) when you want to use the socket transfer object on the network;
C) if you want to transport object when RMI ;

 

59. Dynamic proxies are what? Which applications have?

Dynamic Agent:

When you want to implement the method in an interface class, plus some additional processing. For example, add logs, plus affairs. You can create a proxy for the class, named Incredibles is to create a new class that includes not only the function of the original class methods, but also added a new class of extra processing is still based on the original. The proxy class is not defined, are dynamically generated. Decoupling has meaning, flexibility, and strong expansion.

Dynamic Agent application:

  • Spring's AOP

  • Plus Affairs

  • Plus rights

  • Plus log

 

60. how dynamic proxy?

We must first define an interface, but also (transfer object class that implements the interface to it) a process InvocationHandler class. Then there is a utility class Proxy (habitually referred to as a proxy class, because he calls the newInstance () can generate a proxy object, in fact, he was just a tool to generate a proxy class object). The use of InvocationHandler, splicing proxy class source code, compile the binary code generated proxy class, using the loader loads, and it yielded proxy object instance, and finally returned.

Object copy

61. Why use a clone?

Think of an object is processed, want to keep the original data in the next operation, you need a clone, Java language clones is an instance of the class.

 

62. How to achieve the object clone?

There are two ways:

1) to achieve Cloneable interface and override clone () method of class Object;

2) implement Serializable achieved by cloning serialization and deserialization of the objects, the true depth clones can be achieved, as follows:

 1 import java.io.ByteArrayInputStream;
 2 import java.io.ByteArrayOutputStream;
 3 import java.io.ObjectInputStream;
 4 import java.io.ObjectOutputStream;
 5 import java.io.Serializable;
 6 
 7 public class MyUtil {
 8 
 9     private MyUtil() {
10         throw new AssertionError();
11     }
12 
13     @SuppressWarnings("unchecked")
14     public static <T extends Serializable> T clone(T obj) throws Exception {
15         ByteArrayOutputStream bout = new ByteArrayOutputStream();
16         ObjectOutputStream oos = new ObjectOutputStream(bout);
17         oos.writeObject(obj);
18 
Bin = new new ByteArrayInputStream ByteArrayInputStream. 19 (bout.toByteArray ()); 
20 is the ObjectInputStream the ObjectInputStream new new OIS = (bin); 
21 is return (T) ois.readObject (); 
22 is 
23 is // Description: call close ByteArrayInputStream object or not ByteArrayOutputStream any meaning 
24 // based on these two streams as long as the garbage collector can release resources to clean up the object memory, which is different from the release of external resources (such as file stream) of 
25} 
26}

Here is the test code:

 1 import java.io.Serializable;
 2 
 3 /**
 4  * 人类
 5  * @author nnngu
 6  *
 7  */
 8 class Person implements Serializable {
 9     private static final long serialVersionUID = -9102017020286042305L;
10 
11     private String name;    // 姓名
12     private int age;        // 年龄
13     private Car car;        // 座驾
14 
15     public Person(String name, int age, Car car) {
16         this.name = name;
17         this.age = age;
18         this.car = car;
19     }
20 
21     public String getName() {
22         return name;
23     }
24 
25     public void setName(String name) {
26         this.name = name;
27     }
28 
29     public int getAge() {
30         return age;
31     }
32 
33     public void setAge(int age) {
34         this.age = age;
35     }
36 
37     public Car getCar() {
38         return car;
39     }
40 
41     public void setCar(Car car) {
42         this.car = car;
43     }
44 
45     @Override
46     public String toString() {
47         return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
48     }
49 
50 }

 

 1 /**
 2  * 小汽车类
 3  * @author nnngu
 4  *
 5  */
 6 class Car implements Serializable {
 7     private static final long serialVersionUID = -5713945027627603702L;
 8 
 9     private String brand;       // 品牌
10     private int maxSpeed;       // 最高时速
11 
12     public Car(String brand, int maxSpeed) {
13         this.brand = brand;
14         this.maxSpeed = maxSpeed;
15     }
16 
17     public String getBrand() {
18         return brand;
19     }
20 
21     public void setBrand(String brand) {
22         this.brand = brand;
23     }
24 
25     public int getMaxSpeed() {
26         return maxSpeed;
27     }
28 
29     public void setMaxSpeed(int maxSpeed) {
30         this.maxSpeed = maxSpeed;
31     }
32 
33     @Override
34     public String toString() {
35         return "Car [brand=" + brand + ", maxSpeed=" + maxSpeed + "]";
36     }
37 
38 }

 

. 1 class CloneTest { 
 2 
 . 3 public static void main (String [] args) { 
 . 4 the try { 
 . 5 the Person P1 = new new the Person ( "Guo Jing", 33 is, new new Car ( "Benz", 300)); 
 . 6 the Person P2 = MyUtil. clone (p1); // a deep clone 
 . 7 p2.getCar () setBrand ( "BYD");. 
 . 8 // modified clone p2 car object associated with the Person object brand attributes 
 9 // p1 Person object associated with the original car will not be affected 
10 // Person object because its associated cloning vehicle object has also been cloned 
. 11 System.out.println (P1); 
12 is} the catch (Exception E) { 
13 is e.printStackTrace (); 
14} 
15} 
16}

 

Note: Based on cloning serialization and de-serialization to achieve not only the depth of cloning, more importantly, is limited by generics, you can check out if you want to clone objects support serialization, this check is complete compiler, not in Throws abnormal operation, this scheme is much better than the method using the clone clone Object class. Let problems at compile time exposed always better to leave the matter to the runtime.

 

63. What is the deep and shallow copy copy difference is?

  • Shallow copy just copied object reference address, two objects point to the same memory address, wherein modifying an arbitrary value, another value will also change, which is a shallow copy (Example: assign ())

  • Deep copy is a copy target value and over, two objects modify any value other values ​​do not change, which is a deep copy (Example: JSON.parse () and the JSON.stringify (), but this method can not be replicated function Types of)

Guess you like

Origin www.cnblogs.com/donleo123/p/11621039.html