Java Technology Stack - Interview Essentials (Java Basics)

Java basic knowledge base:

1. What is the difference between JDK and JRE?

1) JDK: The abbreviation of Java Development Kit, the java development kit, provides the development environment and operating environment of java.

2) JRE: Short for Java Runtime Environment, the java runtime environment provides the required environment for the running of java.

Specifically, the JDK actually includes the JRE, the compiler javac that compiles the java source code, and many tools for debugging and analyzing java programs. To put it simply: if you need to run java programs, you only need to install JRE, and if you need to write java programs, you need to install JDK.

2. The difference between i++ and ++i

1) i++: prefix self-increment and self-subtraction method, first perform self-increment or self-decrement operation, and then perform expression operation;

2) ++i: suffix self-increment and self-subtraction, the expression operation is performed first, and then the self-increment or self-decrement operation is performed.

3. The function of break statement and continue statement

1) The Break statement will interrupt the entire loop body, jump out of the loop, and execute subsequent statements.

2) The Continue statement will end this cycle, return to the beginning of the cycle, and start a new cycle.

4. Implement a simple process of executing a task in a separate thread

1) Move the task code to the run method of the class that implements the Runnable interface, because Runnable

If it is a functional interface, lambda expressions can be used; Runnable r=()->{task code};

2)Thread t=new Thread(r);

3)t.start();

5. Start an external program in a Java program

String cmd = "The directory where the external program is located";

Process process=Runtime.getRuntime().exec(cmd); Use the Runtime class to execute and start an external program

6. The difference between thread interrupted and isInterrupted 

1) The interrupted method is a static method that detects whether the current thread is interrupted, and calling the interrupted method will clear the interrupted status of the thread.

2) The isInterrupted method is an instance method that can be used to detect whether a thread is interrupted. Calling this method will not change the interrupted state of the thread.

7. 6 states of threads

1) New newly created

2) Runnable can run

3) Blocked is blocked

4) Waiting

5) Timed waiting Timed waiting

6) Terminated

8. To determine the current state of a thread you should call

To determine the current state of a thread, call t.getState();

9. How to print all the values ​​in the array

Use the toString method of the Arrays class to print all the values ​​in the array.

example:

Int[ ] a=new Int[6];

System.out.println(Arrays.toString(a));

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

== For basic types, it is a value comparison, for reference types, it is a reference comparison; and equals is a reference comparison by default, but many classes re-equals methods, such as String, Integer, etc., turn it into a value Comparison, so in general, equals compares whether the values ​​are equal.

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

No, the hashCode() of two objects is the same, equals() is not necessarily true; if equals() is the same, then hashCode() must be the same.

12. What is the role of final in java?

1) The final modified class is called the final class, which cannot be inherited.

2) Final modified methods cannot be overridden.

3) Variables modified by final are called constants. Constants must be initialized, and the value cannot be modified after initialization.

13.  What is Math.round(-1.5) equal to in java?

It is equal to -1, because when taking values ​​on the number axis, the middle value (0.5) is rounded to the right, so positive 0.5 is rounded up, and negative 0.5 is discarded directly.

14. 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.

15. What classes are there for manipulating strings in java? What's the difference between them?

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

2) The difference between String and StringBuffer and StringBuilder is that String declares an immutable object, each operation will generate a new String object, and then point the pointer to the new String object, while StringBuffer and StringBuilder can be created on the basis of the original object Operation, so it is best not to use String when the content of the string is often changed. String can be used for a small number of string operations, and StringBuffer and StringBuilder can be used for a large number of strings.

3) The biggest difference between StringBuffer and StringBuilder is that StringBuffer is thread-safe, while StringBuilder is not thread-safe, but the performance of StringBuilder is higher than that of StringBuffer, so it is recommended to use StringBuilder in a single-threaded environment, and it is recommended to use StringBuffer in a multi-threaded environment.

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

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

17. How to reverse a 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

18. 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(): Removes blanks at both ends of the string.

  • split(): splits the string and returns 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 the string to uppercase characters.

  • substring(): Intercepts a string.

  • equals(): String comparison.

 19. Do abstract classes have to have abstract methods?

No, abstract classes don't have to have abstract methods;

Sample code:


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

20. What is the difference between ordinary class and abstract class?

  • Ordinary classes cannot contain abstract methods, abstract classes can contain abstract methods.

  • Abstract classes cannot be instantiated directly, ordinary classes can be instantiated directly.

21.  Can an abstract class be modified with final?

No, defining an abstract class is to allow other classes to inherit. If it is defined as final, the class cannot be inherited, which will cause conflicts with each other, so final cannot modify the abstract class. As shown in the figure below, the editor will also prompt an error message:

22.  What is the difference between interface and abstract class?

1) Implementation: Subclasses of abstract classes use extends to inherit; interfaces must use implements to implement interfaces.

2) Constructor: Abstract classes can have constructors; interfaces cannot.

3) main method: abstract class can have main method, and we can run it; interface cannot have main method.

4) Number of implementations: A class can implement many interfaces; but it can only inherit one abstract class.

