Java background development interview questions to share one

float f=3.4; is it right or not?

Incorrect.

In java, where the 3.4default is a double precision number. If double (double) assigned to the floating-point (float) belonging to the next transition (down-casting, also referred to as narrowing) will cause loss of precision, it is necessary cast float f = (float) 3.4or written float f = 3.4F.


&And &&the difference?

  1. &The operator is the "logical AND operator" for the Boolean type, and the "bitwise AND operator" for other types; the &&operator can only be the logical AND operator.

  2. At the same logical AND operators: &operators sides are calculated at calculation, and then determine; &&operator is shorted and operation, if the &&value of the expression on the left is false, the expression on the right will be directly shorted out, not Perform calculations.


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

Not possible.

When a static method is called, no instance object may have been created yet. If a call to a non-static method is issued from a static method, the non-static method cannot be associated with an object.


What is the difference between Integer and int?

Int is one of the eight primitive data types provided by java, and Integer is a wrapper class provided by java for int. The default value of int is 0, and the default value of Integer is null, that is, Integer can distinguish the difference between unassigned and value 0, and int cannot express the unassigned situation.

In JSP development, the default of Integer is null, so when the el expression is used in the text box, the value is a blank string, and the default value of int is 0, so when the el expression is used in the text box, The result is 0, so int is not suitable as the type of form data in the web layer.

In Hibernate, if the OID is defined as an Integer type, then Hibernate can determine whether an object is temporary based on whether its value is null. If the OID is defined as an int type, you also need to set its unsaved- in the hbm mapping file. The value attribute is 0.


This statement involves several objects:String s="a"+"b"+"c"+"d";

After this line of code is optimized by the compiler at compile time, it is equivalent to directly defining an "abcd" string; therefore, the above code only involves an object in the string pool, and s stores the reference address of this object .


String a = new String("ab");String b = a + "cd"; How many String objects are involved?

There are 4 involved.

Which new String("ab")involves two, respectively, and heap memory string constant pool; "cd" in a string constant pool, "abcd" a string constant pool.

Java has designed two different methods to generate string objects, one is to use double quotes, the other is to call the constructor of the String class.

When using double quotation marks to define an object, the Java environment first goes to the string buffer pool to find a string with the same content, and if it exists, it uses it, otherwise it creates a new string and puts it in the buffer pool.

When calling the constructor String class, i.e. with new String()time created, it creates an object is present in memory at runtime stack, then the value of the stack memory which point to a string object string constant pool.

In the "+" connection of the string, if there is a reference, the value of the reference cannot be determined during the compiling of the program, so a + "cd" cannot be optimized by the compiler. It can only be dynamically allocated during the runtime of the program. The new address after connection is assigned to b. So "cd" has an object in the string constant pool, a + "cd" will create an object at runtime.


Please briefly describe the difference between interfaces and abstract classes, and explain the significance of interfaces in actual development projects

  1. The keyword to define an abstract class is abstract class; the keyword to define an interface is interface.

  2. There can be variables and constants in abstract classes; there can only be static constants in interfaces.

  3. An abstract class can have a constructor, but an interface cannot have a constructor.

  4. There can be ordinary methods in abstract classes; before Java 8, there can only be abstract methods in interfaces. Jdk1.8 started to allow default methods and static methods in interfaces, and Java 9 started to allow private methods.

  5. When adding methods to an abstract class, subclasses do not need to be rewritten; before Java 8, the implementation class needs to be rewritten when adding methods to an interface. Starting from jdk1.8, the interface uses default methods to let the implementation class choose whether to rewrite.

  6. Support "single inheritance" between classes and use the extends keyword; classes and interfaces support "multiple implementations" and use the implements keyword; between interfaces and interfaces support "multiple inheritance" and use the extends keyword.

  7. From the perspective of object-oriented design: abstract classes belong to the "is-a" relationship; interfaces belong to the "like-a" relationship. is-aOn behalf of the inheritance relationship, if A is-a B, then B is the parent of A; has-aon behalf of the subordinate relationship, if A has a B, then B is a component of A; like-aon behalf of the combination relationship, if A like a B, then B is A's interface.


List<T> What is the difference with List?

Both are List collections, but one is generic and the other is Object type; when storing elements, the former limits its collection element type to T, and the latter collection elements can be of any type; when a coercive type conversion occurs, The former can find type conversion errors during compile time, and the latter will report errors during runtime.

Generics in Java are only valid during compilation. After compilation, the program will take measures to de-generate. In the compilation process, after correctly checking the generic result, the related information of the generic will be erased, and the method of type checking and type conversion will be added at the boundary of the object entering and leaving the method. In other words, generic information will not enter the runtime phase.


The loading process of the class?

1. First load the parent class of the object to be created, and then load the class of the object to be created.

2. When the class and its parent class are loaded, static members will be loaded; it mainly includes the initialization of static member variables and the execution of static code blocks; when loading, proceed in the order of code.

3. After the required class is loaded, start to create the object; first, the non-static members of the class and its parent class will be loaded, including the initialization of non-static member variables and the execution of non-static code blocks (building blocks); during loading Follow the order of code. 

4. Finally execute the constructor (construction method body); after the constructor is executed, the object is generated.


Java development and operation process?

After installing the JDK and configuring the path, the development and operation steps are as follows:

1. You can use any text editor to create and edit Java source programs. Java source programs use ".java" as the file extension.

2, compiled Java source code, use the command "javac" javac 源程序文件名.javacompiler: . It is then compiled into a set of instructions that the Java virtual machine can understand, and saved in a file in the form of bytecode. Usually, bytecode files have ".class" as the extension.

3, the implementation of java program, use the "java" command runs the bytecode file: java 文件名. The Java virtual machine reads the bytecode, takes out the instructions and translates them into machine codes that can be executed by the computer to complete the running process.

Want to know more, welcome to follow my WeChat public account: Renda_Zhang

 

Guess you like

Origin blog.csdn.net/qq_40286307/article/details/108859036