Ten interview questions per day (Day 1)

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

1) Encapsulation: Encapsulation is to wrap the data and the process, the user can only know and use the properties and methods of the object, and does not know the specific implementation of the object. The data outside the object cannot be accessed at will. You can change its structure or implementation within the encapsulated object. As long as the interface of the object has not changed, the rest of the code does not need to be processed. This localizes the errors, greatly improves the maintainability of the program, and reduces the difficulty of checking and correcting errors.

2) Inheritance: Inheritance is a hierarchical model composed of related classes. It can reuse classes, and provides a way of expressing commonality, that is, derived classes (child classes) of the original class can inherit methods and instance variables from the base class (parent class), and the class can modify or add new methods to make More suitable for special needs. This greatly simplifies the process of designing new classes.

3) Polymorphism: Polymorphism is that the attributes and methods defined in the parent class are inherited by the subclass and can have different data types or exhibit different behaviors. Polymorphism includes parametric polymorphism and inclusion polymorphism. The Java editor will automatically judge based on the parameters passed, and perform different operations according to the type of the runtime object, which solves the problem of application function with the same name. Polymorphism enriches the content of objects, expands the adaptability of objects, and changes the relationship of single inheritance of objects.

2. The scope of public, private, and protected modifiers, and the difference when not writing modifiers?

1) Variables and methods using the public modifier can be accessed after creating an object in any class, including the current class, the same package class, descendant classes, and outsourced classes

2) Variables and methods that use protected modifiers cannot access objects created by outsourced classes.

3) Variables and methods that do not write modifiers are the default class and can only access objects created by classes in the same package.

4) Variables and methods using the private modifier can only be used in the current class.

3. Is String the most basic data type?

No.

The data types in the Java language are divided into two categories, namely basic data types and reference data types.

The basic data types are divided into:

Integer type: byte type (byte), short integer (short), integer (int), long integer (long)

Floating-point type: single-precision (float), double-precision (double)

Character (char)

Logical (boolean)

Reference data types are divided into:

Interfaces, objects, classes, arrays, strings, collections, etc.

4. Is float type float f = 3.4 correct?

Incorrect.

Implicit conversion, that is, automatic conversion, it is the conversion from small to large:

byte——》short——》int——》long——》float——》double

char——》int——》long——》float——》double

It does not require a statement and is the system default.

Explicit conversion, that is, forced conversion, it is from large to small conversion, its format is:

Type variable = (type) variable 2;

In Java, the default without a decimal point is int, and the default with a decimal point is double, which should be written as:

float f=(float)3.4 或float f = 3.4 f

5. The statement float f = 1.3; can the compilation pass?

Can't.

The reason is the same as above. It should be coercive and written as float f = (float) 1.3 or float f = 1.3 f

6. Short s1 = 1; s1 = s1 + 1; what's wrong? Short s1 = 1; s1 + = 1; what's wrong?

short s1 = 1; s1 = s1 + 1; wrong

The Java specification has such rules: 1. High bit to low bit requires forced conversion 2. Low bit to high bit automatic transfer.

i is int type, s1 is short type, s1 is automatically converted to int type after adding 1 operation, so forced conversion

short s1 = 1; s1 + = 1; correct

This type of upgrade will not happen, the JAVA specification says:

e1 + = e2 is actually e1 = (T1) (e1 + e2), where T1 is the data type of e1.

s1 + = 1 is equivalent to s1 = (short) (s1 + 1), so it is correct.

7. Does Java have goto?

No.

goto and const are reserved words in Java and are not used in Java now

8. What is the difference between int and Integer?

1) Integer can be null, but int cannot;

2) Integer can be used for generics, but int cannot;

3) int is more efficient than Integer, because int is a specific value stored directly in the stack, and Integer is a reference to store the value in the heap;

4) The values ​​of two Integers can be the same, but they are not equal;

5) Automatic boxing (the original data type is converted to the reference data type) and automatic unboxing (the reference data type is converted to the original data type)

9. What is the difference between & and &&?

The operator "&&" is a concise and operator, and the operator "&" is a non-concise and operator, and their differences are as follows:

1) The operator "&&" only calculates the logical expression on the right side when its left side is true, otherwise directly returns the operation result false;

2) Whether the operator "&" is true or false on the left, it must calculate the logical expression on the right, and finally return the operation result.

10. Briefly describe the difference between logical operations (&, |, ^) and conditional operations (&&, ||)?

1) Conditional operations can only operate on Boolean types, and logical operations can not only operate on Boolean types, but also numeric types

2) The logic operation will not cause a short circuit.

Published 23 original articles · praised 39 · visits 2704

Guess you like

Origin blog.csdn.net/abc701110/article/details/105500452