5) Access modifiers: methods in interfaces are modified by public by default; methods in abstract classes can be any access modifiers.

6) The abstract class emphasizes relational affiliation, and the interface emphasizes function realization.

7) Subclasses that inherit abstract classes can selectively implement the abstract methods of the parent class; while classes that implement interfaces need to implement all methods of the interface.

23.  How many types of IO streams are there in java?

Divided by function: input stream (input), output stream (output).

Divided by type: byte stream and character stream.

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

1) BIO: Block IO Synchronous blocking IO is the traditional IO we usually use. It is characterized by a simple mode, easy to use, and low concurrent processing capability.

2) NIO: New IO synchronous non-blocking IO is an upgrade of traditional IO. The client and server communicate through Channel (channel) to realize multiplexing.

3) AIO: Asynchronous IO is an upgrade of NIO, also called NIO2, which realizes asynchronous non-blocking IO, and the operation of asynchronous IO is based on event and callback mechanism.

25.  What are the common methods of Files?

1) Files.exists(): Check whether the file path exists.

2) Files.createFile(): Create a file.

3) Files.createDirectory(): Create a folder.

4) Files.delete(): Delete a file or directory.

5) Files.copy(): Copy files.

6) Files.move(): Move files.

7) Files.size(): View the number of files.

8) Files.read(): read files.

9) Files.write(): Write files.

26. What is the mechanism of string storage?

Strings are stored in the constant pool. When assigning a value to a string, the JVM will check whether the string already exists in the constant pool, and if it exists, directly refer to the address, otherwise it will create the string in the constant pool and then refer to the address.

27. What is the difference between passing by value and passing by reference?

1) Passing by value: In a method call, the actual parameter will pass its value to the formal parameter, and the formal parameter is just to initialize a temporary with the value of the actual parameter

The storage unit (local variable in the method), so although the participating actual parameters have the same value, but have different

Storage location, so changes to formal parameters do not affect the value of the actual parameter.

2) Pass by reference: In the call of the method, what is passed is

Object (also can be regarded as the address of the object), at this time the formal participator and the actual parameter point to the same storage unit (object), so for

The modification of the formal parameter will affect the value of the actual parameter.

28. What is the function of Instanceof ?

The function of Instanceof is to determine whether the object pointed to by a variable of reference type is an instance of a class.

29. The similarities and differences between super and this ?

difference:

1) super() is mainly a call to the parent class constructor, this() is a call to the overloaded constructor

2) super() is mainly used in the constructor of the subclass that inherits the parent class, and is used in different classes; this() is mainly used in different constructors of the same class

Same point:

1) Both super() and this() must be called on the first line of the constructor, otherwise it is wrong

30. How do constructors work?

The order in which Java constructs instances is as follows:

1) Allocate object space and initialize the members in the object to 0 or empty. Java does not allow users to manipulate an object with an indeterminate value.

2) Perform explicit initialization of property values

3) Execute the constructor

4) Associate variables with objects in the heap

31. Can the Constructor be overridden?

The constructor Constructor cannot be inherited, so Override cannot be overridden, but Overload can be overloaded.

Constructor cannot be inherited, so Constructor cannot be overridden. Each class must have its own constructor

Number, responsible for constructing the structure of this part of itself. Subclasses do not override parent class constructors, but instead must be responsible for invoking parent

class constructor.

32. If there is a return statement in the try {} , will the code in the finally {} following the try be deleted?

Execution, when is it executed, before or after return ?

It will be executed. After the return in try{} is executed, if no data is returned, the code in finally{} will be executed first, and then return.

33. What are the three basic characteristics of a class? The advantages of each feature?

1) Class has encapsulation, inheritance and polymorphism.

2) Encapsulation: The encapsulation of a class provides public, default, protected and private access rights for members of the class, the purpose is to hide the

The private variables and implementation details of the methods in the class.

3) Inheritance: It is allowed to generate a new class by inheriting some or all of the characteristics of the original class. The original class is called the parent class.

The resulting new class is called a subclass. Subclasses can not only directly inherit the commonality of the parent class, but also create its unique personality.

4) Polymorphism: It means that after the properties and methods defined in the base class are inherited by subclasses, they can have different data types or performances

There are two forms of polymorphism: overloading and overriding.

 34. Create a file with a specified path and verify that the path has the specified file

File file = new File(pathName); file.createNewFile(); file.exists() returns boolean to verify that the path exists

Specify the file.

35. How to convert an array into a List collection

1) Array tool class Arrays provides a static method asList, which can convert an array into a List collection.

2)List<String> list=Arrays.asList(array)。

3) The List collection converted from the array cannot add or delete elements.

36. How to get an array from a collection

1) Use the toArray method

2) String[ ] values=collection object.toArray(new String[x])

37. How is equality of objects different from equality of references pointing to them?

The equality of objects generally refers to the content contained in the object itself is equal, and the equality of references to objects refers to the same first address pointing to the object, so the two are essentially different.

Guess you like

Origin blog.csdn.net/qq_43780761/article/details/127115817