100 JAVA interview questions (sorted out by hand. Note: You don’t have to answer all of them in the interview, you only need to answer the keywords)

1. Can multiple inner classes (not inner classes) be included in a ".java" source file? What are the restrictions?

There can be multiple classes, but only one public class. And the public class name must be consistent with the file name.

2. Is there goto in java?

goto is a reserved word in java, and it is not used in java yet.

3. Tell me the difference between & and &&?

(1) Both & and && can be used as logical operators.

(2) & can also be used as a bitwise operator.

(3) && also has the function of short circuit. If the first expression is false, the second expression is not evaluated.

4. How to break out of the current multiple loop nesting in java?

method one:

Define a label in the outer loop, the format is the label name colon. Then use a break statement with a label in the inner loop, the format is if(...) break label name.

Method Two:

Let the result of the outer loop conditional expression be controlled by the inner loop body code.

5. Can the switch statement act on byte, can it act on long, and can it act on string?

The switch accepts an integer expression or an enumeration constant, and the integer expression can be an int basic type or an integer wrapper type.

Since byte, short, and char can all be implicitly converted to int, the switch statement can act on byte.

As for long and string, neither conforms to the grammatical requirements of switch, and cannot be implicitly converted to int type, so they cannot be used in switch statements.

6. short s1 = 1. Are s1 = s1+1 and s1+=1 wrong?

Because s1+1 will be automatically converted to int type, and then assigned to short type, the compiler will report a forced type conversion error. "

"+=" is an operator specified by the java language, and the java compiler will treat it specially, so s1+=1 will compile normally.

7. Can a Chinese character be stored in a char variable? Why?

It depends. Because char variables are used to store Unicode-encoded characters. If the Chinese character is included in the Unicode coded character set, char can store the Chinese character; otherwise, it cannot.

8. When using the final keyword to modify a variable, does the reference cannot be changed, or the referenced object cannot be changed?

The reference cannot be changed, but the referenced object can be changed.

9. What is the difference between "==" and "equals" methods?

(1) "==" is an operator, and "equals" is a method in the superclass Object.

(2) "==" is specially used to compare whether the values ​​of two variables are the same.

(3) "equals" is used to compare whether the contents of two independent objects are the same.

10. What is the difference between static variables and instance variables?

(1) The static shutdown word should be added before the static variable, and the instance variable is not used.

(2) Static variables are generally called in the way of "class name dot".

(3) The instance variable needs to instantiate the object first, and then call it in the way of "object name point".

11. Is it possible to issue a call to a non-static method from within a static method?

Can't.

12. What is the difference between Integer and int?

(1) int is one of the eight basic types of java, and Integer is the wrapper class of int.

(2) The default value of int is 0, and the default value of Integer is null.

(3) When defining a variable of a student's grade, it is more appropriate to use Integer, because some students may miss the exam!

13. What are the three methods related to rounding in Math?

Ceil means ceiling in Chinese, and this method means rounding up.

floor means floor in Chinese, and this method means rounding down.

The round method means rounding.

14. Talk about the difference between the scope public, private, protected, and not writing?

Do not write any access modifier, it means friendly.

Scope public > protected >friendly >private.

The public scope includes the current class, descendant classes, the same package, and other packages.

The protected scope includes the current class, descendant classes, and the same package.

The friendly scope has the current class, the same package.

The private scope is only the current class.

15. What is the difference between Override and Overload?

(1) Override means overwriting, that is, rewriting. Overload is overloading.

(2) The subclass inherits the parent class and Override rewrites the method in the parent class, and the output result is the content in the subclass method.

(3) Overload means that there can be multiple methods with the same name in a class, but the parameter lists of these methods are different.

16. Can the constructor be overridden?

Can't.

But it can be overloaded by Overload.

17. Can an interface inherit an interface? Can an interface implement an interface? Can an abstract class implement an interface? Can an abstract class inherit from a concrete class Can an abstract class have a static main method?

Interfaces can inherit interfaces.

