java interview guide 2019-9-10

11. Java object-oriented programming three characteristics: a package inheritance polymorphism

Package

Package the properties of an object privatization, while providing some of the ways property can be accessed by the outside world, if the property is access to the outside world do not want, we need not provide a method to access the outside world. However, if a class does not provide a method for the outside world to access, then this class have no meaning.

inherit

Inheritance is defined using existing classes as a basis for the establishment of a new class of technology, the definition of the new class can add new data or new functions may be functions of the parent class, but can not selectively inherited parent class. By using inheritance we can easily reuse the previous code.

About 3:00 Remember succession as follows:

  1. Subclass has all of the parent class object properties and methods (including private property and private methods), but the parent class and subclass of private property is not accessible, just ownership.
  2. Subclasses can have their own attributes and methods that a subclass can extend the parent class.
  3. Subclass the parent class can implement in their own way. (Introduced later).

Polymorphism

Polymorphism refers to the so-called method of the particular type of reference variables defined in the program pointed to by the reference variable and calls made during programming is not determined, but is determined only during the program run, i.e. a reference variable which points to the class in the end examples of objects that reference variable method calls made in the end is which class method implemented, in order to be determined by the program is running.

There are two polymorphic forms may be implemented in Java: Inheritance (s Subclasses override the same method) and an interface (an interface and the interface to achieve the same coverage method).

What is the difference 12. String StringBuffer and StringBuilder is? String Why is immutable?

Variability

Simply put: String class using the keyword final modification of the character array to hold the strings, private final char value[]so String objects are immutable. The StringBuilder and StringBuffer inherit from AbstractStringBuilder class, also using the character in a string array to hold the AbstractStringBuilder char[]value but not modified by the final keyword, so these two objects are variable.

StringBuilder and StringBuffer constructor is calling the parent class constructor is AbstractStringBuilder achieve, we can review the source code itself.

abstract class AbstractStringBuilder implements Appendable, CharSequence {
    char[] value;
    int count;
    AbstractStringBuilder() {
    }
    AbstractStringBuilder(int capacity) {
        value = new char[capacity];
    }

 

Thread safety

The String object is immutable, it can be understood as a constant thread safe. AbstractStringBuilder StringBuilder and StringBuffer is common parent class defines the basic operation of some of the string, such as expandCapacity, append, insert, indexOf other public methods. StringBuffer method adds genlock or method call added a synchronization lock, so are thread-safe. StringBuilder and no method plus genlock, so not thread-safe. 

performance

Every time a change of type String, generates a new String object, then the pointer to the new String object. StringBuffer StringBuffer object itself every time to operate, instead of generating new object and change the object reference. Use StringBuilder StringBuffer compared to using only get about 10% to 15% performance increase under the same circumstances, but they bear the risk of multithreading unsafe.

For the use of the three summary:

  1. Operating a small amount of data: For String
  2. Operation single-threaded operating string buffer large amounts of data: for StringBuilder
  3. Operating a multi-threaded operating string buffer large amounts of data: applicable StringBuffer

26. == and equals (important)

==: its role is to determine whether two objects address is not equal . That is, it is determined the objects are not the same object (primitive data type is a value == comparison, the comparison reference data type == memory address).

equals (): its role is to determine whether two objects are equal. But it is generally used in two cases:

  • Case 1: No cover class equals () method. Through equals () comparing two objects of the class, is equivalent to the "==" to compare the two objects.
  • Case 2: class overrides equals () method. Generally, we cover the equals () method to compare the contents of two objects are equal; if their contents are equal, returns true (that is, the two objects are considered equal).

Description:

