Java basics for Java programmer test interview
1. ==difference between equals and hashcode
① = Is used to compare whether a value is equal, or to compare whether the reference is equal, that is, to compare whether it points to the same object.
② The equals method is the method provided by the Object. Therefore, each Object has the equals method and its implementation is also implemented through the = Operator. Therefore, without overwriting the equals () method, the equals method is exactly the same as the = method.
③ The special feature of the = operator equals method is that it can be overwritten. For example, String can be used to compare the content of two independent objects. For example:
String s1 = new String ("helloworld ");
String s2 = new String ("helloworld ");
If s1 = s2 is used, false is returned. Because s1 and s2 are referenced to different objects, false is returned.
S1.equals (s2) is used to return true because the object content is compared to helloworld.
④ The hashcode () method is also of the extends Object class. It is also used to determine whether two objects are equal. The hashcode () method returns an int value converted from the address of the Object in the memory, so before the hashcode () method is overwritten, The hashcode () Methods of any object are not equal.
2. usage of this and super
① This keyword refers to the current instance object. An important application of this keyword is to differentiate the member variables and formal parameters of the object.
② The super keyword can be used to access the methods and member variables of the parent class, if the subclass has a method or member variable with the same name as the parent class, the method or member variable of the subclass will overwrite the method or member variable of the parent class, at this time, if you want to use the parent class method or member variables, you must use the super keyword.
③ When the subclass constructor needs to display the constructor that calls the parent class, super () must be placed in the first sentence of the constructor.
3. When will the code in the findally block be executed?
In java Exception Handling, finally is used to ensure that the code in the finally block will be executed under any circumstances. When try {} or catch {} has return, because return means that all code must be executed before return, the Code in all finally blocks will be executed before return unless the exit () function is met, when both try and catch and finally have return, return in finally overwrites other return values.
Q: Will the code in the finally block in a java program be executed?
Not necessarily. 1. if an exception occurs before the program enters try {}, it will not be executed. For example, if I = 5/0 is before try {}, it will end directly.
2. When a force exit occurs in the try {} block, for example, System. exit (0). If a force exit occurs, finally is not executed.
4. What is the principle of exception handling?
Java Exception Handling refers to the exception or error that occurs when the java program is running (not during compilation). JVM will indicate the error as an exception and throw it. This exception can be captured in the catch {} block and then processed. The purpose of exception handling is to improve the robustness and security of the program.
Java defines exceptions as object processing and provides a base class Thowable. All exceptions are java. lang. classes of Thowable have defined many exception classes in java APIs. These exception classes can be divided into Error and exception2.
5. Differences between runtime exceptions and Common exceptions