Interfaces cannot implement interfaces.

Abstract classes can implement interfaces.

Abstract classes can inherit concrete classes.

An abstract class can have a static main method.

18. When writing the clone() method, there is usually a line of code, what is it?

super.clone()。

Because the members of the parent class must be copied in place first, and then the members of the parent class must be copied.

19. What are the aspects of object-oriented features?

encapsulation, inheritance, polymorphism, abstraction

Package:

For example, we usually need to encapsulate the attributes in the entity class to ensure the privacy and security of the attributes.

Generally, the set method is used to set the value, and the get method is used to retrieve the value.

inherit:

The subclass inherits the parent class and overrides the methods in the parent class. The output is the content of the subclass method.

Of course, subclasses may not override a method in the parent class.

Polymorphism:

Polymorphic mechanism:

Class A contains a print method, B and C inherit A and rewrite the print method in A.

When A is new B, the print method at point A calls the print method in B.

When A new C, the print method at point A calls the print method in C.

abstract:

Abstract classes generally have two major characteristics. One is that an abstract keyword is added before the class, and the other is that there is no method body.

Abstract classes can implement interfaces.

Abstract classes can inherit concrete classes.

An abstract class can have a static main method.

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

(1) Whether there is a method body. Abstract classes have no method bodies. Interfaces are the opposite.

(2) Abstract classes are used for inheritance, and interfaces are used for implementation.

(3) A class can implement multiple interfaces, but can only inherit one abstract class.

21. Can abstract methods be static at the same time, native at the same time, and synchronized at the same time?

Neither can.

Because abstract methods are inherited by subclasses, and static has nothing to do with subclasses.

It is meaningless for abstract methods not to care about methods and method locks implemented in non-java languages.

22. What is inner class? What is the difference between Static Nested Class and Inner Class?

An inner class is a class defined inside a class.

(1) Nested classes correspond to static inner classes in Java, and inner classes correspond to non-static inner classes in Java.

(2) As long as another class is defined inside a class, then this class is called a nested class (Static Nested Class), which is equivalent to an inner class modified with the static keyword in Java.

(3) Another class defined inside a class using the inner keyword is called an inner class (Inner Class), which is equivalent to an inner class that is not modified by the static keyword in Java.

23. What are the four internal classes in java?

1. Static inner class:

An inner class declared as static.

2. Member inner class:

Also called non-static inner class, there is no inner class declared as static.

3. Local inner class:

A class defined within a code block.

4. Anonymous inner class:

An inner class without a class name.

The keywords class, extends, implements are not used.

24. Can an inner class refer to members of its containing class? Are there any restrictions?

absolutely okay. If it is not a static inner class, then there is no limit!

28. Can anonymous inner classes inherit other classes and implement interfaces?

Can inherit from other classes or implement interfaces. Not only can, but must.

29.super.getclass()

The getClass method is from the Object class. super.getclass() is a method to call the parent class.

30. Is String the most basic data type?

no.

The eight basic types are short, long, double, float, int, byte, boolean and char.

String is a class under the java.lang package.

31.stringbuffer和stringbuilder?

(1) stringbuffer is thread-safe, and stringbuilder is not thread-safe.

(2) In the case of a single thread, stringbuilder is generally used for a large number of modifications to strings.

(3) In the case of multi-threading, a large number of modifications to the string are generally used stringbuffer.

32. Is it possible to inherit the String class?

Can't. Because the String class is final.

33. String s = "Hello"; s = s + "world!"; After these two pieces of code are executed, has the content in the original String object changed?

No.

Because String is an immutable class, but the reference variable s (when the variable points to an object, this variable is called a reference variable) no longer points to it.

34.String s = new String("xyz"); How many objects are generated?

2 objects were generated.

One is the new String() created by the new keyword; the other is the "xyz" object, xyz is in a string pool, and the s object points to this string pool.

String s=new String(“xyz”) creates several objects (detailed analysis)_Blog of Edge Elements-CSDN Blog

35. How to convert a comma-separated string into an array?

