1. In-depth understanding of class and its methods
To obtain the class class method:
1. Call the GetClass () method of the object class to get the class object, which is also the most common way to produce a class object.
2. Use the class class's Static Forname () method to get the class object corresponding to the string. For example:
Class C2=class.forname ("MyObject") //MyObject must be the name of the interface or class
3. The third method of getting class type objects is very simple. If T is a Java type, then T.class represents the matching class object. For example
Class CL1 = Manager.class; Class Cl2 = Int.class; Note that this class Cl3 = Double[].class;
Note: The class object actually describes only the type, and this type is not necessarily a class or interface. For example, the above int.class is an object of type class. For historical reasons, the GetName method of the array type returns a strange name.
GetClass ()
Clone ()
Hashcode ()
Equals ()
Notify (), Notifyall (), Wait ()
Finalize () is called on the object before the garbage collector deletes the object.
ToString ()
There are countless minor problems with these issues, as follows:
1, why to overwrite the Hashcode () method, the default is not?
2, deep cloning and shallow cloning?
3. When do I need to rewrite equals ()?
4. How to overwrite Equals () and hashcode ()?
5. When rewriting equals (), always rewrite hashcode ()
6, two objects if equals then the hashcode of the two objects must be equal, if the hashcode of the two objects are equal then are the two objects necessarily equals?
2. How do I determine the object type at run time?
(1) Using the instance of keywords However, there is a disadvantage that the class inherits the base class and the implemented interface can also be recognized as true
(2) Each class will have and have only one object corresponding to class, and we are using this class object to instantiate all objects of that class, that is, the newInstance()
method in class. You can compare this class object by comparing the following:
Object i = new Integer (7); if (I.getclass (). Equals (Integer.class)) { System.out.println ("Integer i is a integer"); } if (i.getclass () = = Integer.class) { System.out.println ("Integer i is a integer"); } if (Integer.class.isInstance (i)) { System.out.println ("Integer i is a integer"); }
The name can also be output directly from the GetClass () method's GetName ().
3, about random number generation of class random
public int Nextint (int n)
The function of this method is to generate a random int value that is in the interval of [0,n], which is a random int value between 0 and N, containing 0 without n.
4. What's wrong with SimpleDateFormat access in a high-concurrency environment?
There are multiple threading issues, which can cause exceptions, thread hangs, and so on, mainly due to resource contention using member variables.
The solution is to use threadlocal.
Java Interview 08| Java important classes and related methods