search
HomeJavajavaTutorialInterview questions in Java: Solutions to three classic questions

Interview questions in Java: Solutions to three classic questions

Jun 15, 2023 pm 08:43 PM
java interviewClassic solutionoop thought

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)