1. Use regular expressions.

2. Use String Tokenizer. (tool class for splitting strings)

36. Is there a length() method in the array? Does String have a length() method?

Arrays do not have a length() method, but have a length property. String has a length() method.

37.String s = "a" + "b" + "c" + "d" + "e"; How many objects are created by this statement?

one.

Because all four of them are constants, the literal values ​​are stored directly at compile time. When the JVM executes String s = "abcde", it will look for it in the String pool. If there is no such string, it will generate one.

38.String s = a+b+c+d+e; How many objects are created by this statement?

3.

They are: StringBuilder, new char[capacity], new String(value,0,count).

38. There is a return statement in Try{}, so will the code in finally{} immediately following the try be executed, when will it be executed, before or after return?

More specifically, it is executed in the middle of return.

It's like the main function prepares an empty jar. When the sub-function wants to return the result, it puts the result in the jar first, and then returns the program logic to the main function.

39. What if finally does not execute?

System.exit(0)。

40. The difference between final, finally, and finalize

(1) final is used to declare attributes, methods, and classes, respectively indicating that attributes are immutable, methods cannot be overridden, and classes cannot be inherited.

(2) finally is part of the exception handling statement, which means it is always executed. Unless the code System.exit(0) is written, finally will not be executed.

(3) finalize is a method in the Object class, and GC (garbage collection) calls this method before recycling the object.

41. What are the similarities and differences between runtime exceptions and general exceptions

(1) Both exceptions belong to the Exception parent class.

(2) Runtime exceptions are RuntimeException class and its subclass exceptions, such as NullPointerException, IndexOutOfBoundsException, etc.

(3) General exceptions are exceptions other than RuntimeException, and they all belong to the Exception class and its subclasses in type.

42. The difference between Error and Exception

(1) Both Error and Exception are subclasses of Throwable.

(2) Exception is an unexpected situation that can be expected during the normal operation of the program, which can be caught and processed accordingly.

(3) Error refers to the situation that is unlikely to happen under normal circumstances. In most cases, the error will cause the program to be abnormal and cannot be recovered, so there is no need to capture it.

43. Principle and application of exception handling mechanism in java?

(1) Try/catch the code block that will generate an exception.

(2) Throw an exception to the method, and the method will eventually throw the exception to the JVM for processing.

(3) JVM, a fictional computer, is a specification for computer equipment.

44. Please write down your 5 most common runtime exceptions?

(1) NullPointerException, null pointer exception.

(2) ArrayIdexOutOfBoundsException, array out of bounds exception.

(3) ClassCastException, type conversion exception.

(4) SQLException, SQL exception.

(5) IOException, input and output exception.

(6) FileNotFoundException, file not found exception.

45. What do the keywords: throw, throws, try, catch and finally mean?

throws: get exception.

throw: Throw an exception.

try: The try block generally puts code that may cause exceptions.

catch: If there is an exception, the statement inside will be executed.

finally: will always be executed, unless the code System.exit(0) is written.

46. ​​How many ways can a thread be implemented in java? What keyword is used to decorate the synchronized method? Why are the stop() and suspend() methods deprecated?

Before java5, there were two types.

One is to inherit the Thread class, and the other is to implement the Runnable interface.

Starting from java5, there have been some thread pools (a form of multithreading) to create multithreading methods.

The keyword that modifies the synchronization method: synchronized

The stop() method is inherently unsafe. The suspend() method has a natural tendency to deadlock.

47. What is the difference between sleep and wait?

(1) The sleep() method comes from the Thread class, and the wait() method comes from the Object class.

(2) sleep must catch exceptions, while wait, notify and notifyAll do not need to catch exceptions.

(3) sleep() can be used anywhere, while wait() can only be used in synchronization methods or synchronization blocks.

48. What is the difference between notify and notifyall?

notify() will only wake up one thread, and the notifyAll method will wake up all threads.

49. What are the similarities and differences between synchronous and asynchronous? Under what circumstances are they used respectively? for example

