[Java Interview Questions] OOP

Article Directory

Original: https://www.yuque.com/unityalvin/baguwen/pg7qzg

1. What is process-oriented

Process-oriented is a top-down programming model, which splits the problem into steps, and each step is implemented with a function, which can be called in turn

2. What is object-oriented

Object-oriented is a programming model that highly abstracts transactions. It also decomposes problems into steps. It abstracts each step accordingly to form objects, and solves them through combinations of calls between different objects. question

3. The difference between object-oriented and process-oriented

Process-oriented: low resource occupation, relatively fast execution speed
Object-oriented: high resource occupation, relatively slow execution speed, but object-oriented can make the project more modular, achieve lower coupling and higher cohesion

4. Three basic characteristics of object-oriented

encapsulation, inheritance, polymorphism

5. How do you understand packaging

It is to encapsulate objective things into abstract classes, so that only trusted classes or objects can be accessed, and untrusted ones can be hidden.

6. Let’s talk about inheritance

Inheritance means that a class can inherit all the functions of another class and extend it without rewriting the original class. This kind of derivation reflects transitivity. In addition to inheritance, there is another way to reflect transitivity called accomplish.

The difference between the two:
● Inheritance: use all the properties and methods of the parent class without additional coding
● Implementation: only the names of global variables and methods defined by the interface can be used, and the implementation class must provide the implementation of the method

7. What is polymorphism

The so-called polymorphism means that the same method of a class instance has different manifestations in different situations, so that objects with different internal structures can share the same external interface.

8. How to implement polymorphism

● There is class inheritance or interface implementation
● The subclass must override the method of the parent class
● The reference of the parent class points to the object of the subclass

Compile to see the left, run to see the right

9. What is B/S architecture? What is C/S Architecture

  1. B/S(Browser/Server), browser/server program
  2. C/S(Client/Server), client/server, desktop application

10. What is the difference between JDK, JRE, and JVM?

insert image description here

JDK: "Java Development Kit"
● Java development toolkit, which provides a Java development environment and operating environment, including the compiler Javac for compiling Java source files, as well as debugging and analysis tools.

JRE: "Java Runtime Environment"
● Java runtime environment, including Java virtual machine and some basic class libraries

JVM: "Java Virtual Machine"
● Java virtual machine, which provides the ability to execute bytecode files

11. What are the basic data types and wrapper classes of Java?

Numeric
● Integer: byte, short, int, long
● Floating point: float, double
Character
● char
Boolean
● boolean

Packaging classes
● Byte, Short, Integer, Long, Float, Double, Character, Boolean

12. What are the common data structures in Java?

  1. Array "Array"
  2. Stack "Stack"
  3. Linked list "Linked List"
  4. Figure "Graph"
  5. Hash table "Hash"
  6. Queue "Queue"
  7. Tree "Tree"
  8. Heap "Heap"

13. The difference between == and equals

==
● When comparing basic data types, the value is compared
● When comparing reference types, what is compared is the address of the reference in memory

equals
● It is a method of java.lang.Object, this method returns the judgment of ==, but some classes rewrite this method, so they compare whether the content is equal, for example: String

Object's equeals method:
insert image description here
String's equeals method:
insert image description here

14. What is the relationship between classes and objects?

A class is an abstraction of an object, and an object is a concrete instance of a class A class
is a template for an object, and an object is an instance of a class

15. The instanceof keyword

Used to test whether an object is an instance of a class, this instance can be an object or an interface

The compiler will check whether the object can be converted into an instance of the class in real time
● If the instance type can be determined, if it cannot be converted, the compilation will not pass directly
● If the instance type cannot be determined, it depends on the runtime

Note: If you judge whether the basic data type belongs to the corresponding packaging class, such as: 1 instanceof Integer, the result must be that the compilation fails

16. What is implicit conversion and explicit conversion

● Implicit conversion is automatic type conversion, which is to assign a small type of data to a large type of data and box it; ● Explicit
conversion is a forced type conversion, which is to assign a large type of data to a small type box;

17. Can char type be converted into int type? Can it be converted into String type, can it be converted into double type

● char is also a special type in java. Its int value starts from 0, and there are 16 times (65536) squares of data in total; ● char<
int<long<float<double
● char type can be implicitly converted to Int and double types, but cannot be implicitly converted to String;
● When converting char type to byte or short type, it needs to be forcibly converted.

18. What is boxing and unboxing

  1. Boxing is to convert the basic data type into a packaging type (int–>Integer)
    a. Call: Integer.valueOf(int) method
    b. Before jdk 1.5, Integer integer = new Integer(5);
    c. After jdk 1.5, Automatic boxing is realized, just like this, Integer integer = 5;
  2. Unboxing is to convert the packaging type to the basic data type (Integer–> int)
    a. Call: Integer.intValue() method
    b. After jdk 1.5, automatic unboxing is realized, int i = integer;

19. Which places will automatically unpack the box

  1. Put primitive data types into collection classes

  2. Wrapper type compares size with base type

  3. Operations on wrapped types

  4. Use of the ternary operator
    insert image description here

  5. Function parameters and return values

20. What are the functions and usages of final in java

  1. Classes modified by final cannot be inherited
  2. The method modified by final cannot be rewritten, and the JVM will try to inline it to improve operating efficiency
  3. Variables modified by final cannot be changed, and the JVM will store them in the constant pool during the compilation phase. If the reference is modified, it means that the reference is immutable, but the content pointed to by the reference is variable

21. What is included in a java class?

Properties, methods, inner classes, constructors, code blocks

22. Why can't the amount be expressed in floating point?

Since the decimals stored in the computer are actually approximate values ​​of decimals, not exact values, it is best to use BigDecimal or Long (unit is cent) to represent the amount

23. What are the access modifiers in Java and their differences

insert image description here

● public: indicates that the member variable or method is visible to all classes and objects, and all classes or objects can be directly accessed
● private: indicates that the member variable or method is private, and only the class itself has access to it. In addition, other classes or objects have no access rights, including subclasses
. Protected: Indicates that the member variable or method is visible to the class itself, other classes in a package, and subclasses, and cannot be accessed by classes under other packages
. default: When a member of a class does not write an access modifier, it defaults to default, indicating that the member variable or method is only visible to the class itself and other classes in a package, and subclasses and classes under other packages are not accessible

24. Is float f=3.4; correct?

insert image description here

Incorrect, 3.4 is a double-precision number. Assigning a double-precision type (double) to a floating-point type (float) is a downcast that will cause precision loss, so it is necessary to cast float f = (float) 3.4; or write it as float f = 3.4F;.

25. What is single precision and double precision

● Single-precision floating-point numbers occupy 4 bytes (32 bits) in the computer's memory. Using the "floating point (floating decimal point)" method, a large range of values ​​can be represented.
● Double-precision floating-point number (double) uses 8 bytes (64 bits) to store a floating-point number.

26. The difference between String, StringBuffer, and StringBuilder

String
● String is a read-only string, it is not a basic data type, but an object. From the perspective of the underlying source code, it is a final character array. The referenced string cannot be changed. Once defined, it cannot be added, deleted or modified.
● Each operation on String will generate a new String object
● Each + operation will implicitly create a new StringBuilder object on the heap that is the same as the original string, and then call the append method to concatenate the characters after +.

StringBuffer, StringBuilder
● Both StringBuffer and StringBuilder inherit the AbstractStringBulder class, and AbstractStringBuilder implements the CharSequence interface. Both classes are used for string operations.
● It is more efficient than String when concatenating, modifying, deleting and replacing strings.
● StringBuffer is thread-safe, Stringbuilder is not thread-safe.
● Stringbuilder is more efficient than Stringbuffer because most of the methods of StringBuffer have added the synchronized keyword

27. The difference between i++ and ++i

● i++: assign first, then calculate
● ++i: calculate first, then assign

28. There are several instantiation methods for arrays

● Static instantiation: Specify the elements in the array while creating the array
● Dynamic instantiation: When instantiating the array, only specify the length of the array, and all elements in the array are the default values ​​of the array type

29. After instantiating the array, can the length of the array be changed?

No, once an array is instantiated, its length is fixed

30. Assuming there are 5 elements in the array, what should be done if the array is reversed?

Create a new array, loop through each element from the back to the front, and put the taken elements into the new array in order
Or, use a temporary variable and swap the first and last two by two

31. Default values ​​of various data in Java

● The default value of byte, short, int, long is 0
● The default value of boolean is false
● The default value of char is \u0000
● The default value of float and double is 0.0
● The default value of object type is null

32. What are the commonly used Java packages?

● Java.lang
● Java.io
● Java.sql
● Java.util
● Java.awt
● Java.net
● Java.math
juc

33. What are the commonly used methods of the Object class?

● equals(Object obj)
● hashCode()
● toString()
● wait()
● notify()
● clone()
● getClass()

34. Are there pointers in Java?

Yes, but it is hidden. Developers cannot directly manipulate pointers, and jvm manipulates pointers

35. Is Java passed by value or by reference?

In Java, parameter passing is actually achieved through "value passing", but for the passing of Java objects, the content passed is a reference to the object.

In order to let everyone understand what you said, you said that there is only value transfer in Java, but the content passed is a reference to an object. This is also fine. However, pass-by-reference in Java should never be considered.

36. Why is value passing

In the programming language, parameters need to be passed between methods. The strategy of this transfer is called the evaluation strategy.

In programming, there are many evaluation strategies, the more common ones are "pass by value" and "pass by reference". Another special case of value transfer is "shared object transfer".

The biggest difference between "pass by value" and "pass by reference" is whether a copy is copied during the transfer process. If a copy is passed, it is passed by value, otherwise it is passed by reference.

37. Formal Participation and Actual Parameters

Formal parameter: It is the parameter used when defining the function name and function body, and its purpose is to receive the parameters passed in when calling the function.
Actual parameters: When calling a function with parameters, there is a data transfer relationship between the calling function and the called function. When calling a function in the calling function, the parameters in parentheses after the function name are called "actual parameters"

37. Can the constructor be called explicitly?

The construction method cannot be called as a normal method, it will only be called by the system when creating an object

38. What is overloading?

Overloading refers to the situation where functions or methods have the same name but different parameter lists. Such functions or methods with the same name but different parameters are called overloaded functions or methods.

39. What is rewriting

Overriding refers to the situation where there are two methods with the same name and parameter list in the Java subclass and parent class. Since they have the same method signature, the new method in the subclass will override the original method in the parent class, which is called rewriting.

40. Conditions for rewriting

● The parameter list and return type must be the same as those of the overridden method;
● Access level restrictions: cannot be stronger than that of the overridden method, but can be weaker than that of the overridden method;
insert image description here

● Overriding methods cannot throw new checked exceptions, nor can they throw checked exceptions that are more extensive than the overridden method, but can throw fewer or more limited exceptions
● Cannot override methods marked as final;
● If you cannot inherit a method (such as private modification), you cannot override this method.

41. Conditions for overloading

● The overloaded method must change the parameter list;
● The overloaded method can change the return type, access modifier, and can declare new or wider checked exceptions; ●
The method can be in the same class or in a subclass is overloaded.

42. The difference between overloading and rewriting

  1. Overloading is a compile-time concept, and rewriting is a runtime concept.
  2. Overloading follows the so-called "compile-time binding", that is, which method should be called based on the type of the parameter variable at compile time.
  3. Rewriting follows the so-called "runtime binding", that is, at runtime, the method is called according to the type of the actual object pointed to by the reference variable
  4. Because which method to call is determined at compile time, overloading is not polymorphic. Whereas overriding is polymorphism. Overloading is just a language feature, a grammatical rule, and has nothing to do with polymorphism or object-oriented. (Note: Strictly speaking, overloading is compile-time polymorphism, that is, static polymorphism. However, polymorphism mentioned in Java refers to dynamic polymorphism unless otherwise specified)

43. Can the construction method be rewritten? Can it be overloaded?

cannot be overridden, but can be overloaded

44. What is the difference between a static inner class and a (member) inner class?

  • Static inner classes
    ● Static inner classes exist independently of outer classes.
    ● Static inner classes can access all static variables and methods of outer classes, even private ones.
    ● Static inner classes cannot directly access instance variables of outer classes. , method, if you want to access, you must new an object of an external class, and use the new object to access.
    ● The static inner class is consistent with the general class, and can define static variables, methods, construction methods, etc.
    ● For other classes to access the properties of the static inner class or call the methods of the static inner class, they need to use the method of "outer class. static inner class". ● There is a
    static inner class Entry inside the Java collection class HashMap. Entry is the abstraction of HashMap storing elements. HashMap internally maintains an Entry array to store elements, but Entry is transparent to users. Static inner classes can be used for those that are closely related to external classes and do not depend on external class instances.

  • Ordinary inner class
    ● An ordinary inner class exists as a member of an outer class. In an ordinary inner class, you can directly access the properties of the outer class and call the methods of the outer class.
    ● If the outer class wants to access the properties of the inner class or call the method of the inner class, it must create an object of the inner class, and use this object to access the properties or call the method.
    ● If other classes want to access the properties of ordinary inner classes or call the methods of ordinary inner classes, they must create an object of ordinary inner classes in the outer class as an attribute, and then they can use this attribute to call methods of ordinary inner classes or access Attributes of ordinary inner classes
    insert image description here
    ● Static methods and variables cannot be defined (except those modified by final)

45. What does the static keyword do?

static can modify internal classes, methods, variables, and code blocks
● The class modified by static is a "static internal class" The
method modified by static is a "static method", which means that the method belongs to the current class, not an object. Static methods 不能被重写, can be called directly using the class name. The this or super keywords cannot be used in static methods.
● Variables modified by static are "static variables" or "class variables". Static variables are shared by all instances and do not depend on objects. There is only one copy of static variables in memory, and memory is only allocated once for static variables when the JVM loads a class.
● Static modified code blocks are called "static code blocks", which are usually used for program optimization. The code in the static code block will only be executed once when the entire class is loaded. There can be multiple static code blocks, and if there are multiple blocks, they will be executed sequentially.
● static can also statically import packages, which is a new feature after jdk 1.5, for example: import static java.lang.Math.*;, if you want to use the sin() method in the Math class, you don’t need to use Math at this time ., you can call sin() directly;

46. ​​What are the common methods of the Math class

● pow(): power operation (finally call the native method)
insert image description here

● sqrt(): square root
insert image description here

● round(): round up
insert image description here

● abs(): Find the absolute value
insert image description here

● random(): generate a random number from 0 to 1, including 0 and excluding 1
insert image description here

min()
insert image description here

max()
insert image description here

47. What are the common methods of the String class?

● charAt(): Returns the character at the specified index
● indexOf(): Returns the index of the specified character
● replace(): Replaces the string
● 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(): Converts the string to lowercase letters
● toUpperCase(): Converts the string to Uppercase characters
● substring(): intercept string
● format(): format string
● equals(): string comparison
toCharArray(): convert to character array
intern(): return directly if the character constant pool has the string , otherwise put into the constant pool and return

48. Is Java single inheritance or multiple inheritance?

Only one class can be inherited, but multiple interfaces can be implemented

49. What do super and this mean?

● super means the parent class object of the current class
● this means the object of the current class

50. What is the difference between ordinary classes and abstract classes?

● Ordinary classes cannot contain abstract methods, but abstract classes can contain abstract methods
● Abstract classes cannot be instantiated directly, but ordinary classes can be instantiated directly

51. What is an interface? Why do you need an interface?

An interface is a statement of some functions provided by something externally. It is a special java class. The interface makes up for the shortcomings of java single inheritance.

52. What are the characteristics of the interface?

● Variables in interfaces can only be public static final modified constants
● All methods in interfaces are abstract methods
● Interfaces have no construction methods
● Interfaces cannot be instantiated directly
● A class can implement multiple interfaces

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

abstract class

  1. Abstract method, only the concept of behavior, no specific behavior to achieve.
  2. Decorated with the abstract keyword, there is no method body. Subclasses must override these abstract methods.
  3. Abstract classes can only be inherited, and a class can only inherit one abstract class.
  4. Members of abstract classes support all permission modifiers
  5. Abstract classes can define constructors, abstract methods, non-abstract methods

interface

  1. All methods are abstract methods, and properties are constants
  2. cannot be instantiated
  3. Interface variables can refer to instances of concrete implementation classes
  4. Interfaces can only be implemented, and a concrete class implementing an interface must implement all abstract methods
  5. Multiple interfaces can be implemented
  6. A concrete class can implement multiple interfaces to achieve multiple inheritance
  7. Members defined in an interface can only be defined as public
  8. After jdk 1.8, there can be implemented methods in the interface, and default or static should be added to the method declaration

54. Why is there a hashCode?

There are two types of collections in Java, List and Set. List is ordered and repeatable, and Set is unordered and cannot be repeated. When we want to insert data into Set, how does it judge whether the data to be inserted is repeated? You can use equals( ) method, but if there are too many elements, the execution efficiency will be slower.

So someone invented the hash algorithm to improve the efficiency of finding elements in the collection. This method will divide the collection into several storage areas, and each object can calculate a hash code, which can be grouped, and each group corresponds to a storage area, and the hash code of an object can be determined. The area where the object should be stored.

The hashCode() method can be understood in this way: what it returns is a value converted according to the memory address of the object.

In this way, when a new element is to be added to the collection, the hashCode() method of this element is called first, and the physical location where it should be stored can be located at once. If there is no element at this position, it can be directly stored at this position without any comparison;

If there is already an element at this position, call its equals() method to compare with the new element, if it is the same, it will not be saved, if not, it will be hashed to another address. In this way, the number of actual calls to the equals() method is greatly reduced, almost only one or two times.

, Is it possible that two unequal objects have the same hashCode? It is
possible that when a hash conflict occurs, two unequal objects will have the same hashcode value

Generally, there are several ways to deal with it:

  1. Zipper method (chain address method)
    ● Each unit of the hash table is used as the head node of the linked list, and all elements whose hash address is i form a synonym linked list. That is, when a conflict occurs, the keyword is linked at the end of the linked list with the unit as the head node.
  2. Open addressing method
    ● Once a conflict occurs, look for the next empty hash address. As long as the hash table is large enough, you can always find an empty hash address and store the record in
  3. Re-hash method
    ● When the hash address conflicts, use other functions to calculate another hash function address until no more conflicts occur.

55. The four references of Java, strong and weak

Strong reference
● It is the most commonly used reference in normal times. Strong references will not be recycled when the program memory is insufficient "OOM"

Soft references
● Soft references will be recycled when the memory of the program is insufficient.
● Available scenarios: When the cache is created, the created objects are put into the cache. When the memory is insufficient, the JVM will recycle the objects created earlier.

Weak reference
● Weak reference means that as long as the JVM garbage collector finds it, it will be recycled. ●
Available scenarios: the key in java.util.WeakHashMap in the Java source code uses weak references.
● My understanding: once I do not Need a reference, JVM will automatically handle it for me, so I don't need to do other operations.

Phantom references
● The recycling mechanism of phantom references is similar to that of weak references, but before they are recycled, they will be put into ReferenceQueue. Other references are passed into ReferenceQueue after being recycled by the JVM. Due to this mechanism, phantom references are mostly used for processing before the reference is destroyed.
● When virtual references are created, they must have a ReferenceQueue
● Available scenarios: some operations before object destruction, such as resource release (although Object.finalize() can also do this kind of action, but this method is not safe and inefficient)

The types of references mentioned above refer to the references of the object itself, not to the references of the four subclasses of Reference

56. How many ways are there to create objects in Java? (4)

  1. new creates a new object
  2. through the reflection mechanism
  3. Using the clone mechanism
  4. through the serialization mechanism

57. What is the difference between shallow copy and deep copy?

Shallow copy
Pass by value for basic data types, copy by reference for reference data types

Deep copy
transfers by value for basic data types, creates a new object for reference data types, and copies its contents

58. Is there any difference between a = a + b and a += b?

The += operator performs implicit automatic type conversion, where a += b implicitly casts the result type of the addition operation to the type that holds the result, while a = a + b does not automatically perform type conversion

59. Short s1 = 1; s1 = s1 + 1; Is there anything wrong?

There is a mistake, the short type will be automatically promoted to the int type when performing operations, that is to say, the operation result of s1 + 1 is int type, and s1 is short, and the compiler will report an error at this time

Solution: Change s1 = s1 + 1; to s1 += 1;

For example, in Idea:
insert image description here

60. What is the difference between final, finally, and finalize?

different in nature

  1. final is a keyword;
  2. finally is a block flag, used in the try statement;
  3. finalize() is a method;

effect

  1. final is a keyword used to identify constants, and the keywords identified by final are stored in the constant pool
  2. finally{} is used to identify a code block and cooperates with try{}. Regardless of whether the code in try is executed or not (here, there is an exception), the code in finally{} will definitely run
  3. The finalize() method is defined in Object and is used to be called by the JVM when the object "disappears" for garbage collection of the object, similar to the "destructor" in C++; when user-defined, it is used to release The resources occupied by the object (such as performing I/O operations);

61. Steps of JDBC operation (5)

  1. Load the database driver class
  2. open database connection
  3. Execute SQL statement
  4. Handle the returned result
  5. close resource

62. How to prevent SQL injection when using JDBC.

Use the PreparedStatement class instead of the Statement class

63. How to call a stored procedure in JDBC

Use CallableStatement

64. Do you know about connection pools? What are the benefits of using connection pools?

Database connection is very resource-intensive and affects the performance indicators of the program.

The connection pool is used to allocate, manage, and release database connections, so that the application can reuse the same database connection instead of creating a new database connection every time. By releasing the database connection with a long idle time, avoiding the problem of connection omission caused by creating too many connections in the database, and improving the program performance.

65. What data source technologies do you know? What are the benefits of using data sources?

Druid, Dbcp, c3p0, etc. are mostly used because Druid is more stable and secure; database information is maintained in the form of configuration files instead of hard coding. When the connected database information changes, the database information can be updated without changing the program code.

66. The difference between & and &&

● & is a bitwise operator.
● && is a boolean logic operator, used in logic judgment

● If the front of & is false, the following content still needs to be processed.
● If the front of && is false, the subsequent content will not be processed.

67. Does an array have a length() method? Does String have a length() method?

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

68. Use the most efficient method to calculate how many times 2 times 8 is equal to

2 << 3

69. What are the methods of sorting? Please list

insert image description here
insert image description here

70. Can a Chinese character be stored in a char variable, why?

The char type can store a Chinese character, because the encoding used in Java is Unicode (do not choose any specific encoding, directly use the number of characters in the character set, which is the only way to unify), a char type occupies 2 bytes ( 16 bits), so it is no problem to put a Chinese.

71. Will there be memory leaks in Java? Please describe briefly

In actual development, there may be some objects that are no longer used, but are still referenced by some places. These objects will not be reclaimed by GC, so memory leaks will occur.

72. Can an abstract method be static at the same time, can it be native at the same time, and can it be modified by synchronized at the same time? (both are not)

both are not. Abstract methods need to be rewritten by subclasses, while static methods cannot be rewritten, so the two are contradictory. Native methods are methods implemented by native code (such as C code), while abstract methods are not implemented, which is also a contradiction. Synchronized is related to the implementation details of the method, and the abstract method does not involve the implementation details, so it is also contradictory.

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

No, static methods can only access static members, because non-static method calls must first create objects, and objects may not be initialized when calling static methods.

74. How to implement object cloning?

I used to use BeanUtils.copyProperties();, but later learned that it uses reflection to obtain objects and assign values, which is less efficient, and then replaced with Mapstruct, which will compile the project according to the defined interface. , automatically create an interface implementation class, which will generate corresponding implementation methods according to the formal parameters and return values ​​of the methods in the interface

75. Can an interface inherit an interface? Can an abstract class implement an interface? Can an abstract class inherit from a concrete class? (It will be all right)

Interfaces can inherit interfaces, and multiple inheritance is supported.
Abstract classes can implement interfaces, abstract classes can inherit concrete classes, and abstract classes can also inherit

76. Can anonymous inner classes inherit other classes? Is it possible to implement the interface? (It will be all right)

Can inherit from other classes or implement other interfaces

77. Integer cache mechanism

What is the result of running the following code?

		Integer i1 = new Integer(1);
        Integer i2 = new Integer(1);
        int i3 = 1;

        System.out.println("=====");
        System.out.println(i1 == i2);
        System.out.println(i1 == i3);

        Integer i4 = 2;
        Integer i5 = 2;
        int i6 = 2;
        System.out.println("=====");
        System.out.println(i4 == i5);
        System.out.println(i4 == i6);

        Integer i7 = 128;
        Integer i8 = 128;
        int i9 = 128;
        System.out.println("=====");
        System.out.println(i7 == i8);
        System.out.println(i7 == i9);

result:
insert image description here

Summary:
1. Use ==comparison between Integer objects, the comparison is the address
2, the size of the wrapper class and the basic type, the wrapper class is implicitly converted to the basic type
3, assign the basic type to Integer, call the valueOf method:
insert image description here
valueOf method, when When the value is in [-128~127], it will be taken from the cache, otherwise, the new object

Guess you like

Origin blog.csdn.net/m0_55155505/article/details/126806117