Java Programmer's written interview Java Basics

Source: Internet
Author: User
Tags finally block

The difference between 1.== and equals and hashcode

①== are used to compare values for equality, or to compare references for equality, that is, whether the comparison points to the same object.

The ②equals method is the method provided by object, so each object has the Equals method, and his implementation is implemented by the = = operator, so the Equals method is exactly the same as = = without overwriting the Equals () method.

③ compared to the = = operator The Equals method is special in that he can be overridden, such as String, that he can compare the contents of 2 separate objects for equality, for example:

String S1=new string ("HelloWorld");

String S2=new string ("HelloWorld");

Using S1==S2 will return false because the comparison refers to S1 with S2 pointing to different objects so return false

Using S1.equals (S2) will return true because the contents of the object are compared to HelloWorld

The ④hashcode () method is also extends the object class, and he is also used to determine whether 2 objects are equal, and the Hashcode () method returns an int value that is converted to an object's address in memory, so before the Hashcode () method is overridden, The Hashcode () method of any object is not equal


Usage of 2.this and super

①this keyword refers to the current instance object, he has a very important application is to distinguish the object's member variables and formal parameters.

The ②super keyword can be used to access the methods and member variables of the parent class, if the child class has a method or member variable with the same name as the parent class, and the method or member variable of the subclass overrides the parent class's method or member variable, if you want to use the Super keyword for the parent class's method or member variable.

③ when a subclass constructor needs to display a constructor that calls the parent class, super () must be placed in the first sentence of the constructor


When is the code in the 3.findally block executed?

In the Java language exception handling, the function of finally is to ensure that the code in the finally block is bound to be executed, when there is a return in try{} or catch{}, Because return means that all code must be executed before the return, the code in all the finally blocks will be executed before the return, unless the exit () function is encountered, and when the try and catch and finally have return The return in finally will overwrite the other return


Question: Is the code that appears in the finally block in a Java program bound to be executed?

Not necessarily. 1. When the program enters try{}, the exception will not be executed, such as i=5/0 in front of try{}, the direct end

2. When force exits in try{} block such as System.exit (0); This direct force exit will not execute finally



4. What is the principle of exception handling

Java exception handling refers to abnormal conditions or errors that occur when a Java program is run (not at compile time), and the JVM represents an exception and throws the error that occurs. This exception can be captured in the catch{} block and processed, and the purpose of exception handling is to improve the robustness and security of the program.

The Java language defines exceptions as object processing, and provides a base class thowable, All exceptions are subclasses of java.lang.Thowable, and many exception classes have been defined in the Java API, which can be divided into error and Exception2 classes


5. Differences between runtime exceptions and common exceptions




Java Programmer's written interview Java Basics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.