  • String equals method is to be rewritten, because the object is the equals method to compare the memory address of the object, and the equals method of comparison is a value String object.
  • When creating an object of type String, the virtual find same value objects have no value already exists and you want to create in the constant pool, if you have put it assigned to the current reference. If not re-create a String object in the constant pool.

27. hashCode and equals (important)

The interviewer may ask you: "You overridden hashcode and equals it, you must override hashCode method Why rewrite equals?"

hashCode () Introduction

Action hashCode () is to obtain the hash code, also called a hash code; it actually returns an int integer. The role of this hash code is to determine the location of the object in the hash index table. hashCode () is defined in the JDK Object.java, this means that any Java class contains a hashCode () function.

Hash table is stored in key-value pair (key-value), which is characterized by: to "value" in accordance with "key" corresponding to the retrieved quickly. This one on the use of the hash code! (I can quickly find the object needed)

Why should hashCode

We first "HashSet how to check for duplicate" as an example to illustrate why we need hashCode: When you put an object join HashSet, HashSet will first calculate the hashcode value of the object to determine the position of the object to join, but also has joined with other objects the hashcode value for comparison, if there is no match of hashcode, HashSet assumes that the object is not repeated. But if the object has the same hashcode value found, then it calls the  equals()method to check whether the object really equal hashcode same. If they are the same, HashSet will not be allowed to join the operation was successful. If different, it would re-hash to another location. . So we greatly reduce the number of equals, and accordingly will greatly improve the speed of execution.

As can be seen by us: hashCode() the role is to get the hash code, also called a hash code; it is actually returns an int integer. The role of this hash code is to determine the location of the object in the hash index table. hashCode()Be useful in the hash table, in other cases useless. Action hashCode () is acquired in the hash table is a hash code of the object, and to determine the location of the object in the hash table.

hashCode () and equals () the relevant provisions

  1. If two objects are equal, it must also be the same hashcode
  2. Equal two objects, two objects were to call the equals method returns true
  3. Hashcode two objects have the same value, they are not necessarily equal
  4. Thus, equals overridden the method, the method must be covered hashCode
  5. The default behavior hashCode () is to create a unique value to objects on the heap. If no override hashCode (), then the two objects are not equal class anyway (even if these two objects point to the same data)

Recommended reading: Issues Java hashCode () and equals () answers

30. What are the basic state of the thread there?

 

 It can be seen from FIG:

After the thread is created it will be in NEW (New) state, calls  start() began to run after the method, this time the thread is READY (run) state. Threads running state can be obtained cpu time slice (TimeSlice) is after the RUNNING (running) state.

When the thread execution  wait()after the method, the thread enters WAITING (waiting) state. The thread into a wait state need to rely on other threads notice to be able to return to operational status, and TIME_WAITING (timeout wait) state the equivalent of adding wait states on the basis of the timeout limit on, for example by  sleep(long millis)the method or  wait(long millis)Java thread can be placed TIMED method WAITING state. After the timeout arrives Java thread will return to RUNNABLE state. When a thread calls a synchronization method, in the absence of acquired lock, the thread will enter into BLOCKED (blocked) state. Runnable threads executing the run()following method will enter into TERMINATED (terminated) state.

31 some conclusions about the final keyword

final keyword is mainly used in three places: variables, methods and classes.

  1. For a final variable, if the basic data types of variables, then its value can not be changed once after the initialization; if it is a reference type variable, then after its initialization will not allow its point to another object.
  2. When modifying a class with a final, indicating that this class can not be inherited. All members of the final class methods will be implicitly designated as the final method.

33 Java serialization if some field do not want to be serialized, how do?

Who do not want to serialize variables, using the keyword transient modification.

the role of transient keyword is: to prevent instances of those variables with this keyword modified serialization; when the object is deserialized is transient modified variable values are not persisted and recovery. transient can only modify variables, classes, and methods can not be modified.

34 static keyword

There are four main static keyword usage scenarios:

  1. Member variables and member methods modifications: the modified static members belong to the class, not part of a single class of objects, is shared by all objects in the class, and can be recommended by the class name calling. Static member variables are declared in a static member variables, static variables stored in the memory area of ​​Java method area. Call format:类名.静态变量名 类名.静态方法名()
  2. Static block of code: static block of code is defined in an outer class methods, static block of code (static block of code -> non-static block -> Constructor) before the non-static block. No matter how many objects the class is created, static code block is executed only once.
  3. Static inner class (static modification that kind of thing can only be modified inside the class): There is a difference between the maximum static inner class with non-static inner classes: non-static inner classes after the completion of the translation will implicitly holds a reference that It points to create its enclosing class, but static inner classes do not. Without this reference means that: 1. It does not require the creation of dependence created enclosing class. 2. It can not use any non-static member variables and methods of the enclosing class.
  4. Static guide package (used to import class static resources, new features after 1.5): The format is: import static These two keywords can be used in conjunction with a designated import the specified static resource class, and does not require the use of the class name to call the class static member, you can use the class static member variables and member methods directly.

 

Guess you like

Origin www.cnblogs.com/ustc-anmin/p/11502518.html