(1) All belong to the interactive mode, and all send requests.

(2) Synchronous interaction: refers to sending a request, need to wait for the return, and then can send the next request, there is a waiting process.

(3) Asynchronous interaction: refers to sending a request without waiting for the return, and can send the next request at any time, that is, without waiting.

(4) Synchronous interaction needs to wait before sending the next request. Asynchronous interactions do not require waiting.

A phone call is an example of synchronization. The originator needs to wait for the receiver to connect the phone before the communication starts.

Broadcasting is an asynchronous example. The initiator does not care about the state of the receiver.

50. How many ways are there to achieve synchronization?

two kinds. synchronized, wait and notify.

51. To start a thread, use run() or start()

start()。

To start a thread is to call the start() method, so that the thread can be scheduled to run by the JVM in the ready state. A thread must be associated with some specific execution code, and the run() method is the execution code associated with the thread.

52. When a thread enters a synchronized method of an object, can other threads enter the method of this object

There are four cases, three of which I know of. (There is also a kind of Baidu, too lazy to write)

(1) Whether the synchronized keyword is added before other methods, if not, it can be.

(2) If this method calls wait internally, it can enter other synchronized methods.

(3) If the synchronized keyword is added to other methods, and wait is not called internally, it cannot.

53. What are the basic concepts of threads, the basic states of threads, and the relationship between states?

Threads are generally divided into single-threaded and multi-threaded. example...

In the case of a single thread, a large number of modifications to the string are generally used stringbuilder.

In the case of multi-threading, a large number of modifications to the string are generally used stringbuffer.

Threads in Java have four states: running, ready, suspended, and terminated.

Relationship between states:

Ready, run, synchronize blocks, wait and sleep suspend, end. wait must be called inside synchronized.

54. Briefly describe the similarities and differences between synchronized and lock?

(1) lock can complete all functions realized by synchronized.

(2) lock has more precise thread semantics and better performance than synchronized.

(3) synchronized will automatically release the lock, and the lock must be released manually by the programmer, and must be released in the finally clause.

55. Introduce the structure of the Collection (single-column collection) framework?

Collection includes List list and Set collection.

The elements of the List list are ordered and repeatable. The elements of the Set set are unordered and non-repeatable.

The commonly used classes in the List interface are Vector (thread safe, but slow, has been replaced by ArrayList), ArrayList (thread unsafe, fast query speed), LinkedList (thread unsafe, fast addition and deletion).

The commonly used classes in the Set interface are HashSet (thread unsafe, fast access speed), TreeSet (thread unsafe, elements in the Set collection can be sorted).

56. Briefly talk about HashMap?

(1) HashMap is an important implementation class of Map (two-column collection), and it is also the most commonly used. It is implemented based on a hash table (also called a hash table, which is a data structure that is directly accessed according to the key value (Key value)) .

(2) The bottom layer of HashMap is a collection of array + linked list structure

(3) In java1.8, the bottom layer of HashMap changed from array + linked list to array + linked list + red-black tree (a data structure, the typical use is to realize associative array)

57. What interface should be implemented to achieve comparison in the Collection framework?

Comparable and Comparator interfaces.

Comparable:

Call its compareto() method to compare.

Comparator:

Define a comparator separately (generally use an anonymous inner class to implement the comparator), and implement its compare() method for comparison.

58. How to make arraylist thread-safe?

(1) Use the synchronized keyword for method locking.

(2) Use the synchronizedList method in the Collections framework.

59. The difference between Arraylist and Vector?

(1) Vector is thread-safe, while ArrayList is not thread-safe.

(2) Arraylist belongs to the java.lang package, and Vector belongs to the java.util package.

(3) Both of them implement the List interface.

60. What is the difference between hashmap and hashtable?

(1) The main methods in HashTable, such as put, get, remove, etc., have the same functions as those in HashMap.

(2) hashtable inherits from the dictionary class, and hashmap is an implementation of the map interface introduced by java1.2.

(3) HashMap is not thread-safe, and HashTable is thread-safe.

(4) Because of thread safety issues, HashMap is a little more efficient than HashTable.

61. What is the difference between List and Map?

(1) List inherits from the single-column collection Collection. Map is not, it itself is a two-column collection.

(2) The elements in the List collection are ordered and duplicates are allowed. Map is unordered, its keys cannot be repeated, and its values ​​can be repeated.

Map extension:

Commonly used classes in Map are HashTable, HashMap and TreeMap (which can be used to sort the keys in the Map collection.).

62. What is the specificity of List, Map and Set storage elements?

(1) List: Ordered, and allows repetition.

(2) Map: Unordered, its keys cannot be repeated, but its values ​​can be repeated.

(3) Set: Unordered and non-repeatable.

63. Tell me about the storage performance and characteristics of ArrayList, Vector, LinkedList?

(1) Both ArryList and Vector use arrays to store data.

(2) LinkedList uses a doubly linked list (a doubly linked list is also called a doubly linked list, which is a kind of linked list, and each of its data nodes has two pointers, which point to the direct successor and direct predecessor respectively.) to realize storage.

(3) ArrayList and LinkedList are not thread-safe, and Vector is thread-safe.

64. How does HashSet ensure that elements are not repeated?

Hashset encapsulates the add method and put method.

put traverses through the for loop to determine whether the values ​​of the hash codes of the elements are the same.

Same, if it proves that the element exists, discard it. Not the same, prove that the element does not exist, then add.

65. What is the difference between collection and collections?

(1) collection is the upper-level interface of the collection class, and the interfaces that inherit it mainly include Set and List.

(2) collections is a helper class for collection classes. For example, turning the original non-thread-safe ArrayList into thread-safe can be done through the synchronizedList method in collections.

66. The elements in the Set cannot be repeated, so what method is used to distinguish whether it is repeated or not?

Equals。

expand:

Why use equals() instead ofTo distinguish? ("" and "equals" method difference see question 9)

According to the storage mechanism of java, it can be seen that the references of objects are stored in the set, so when two elements satisfy equals(), they already point to the same object, and there are duplicate elements.

67. What collection classes do you know? main method?

The most commonly used collection class interfaces are List and Map.

The main methods of List are:
add, get, remove, set, addAll, removeAll, indexOf, clear, isEmpty

The main methods of Set are:
add, remove, addAll, removeAll, toArray, clear, isEmpty

The main methods of Map are:
put, get, keySet, values, clear, remove, isEmpty

68. Two objects have the same value (x.equals(y) == true), but they can have different hash codes. Is this sentence correct?

Some say it's not right, and some say it's right. I think so.

(1) If objects are to be stored in hashset or hashmap, and their requals are the same, then the hash code must be the same.

(2) If it is not to be stored in the hashset or hashmap, it has nothing to do with the hash code. At this time, the hash code cannot be used.

69. Put objects in the TreeSet. If you put the instance objects of the parent class and the subclass at the same time, then use the compareTo method of the parent class or the compareTo method of the subclass when comparing, or throw an exception!

It depends.

Which object is put into the current add method, the compareTo method of which object is called.

70. Name some commonly used classes, packages, and interfaces, please give 5 of each

kind:

String Integer Class Date StringBuffer StringBuilder

Bag:

java.lang java.io java.util java.sql java.servlet

interface:

ListMapDocumentNodeListServlet

71. How many types of streams are there in java? JDK provides some abstract classes for each type of stream for inheritance. Please tell me which classes they are?

byte stream, character stream.

The byte stream inherits from InputStream OutputStream.

The character stream inherits from InputStreamReader OutputStreamWriter.

72. What is the difference between byte stream and character stream?

(1) The byte stream processing unit is a 1-byte Unicode character. The unit of character stream processing is 2 bytes.

(2) Byte streams generally deal with bytes and byte arrays or binary objects. Character streams generally deal with characters, character arrays, or strings.

(3) The byte stream ends with stream. And the character stream ends with reader and writer.

(4) The byte stream does not use a buffer by default. Character streams use buffers.

73. What is java serialization, how to implement java serialization?

Serialization is a mechanism for handling streams of objects.

Serialization and deserialization:

Serialization is the process of converting a Java object into a sequence of bytes.

Deserialization is the process of restoring a sequence of bytes to a Java object.

Two ways to implement java serialization:

1. Implement the interface Serializable

(1) Define a class that implements the Serializable interface

(2) Serialize the object of the Student class

(3) Deserialize the object of the Student class

2. Implement the interface Externalizable

(1) Define a class that implements the Externalizable interface and implements the writeExternal and readExternal methods

74. Explain the role of Serializable

1. Store the object in the storage medium, so that a copy can be quickly and quickly rebuilt when it is used next time;

2. It is convenient for data transmission, especially when calling remotely.

75. Describe the principle mechanism of JVM loading class files?

1. The loading of classes in the JVM is implemented by the class loader (ClassLoader) and its subclasses.

2. The class loader in java is an important java runtime system component, which is responsible for finding and loading classes in class files at runtime.

76. What is the difference between heap and stack?

(1) heap is a heap, and stack is a stack.

(2) The heap space is manually applied and released. Stack space is automatically allocated and released by the operating system.

(3) The heap mainly stores some new objects and arrays. stack mainly saves some basic data types and reference variables of objects

77. What is GC? Why is there a GC?

Garbage collection.

Because invalid objects in the program will occupy memory and affect performance, an effective mechanism is needed to release memory at this time. He is the GC.

78. Principles and advantages of garbage collection mechanisms and consider two recycling mechanisms.

1. Don't think about memory management

2. Effectively prevent memory leaks

Principle (objects cannot be garbage collected immediately):

Garbage collection runs as a separate thread to recycle objects that have not been used for a long time, or dead objects in the memory heap, but cannot immediately garbage collect objects

And consider two recycling mechanisms:

1. Timing recycling.

2. When the garbage accounts for a certain percentage, GC is performed.

79. When to use assert?

Assertions are a common tuning technique in software development.

Assertions are usually turned on during development and testing.

Assertions come in two forms:

assert <boolean expression>;

assert <boolean expression> : <error message expression>;

80. Will there be a memory leak in java? Please briefly describe

Will do.

For example:

1. An object or variable that is no longer used by the programmer has been occupying memory, causing a memory leak.

2. Various connections did not call the close method.

3. The inner class holds the outer class. (Blowing internal class)

81. Can you write a class by yourself, also called java.lang.String

Can.

But you need to use your own class loader to load it when you apply it.

And the class loader of the system will only load the java.lang.String in the jre.jar package forever.

82. How to customize a class loader?

1. Inherit the classLoad class.

2. Rewrite the findClass method.

83. What is the difference between i++ and ++i?

i++ is the first assignment and then ++, ++i is the first ++ and then assignment.

If say int i = 1, and output i++ and ++i at the same time, the result is 1 and 3.

84. What features are added to the interface of Java 8?

lambda expression (a total of six formats. Standard format: (parameter list) -> {code for some rewritten methods};)

functional interface

method reference

default method

Duplicate annotation

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

(1) BIO is synchronous and blocked. NIO is synchronous non-blocking. AIO is asynchronous and non-blocking.

(2) BIO is suitable for architectures with a relatively small and fixed number of connections.

(3) NIO is suitable for architectures with a large number of connections and relatively short connections.

(4) AIO is suitable for architectures with a large number of connections and relatively long connections.

86. Three ways to get reflection in Java

Reflection is to map each member of a Java class into a Java object one by one.

First, use the Class.forName static method.

Second, use the .class method of the class

The third way is to use the getClass() method of the instance object.

87. What are the Java reflection mechanisms?

Determine the class of any object at runtime

Construct an object of any class at runtime

Get the member variables and methods of any class at runtime

Call member variables and methods of any object at runtime

Generate dynamic proxy

88. Main classes related to reflection

java.lang.Class:

