How to enter a big factory if the foundation is not reliable? These basic Java questions are recommended for collection!

Preface

In the cold winter, I can't stop a Java programmer from pursuing a big factory. The current interviews with a big factory, especially one and two sides, involve deep Java basic knowledge, so the editor summarizes some big factory frequently asked questions. Java basic interview questions and answers, share with you, I hope you can enter your ideal company as soon as possible~~
Insert picture description here

1. What is the difference between JDK and JRE?

  • JDK: The abbreviation of Java Development Kit, java development kit, provides a java development environment and operating environment.
  • JRE: short for Java Runtime Environment, java runtime environment, which provides the required environment for the operation of java.

Specifically, JDK actually includes JRE, as well as javac, a compiler for compiling java source code, as well as many tools for debugging and analyzing java programs. Simply put: if you need to run a java program, you only need to install the JRE, if you need to write a java program, you need to install the JDK.

2. What is the difference between == and equals?

1. == Interpretation
The effect of == for basic types and reference types is different, as shown below:

  • Basic type: compare whether the values ​​are the same;
  • Reference type: compare whether the references are the same;

Code example:

String x = "string";
String y = "string";
String z = new String("string");
System.out.println(x==y); // true
System.out.println(x==z); // false
System.out.println(x.equals(y)); // true
System.out.println(x.equals(z)); // true

Code interpretation: Because x and y point to the same reference, == is also true, and the new String() method is rewritten to open up memory space, so the result of == is false, and the comparison of equals is always a value, so The results are all true.

Two, equals interpretation

Equals is essentially ==, but String and Integer have rewritten the equals method, turning it into a value comparison. Look at the following code to understand.
First, let’s look at equals comparing an object with the same value by default. The code is as follows:

class Cat {
    
    
    public Cat(String name) {
    
    
        this.name = name;
    }

    private String name;

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }
}

Cat c1 = new Cat("王磊");
Cat c2 = new Cat("王磊");
System.out.println(c1.equals(c2)); // false

The output is beyond our expectations, it turns out to be false? What is going on, you can see the source code of equals. The source code is as follows:

public boolean equals(Object obj){
    
    
        return (this == obj);
}

It turns out that equals is essentially ==.
The question is, why do two String objects with the same value return true? code show as below:

String s1 = new String("老王");
String s2 = new String("老王");
System.out.println(s1.equals(s2)); // true

Similarly, when we entered the equals method of String and found the answer, the code is as follows:

public boolean equals(Object anObject) {
    
    
    if (this == anObject) {
    
    
        return true;
    }
    if (anObject instanceof String) {
    
    
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
    
    
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
    
    
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

It turns out that String rewrites the equals method of Object, changing reference comparison to value comparison.

Summary: == for basic types is value comparison, for reference types, it is reference comparison; and equals by default is reference comparison, but many classes override the equals method, such as String, Integer, etc. to change it It becomes a value comparison, so in general, equals compares whether the values ​​are equal.

3. If the hashCode() of two objects is the same, equals() must also be true, right?

No, the hashCode() of the two objects is the same, equals() is not necessarily true.
Code example:

String str1 = "通话";
String str2 = "重地";
System.out.println(String.format("str1:%d | str2:%d",  str1.hashCode(),str2.hashCode()));
System.out.println(str1.equals(str2));

The result of execution:

str1:1179395 | str2:1179395
false

**Code interpretation:** Obviously the hashCode() of "call" and "heavy ground" are the same, but equals() is false, because in the hash table, hashCode() is equal to the hash value of the two key-value pairs Equal, but the hash value is equal, and the key-value pair is not necessarily equal.

4. What is the role of final in java?

  • The final modified class is called the final class, which cannot be inherited.
  • Final modified methods cannot be overridden.
  • The final modified variable is called a constant. The constant must be initialized, and the value cannot be modified after initialization.

5. How much is Math.round(-1.5) in java?

Equal to -1.

6. Is String a basic data type?

String is not a basic type, there are 8 basic types: byte, boolean, char, short, int, float, long, double, and String is an object.

7. What are the classes of manipulation strings in java? What is the difference between them?

The classes for manipulating strings are: String, StringBuffer, StringBuilder.

The difference between String and StringBuffer and StringBuilder is that String declares an immutable object. Each operation generates a new String object, and then points the pointer to the new String object. StringBuffer and StringBuilder can operate on the basis of the original object. Therefore, it is best not to use String when the content of a string is frequently changed.

The biggest difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe, while StringBuilder is non-thread-safe, but StringBuilder's performance is higher than StringBuffer, so StringBuilder is recommended in a single-threaded environment, and StringBuffer is recommended in a multi-threaded environment.

8. Is String str="i" the same as String str=new String("i")?

Not the same, because the memory allocation method is different. With String str="i", the java virtual machine will allocate it to the constant pool; and String str=new String("i") will be allocated to the heap memory.

9. How to reverse the string?

Use the reverse() method of StringBuilder or stringBuffer.
Sample code:

// StringBuffer reverse
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("abcdefg");
System.out.println(stringBuffer.reverse()); // gfedcba
// StringBuilder reverse
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("abcdefg");
System.out.println(stringBuilder.reverse()); // gfedcba

10. What are the commonly used methods of the String class?

  • indexOf(): Returns the index of the specified character.
  • charAt(): Returns the character at the specified index.
  • replace(): string replacement.
  • trim(): Remove blanks at both ends of the string.
  • split(): Split the string and return a split string array.
  • getBytes(): Returns the byte type array of the string.
  • length(): Returns the length of the string.
  • toLowerCase(): Convert the string to lowercase letters.
  • toUpperCase(): Convert a string to uppercase characters.
  • substring(): intercept a string.
  • equals(): String comparison.

11. Does an abstract class have to have abstract methods?

No, abstract classes do not have to have abstract methods.
Sample code

abstract class Cat {
    
    
    public static void sayHi() {
    
    
        System.out.println("hi~");
    }
}

In the above code, the abstract class has no abstract methods but can run normally.

12. What are the differences between ordinary classes and abstract classes?

  • Ordinary classes cannot contain abstract methods, and abstract classes can contain abstract methods.
  • Abstract classes cannot be instantiated directly, ordinary classes can be instantiated directly.

13. Can abstract classes use final modification?

No, the definition of an abstract class is for other classes to inherit. If it is defined as final, the class cannot be inherited. This will cause conflicts with each other. Therefore, final cannot modify the abstract class. As shown in the figure below, the editor will also prompt an error message:
Modification abnormal

14. What is the difference between an interface and an abstract class?

  • Implementation: Subclasses of abstract classes use extends to inherit; interfaces must use implements to implement interfaces.
  • Constructor: Abstract classes can have constructors; interfaces cannot.
  • Main method: An abstract class can have a main method, and we can run it; an interface cannot have a main method.
  • Number of implementations: A class can implement many interfaces; but can only inherit one abstract class.
  • Access modifier: The methods in the interface use public modification by default; the methods in the abstract class can be any access modifier.

15. What are the types of IO streams in java?

Divided by function: input stream (input), output stream (output).
Divided by type: byte stream and character stream.
The difference between byte stream and character stream is: byte stream transfers input and output data in bytes according to 8 bits, and character stream transfers input and output data in characters according to 16 bits.

16. What is the difference between BIO, NIO and AIO?

  • BIO: Block IO Synchronous blocking IO is the traditional IO we usually use. It is characterized by its simple mode and convenient use, and low concurrent processing capability.
  • NIO: New IO synchronous non-blocking IO is an upgrade of traditional IO. The client and server communicate through Channel to achieve multiplexing.
  • AIO: Asynchronous IO is an upgrade of NIO, also called NIO2, which implements asynchronous non-blocking IO. The operation of asynchronous IO is based on events and callback mechanisms.

17. What are the common methods of Files?

  • Files.exists(): Check whether the file path exists.
  • Files.createFile(): Create a file.
  • Files.createDirectory(): Create a folder.
  • Files.delete(): Delete a file or directory.
  • Files.copy(): Copy files.
  • Files.move(): Move files.
  • Files.size(): View the number of files.
  • Files.read(): Read files.
  • Files.write(): Write files.

to sum up

Due to space reasons, I will not show all of them here.
The editor also sorted out most of the interview questions and answers involved in the interview with Dachang java programmers to share with you for free, hoping to help everyone, friends in need can see the free way to get them below !

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ Get
information here: password CSDN

Insert picture description here
Insert picture description here

To get the information, click here: CSDN
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

Finally, thank you for your support, and hope that the information compiled by the editor can help you! I also wish everyone a promotion and salary increase!
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_47955802/article/details/109341188