Home  >  Article  >  Java  >  Interview questions in Java: Solutions to three classic questions

Interview questions in Java: Solutions to three classic questions

PHPz
PHPzOriginal
2023-06-15 20:43:29978browse

As a popular programming language, Java’s interview questions have also attracted much attention. Let's take a look at three classic Java interview questions and their answers.

1. How is String determined to be equal?

In Java, String is a special object, so its equality judgment also requires special attention. Generally speaking, there are two ways to judge String equality.

  1. Use the equals method

In Java, you can use the equals method to determine whether two String objects are equal. This method compares the strings contained in the two String objects to see if they are the same. If they are the same, it returns true; if they are different, it returns false.

For example:

String str1 = "Hello";
String str2 = "Hello";

if(str1.equals(str2)){
    System.out.println("str1和str2相等");
} else {
    System.out.println("str1和str2不相等");
}

The output result is: str1 and str2 are equal.

It should be noted that when using the equals method to compare strings for equality, what is compared is whether the values ​​of the strings are equal, not whether the memory addresses are equal.

  1. Use the == operator

You can also use the == operator in Java to determine whether two String objects are equal. However, it should be noted that when using the == operator to compare two objects, what is compared is whether the memory addresses of the two objects are equal, not whether the string contents are equal.

For example:

String str1 = "Hello";
String str2 = "Hello";

if(str1 == str2){
    System.out.println("str1和str2地址相等");
} else {
    System.out.println("str1和str2地址不相等");
}

The output result is: str1 and str2 addresses are equal.

It should be noted that when assigning a String object, the string pool in Java will automatically create a new string object, so you need to pay special attention when using the == operator to compare strings. .

2. What is Final in Java?

In Java, the final keyword can be used to modify classes, methods, and variables. Let's explain the functions of the final keyword separately.

  1. Final modified class

If a class is modified with final, it means that the class cannot be inherited. For example:

public final class MyClass{
    //...
}
  1. final modified method

If a method is modified with final, it means that this method cannot be overridden by subclasses. For example:

public class MyClass{
    public final void myMethod(){
        //...
    }
}
  1. final modified variable

If a variable is modified with final, it means that the variable cannot be reassigned. For example:

public class MyClass{
    public final int MY_CONSTANT = 10;
}

It should be noted that variables modified by the final keyword must be initialized and assigned when declared.

3. What is polymorphism in Java?

In Java, polymorphism means that the same interface can implement different objects. Specifically, there are two forms of polymorphism:

  1. Compile-time polymorphism

Compile-time polymorphism refers to calling a subclass through a reference to the parent class type Methods of objects of type. For example:

Parent parent = new Child();
parent.myMethod();

In this example, parent is a reference of the parent class type, but it calls the myMethod method in the object of the subclass type.

It should be noted that compile-time polymorphism can only call the parent class and methods in the parent class, but cannot call methods unique to the subclass.

  1. Run-time polymorphism

Run-time polymorphism refers to calling the corresponding method according to the type of the actual object during the running of the program. For example:

public class MyClass{
    public static void main(String[] args){
        Animal animal = new Dog();
        animal.makeSound();
    }
}

class Animal{
    public void makeSound(){
        System.out.println("动物发出声音");
    }
}

class Dog extends Animal{
    public void makeSound(){
        System.out.println("汪汪汪");
    }
}

In this example, animal is a reference of Animal type, but it points to an object of Dog type, so when the makeSound method is called, the makeSound method in the Dog type object is actually called. , the output result is also "woof woof woof".

It should be noted that runtime polymorphism can only determine the method called at runtime, so runtime errors may occur.

To sum up, the String equality judgment, final keyword and polymorphism in Java are questions that are often asked in interviews. Programmers need to have an in-depth understanding and mastery of these questions.

The above is the detailed content of Interview questions in Java: Solutions to three classic questions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn