Power Node Java Large Internet Architect

Everyone should know that Java is one of the most popular computer languages ​​at present, and it has been the most popular computer language among programmers for several consecutive years, so every year there are countless new Java programmers. Are these newly recruited Java programmers entering the pit or entering the industry? It depends on their views on the Java language. In any case, the questions will be interviewed before entering the job, so how do the Java interview questions come out? Below is a list of 20 common junior Java interview questions, which are simply a must for recruits!

1. What are the characteristics of object-oriented?

Answer: Object-oriented features mainly include the following aspects:

  • Abstraction: Abstraction is the process of constructing a class by summarizing the common characteristics of a class of objects, including data abstraction and behavior abstraction. Abstraction only pays attention to the properties and behaviors of the object, and does not pay attention to the details of these behaviors.

  • Inheritance: Inheritance is the process of obtaining inheritance information from existing classes to create new classes. The class that provides inheritance information is called the parent class (superclass, base class); the class that gets the inheritance information is called the subclass (derived class). Inheritance allows the changing software system to have a certain continuity. At the same time, inheritance is also an important means to encapsulate the variable factors in the program (if you don’t understand, please read Dr. Yan Hong’s "Java and Patterns" or "Design Patterns" about Part of the bridge mode).

  • Encapsulation: It is generally believed that encapsulation is to bind data and methods of manipulating data, and access to data can only be through a defined interface. The essence of object-oriented is to describe the real world as a series of completely autonomous and closed objects. The method we write in the class is a encapsulation of the implementation details; we write a class is the encapsulation of data and data operations. It can be said that packaging is to hide everything that can be hidden, and only provide the simplest programming interface to the outside world (think about the difference between ordinary washing machines and fully automatic washing machines. It is obvious that fully automatic washing machines are better packaged and therefore easier to operate; we now use The smart phone is also packaged well enough, because just a few buttons do everything).

  • Polymorphism: Polymorphism refers to allowing objects of different subtypes to respond differently to the same message. Simply put, it calls the same method with the same object reference but does different things. Polymorphism is divided into compile-time polymorphism and runtime polymorphism. If the method of the object is regarded as the service provided by the object to the outside world, then the runtime polymorphism can be explained as: when A system accesses the service provided by B system, B system has multiple ways to provide services, but everything is right for A The system is transparent (just like the electric shaver is A system, its power supply system is B system, B system can use battery power or AC power, or even solar energy, A system will only pass B type The object invokes the method of power supply, but does not know what the underlying implementation of the power supply system is, and how it gained power). Method overload realizes compile-time polymorphism (also called pre-binding), and method override (override) realizes runtime polymorphism (also called post-binding). Runtime polymorphism is the essence of object-oriented. To achieve polymorphism, you need to do two things: 1). Method rewriting (the subclass inherits the parent class and overrides the existing or abstract methods in the parent class); 2). Object modeling (using the parent type to refer to the subtype object, so that the same reference calling the same method will show different behaviors according to the subtype object).

2.What is the difference between the access modifiers public, private, protected, and when not written (default)?

answer:

Modifiers currently similar to other packages

public √ √ √ √

protected √ √ √ ×

default √ √ × ×

private √ × × ×

When the members of the class do not write access modification, the default is default. The default is equivalent to public (public) for other classes in the same package, and equivalent to private (private) for other classes that are not in the same package. Protected is equivalent to public for subclasses, and private to classes that are not in the same package and have no parent-child relationship. In Java, the modifiers of external classes can only be public or default, and the modifiers of class members (including internal classes) can be the above four.

3. Is String the most basic data type?

Answer: No. There are only 8 basic data types in Java: byte, short, int, long, float, double, char, boolean; in addition to primitive types and enumeration types, the rest are reference types ( reference type).

4. float f=3.4; is it correct?

Answer: Not correct. 3.4 is a double-precision number. Assigning a double-precision type (double) to a floating-point type (float) belongs to down-casting (also called narrowing), which will cause precision loss, so it is necessary to force type conversion float f = (float ) 3.4; Or written as float f = 3.4F;.

5. Short s1 = 1; s1 = s1 + 1; is there any error? short s1 = 1; s1 += 1; is there any error?

Answer: For short s1 = 1; s1 = s1 + 1; Since 1 is an int type, the result of the s1+1 operation is also an int type, and the type needs to be cast to be assigned to the short type. And short s1 = 1; s1 += 1; can compile correctly, because s1+= 1; is equivalent to s1 = (short)(s1 + 1); there is an implicit type conversion.

6. Does Java have goto?

Answer: goto is a reserved word in Java and is not used in the current version of Java. (According to the appendix of the book "The Java Programming Language" written by James Gosling (the father of Java), there is a list of Java keywords, including goto and const, but these two keywords are currently unusable, so In some places, they are called reserved words. In fact, the word reserved words should have a broader meaning, because programmers familiar with the C language know that words or word combinations with special meanings used in the system library are all Is considered a reserved word)

7. What is the difference between int and Integer?

Answer: Java is an almost pure object-oriented programming language, but for the convenience of programming, basic data types are introduced. However, in order to be able to treat these basic data types as objects, Java introduces corresponding packaging for each basic data type. Type (wrapper class), the wrapper class of int is Integer. Since Java 5, an automatic boxing/unboxing mechanism has been introduced to make the two convertible.

Java provides a packaging type for each primitive type:

  • Primitive types: boolean, char, byte, short, int, long, float, double

  • Packaging types: Boolean, Character, Byte, Short, Integer, Long, Float, Double

Recently I encountered an interview question, which is also related to automatic boxing and unboxing. The code is as follows:

If you don’t know it, it’s easy to think that both outputs are either true or false. The first thing to note is that the four variables f1, f2, f3, and f4 are all Integer object references, so the following == operation compares not values ​​but references. What is the essence of boxing? When we assign an int value to an Integer object, we will call the static method valueOf of the Integer class. If you look at the source code of valueOf, you know what happened.

IntegerCache is an internal class of Integer, and its code is as follows:

Simply put, if the value of the integer literal is between -128 and 127, then the new Integer object will not be new, but the Integer object in the constant pool will be directly referenced, so f1 in the above interview questionThe result of f2 is true, and f3The result of f4 is false.

Reminder: The more seemingly simple interview questions, the more mystery will be, and the interviewer needs to have considerable skills.

8. Explain the usage of stack, heap and static area in memory.

Answer: Usually we define a variable of a basic data type, a reference to an object, and the on-site storage of function calls use the stack space in memory; while the objects created by the new keyword and the constructor are placed in the heap space; program Literals such as 100, "hello" and constants written directly in are placed in the static area. The stack space is the fastest to operate but the stack is small. Usually a large number of objects are placed in the heap space. In theory, the entire memory is not used by other processes and even the virtual memory on the hard disk can be used as a heap space.

In the above statement, the variable str is placed on the stack, the string object created with new is placed on the heap, and the literal "hello" is placed in the static area.

9. When an object is passed as a parameter to a method, the method can change the properties of the object and return the changed result. So, is it passed by value or by reference?

Answer: It is value transfer. Method calls in Java language only support parameter value passing. When an object instance is passed to a method as a parameter, the value of the parameter is a reference to the object. The properties of the object can be changed in the process of being called, but changes to the object reference will not affect the caller. In C++ and C#, the value of the incoming parameter can be changed by passing by reference or passing out the parameter. You can write the code shown below in C#, but you can't do it in Java.

Note: It is very inconvenient that no reference is passed in Java. This is still not improved in Java 8. This is why a large number of Wrapper classes will appear in the code written in Java (the reference will need to be modified by method call Put it in a Wrapper class, and then pass the Wrapper object to the method). This approach will only make the code bloated, especially for developers who transform from C and C++ to Java programmers.

10. The difference between Overload and Override. Can overloaded methods be distinguished based on the return type?

Answer: Method overloading and rewriting are both ways to achieve polymorphism. The difference is that the former implements polymorphism at compile time, while the latter implements polymorphism at runtime. Overloading occurs in a class. If the method with the same name has different parameter lists (different parameter types, different numbers of parameters, or both), it is considered overloading; overriding occurs between the subclass and the parent class, Overriding requires that the overridden method of the subclass has the same return type as the overridden method of the parent class, which is more accessible than the overridden method of the parent class, and cannot declare more exceptions than the overridden method of the parent class. Exchange principle). Overloading has no special requirements for the return type.

Interview question: Huawei’s interview question once asked such a question-"Why can’t I distinguish overloads based on the return type?", speak your answer!

11. Describe the principle and mechanism of JVM loading class files?

Answer: The loading of classes in the JVM is implemented by the ClassLoader and its subclasses. The class loader in Java is an important Java runtime system component, which is responsible for finding and loading classes at runtime The class in the file.

Due to the cross-platform nature of Java, the compiled Java source program is not an executable program, but one or more class files. When a Java program needs to use a certain class, the JVM will ensure that this class has been loaded, connected (verified, prepared, and parsed) and initialized. Class loading refers to reading the data in the class's .class file into memory, usually by creating a byte array to read into the .class file, and then generating the Class object corresponding to the loaded class. After loading, the Class object is not complete, so the class at this time is not yet available. When the class is loaded, it enters the connection phase. This phase includes three steps: verification, preparation (allocating memory for static variables and setting the default initial value) and parsing (replace symbolic references with direct references). Finally, the JVM initializes the class, including: 1) If the class has a direct parent class and the class has not been initialized, then initialize the parent class first; 2) If there are initialization statements in the class, execute these initialization statements in sequence.

The loading of the class is completed by the class loader, which includes: root loader (BootStrap), extension loader (Extension), system loader (System) and user-defined class loader (java.lang.ClassLoader) Subclass). Starting from Java 2 (JDK 1.2), the parent delegation mechanism (PDM) has been adopted for the class loading process. PDM better guarantees the security of the Java platform. In this mechanism, the Bootstrap that comes with the JVM is the root loader, and other loaders have and only have one parent loader. The loading of the class first requests the parent class loader to load, and the child class loader will load it when the parent class loader is powerless. The JVM does not provide a reference to Bootstrap to Java programs. The following is a description of several class loaders

Guess you like

Origin blog.csdn.net/weixin_52772147/article/details/112362559