Represents a class, and the Class object represents the object in the heap after a certain class is loaded

java. lang.reflec.Method:

Represents the method of the class, and the Method object represents the method of a certain class

java.lng.reflect.Field:

Represents a member variable of a class, Field object representation, a member variable of a class

java.lang.reflect.Constructor:

Represents the construction method of the class, and the Constructor object represents the constructor

89. Push and pop stack?

(1) Push stack: It is to store elements. That is, the element is stored at the top position of the stack, and the existing elements in the stack move one position to the bottom of the stack in turn.

(2) Pop the stack: it is to take the element. That is, the element at the top position of the stack is taken out, and the existing elements in the stack move one position toward the top of the stack in turn.

90. What is upward transformation? What is downcasting?

Upcasting and downcasting are type conversions between parent and child.

Upcasting is equivalent to automatic type conversion.

Downcasting is equivalent to a forced type conversion. Downcast keyword instanceof.

91. What is a generic wildcard? What are the upper and lower bounds of generics?

<>

Suppose there is a Dog class.

The upper limit of generics: only Dog or a subclass of Dog can be taken.

The lower limit of generics: only Dog and the parent class can be used, and Dog subclasses cannot be used.

92. What is a thread pool? What are the advantages of thread pool?

Thread Pool:

It is a container that accommodates multiple threads, and the threads in it can be used repeatedly, eliminating the need to frequently create thread objects and consume too many resources by repeatedly creating threads.

advantage:

Reduce resource consumption. Improve responsiveness. Improve thread manageability.

93. The relationship between JVM, JRE and JDK

(1) Java Virtual Machine is a Java virtual machine. Java programs need to run on a virtual machine. Different platforms have their own virtual machines, so the Java language can achieve cross-platform.

(2) The Java Runtime Environment includes the core class libraries required by the Java virtual machine and Java programs.

(3) The Java Development Kit is provided to Java developers, which includes Java development tools and JRE.

94. What is bytecode? What is the biggest benefit of adopting bytecode?

The file generated after the Java source code is compiled by the virtual machine compiler (that is, the file with the extension .class), it is not oriented to any specific processor, but only to the virtual machine.

Benefits of using bytecode:

(1) To a certain extent, it solves the problem of low execution efficiency of traditional interpreted languages, while retaining the portability of interpreted languages.

(2) Since the bytecode is not specific to a specific machine, a Java program can run on a variety of different computers without recompilation.

95. What is cross-platform? What is the principle?

The so-called cross-platform refers to a program written in the Java language, which can run on multiple system platforms after being compiled once.

Implementation principle:

The Java program runs on the system platform through the java virtual machine, as long as the system can install the corresponding java virtual machine, the system can run the java program.

96. What is value passing and reference passing?

(1) Value transfer is for basic variables, and what is transferred is a copy of the variable, and changing the copy does not affect the original variable.

(2) Transfer by reference, generally for object variables, what is passed is a copy of the address of the object, not the original object itself.

97. What is the difference between a synchronized code block and a synchronized method?

(1) The synchronization method is to add the keyword synchronized before the method. A synchronized code block uses curly braces inside a method to make a code block synchronized.

(2) The synchronization method directly adds synchronized to the method to realize locking. Synchronized code blocks are locked inside the method

98. What is deadlock (deadlock)?

A deadlock occurs when both processes are waiting for the other to finish executing before continuing to execute.

The result is that both processes are stuck in an infinite wait.

99. How to solve deadlock?

(1) Avoid one thread acquiring multiple locks at the same time.

(2) Avoid a thread occupying multiple resources in the lock at the same time, and try to ensure that each lock only occupies one resource.

(3) Try to use a timed lock, use Lock.tryLock(timeout) instead of using the internal lock mechanism.

(4) For database locks, locking and unlocking must be in one database connection, otherwise unlocking will fail.

100. Do you like JAVA?

Let me know in the comments section! ! !

Guess you like

Origin blog.csdn.net/m0_67265464/article/details/126741091