java basic part

Keywords: Java network Back-end

Here are some common interview questions, analysis of test sites, answers, etc.

Basic concepts

Some basic java knowledge, various basic concepts, etc.

What are the access modifiers? difference?

Modifier Current classSame packageSubclassOther packages

   Scan VX for Java data, front-end, test, python and so on

String   Is it the most basic data type?

    Java There are only 8 basic data types in:

short s1 = 1; s1 = s1 + 1; Is there anything wrong? short s1 = 1; s1 += 1; Is there anything wrong?

about short s1 = 1; s1 = s1 + 1;
  Because 1 is int Type, so s1+1 So is the result of the operation int Type, cast type is required to assign to short Type.

and short s1 = 1; s1 += 1;
  Can compile correctly because s1+= 1;amount to s1 = (short)(s1 + 1);There is an implicit cast.

What is Math.round(11.5) equal to? What is Math.round(-11.5) equal to?

Answer: Math.round(11.5)The return value of is 12, Math.round(-11.5)The return value of is-11. 
The principle of rounding is to add 0 to the parameter.5 Then round down.

Can switch work on byte, long and String?

(1)stay Java 5 Before, switch(expr)In, expr Only byte,short,char,int. 
(2)from Java 5 Start, Java Enumeration types are introduced in, expr It can also be enum Type,
(3)from Java 7 Start, expr It can also be a string( String). 
**But long integer( long)It is not allowed in all current versions.**

Can a constructor be overridden?

A: constructors cannot be inherited, so they cannot be overridden, but they can be overloaded.

What are the functions and differences between equals and hashcode?

**effect: hashCode()and equals()The role of is actually the same,
The purpose is to java Compare whether the two objects are equal and consistent**
One is performance and the other is reliability
 Because rewritten equals()In general, it is more comprehensive and complex(It will compare all member variables in this object one by one),
  This is inefficient, and through hashCode()For comparison, only one is generated hash Value can be compared, and the efficiency is very high
  because hashCode()It's not completely reliable, and it's very likely that two completely different objects hash The value is the same.

If you need a lot of and fast comparison equals()It is obviously inefficient to do so, so the solution is:
    1. Whenever you need to compare, first use hashCode()To compare, and if hashCode()If they are different, the two objects must be different,
    There is no need to use it at this time equals()Compared;
    2. If hashCode()If they are the same, the two objects may be the same. At this time, compare the two objects equals(),
    If equals()Also the same, it means that the two are really the same, which not only greatly improves the efficiency, but also ensures the accuracy.

What is the difference between equals and =?

(1),Comparison of foundation types
    use==Compare values for equality.
(2),Reference type comparison
    ①Rewritten equals Methods, such as String. 
        First case: use==The comparison is String Does the reference to point to the same block of memory
        Second case: use equals The comparison is String Whether the referenced object contents of are equal.
    ②No rewriting equals Methods, such as User Other custom classes
    ==and equals The comparison is whether the reference points to the same block of memory
 Example 1:
string x = "string";
string y = "string";
string z = new String("string");
system.out.printIn(x==y); // true
System.out.println(x==z);// false
system.out.printIn(x.equals(y)); // true
System.out.printIn(x.equals(z)); // true
 Code interpretation:
  because x and y It points to the same reference, so==Also true,
  and newString()Method opens up memory space, so==The result is false,
  and equals Values are always compared, so the results are true.

equals Essentially==,Just String and Integer When it's rewritten equals Method, turning it into a value comparison.

   Scan VX for Java data, front-end, test, python and so on

What is the difference between String, StringBuilder and StringBuffer?

String Is a read-only string, which means String The content of the referenced string cannot be changed.
StringBuilder yes Java 5 Introduced in, it and StringBuffer In exactly the same way,
The difference is that it is used in a single threaded environment because all aspects of it are not used synchronized modification,
So it's also more efficient than StringBuffer Higher.

String+The essence of the operation is to create StringBuilder Object append Operation,
Then the spliced StringBuilder Object use toString Method processing into String Object,
This can be used javap -c Test.class Command acquisition class File corresponding JVM Bytecode instructions can be seen.

Can abstract methods be static, native, and synchronized?

Not at all.
    Abstract methods need subclass rewriting, while static methods cannot be rewritten, so the two are contradictory.
    Local methods are generated by local code, such as C Code) implementation method, while abstract method is not implemented, which is also contradictory.
    synchronized It is related to the implementation details of methods. Abstract methods do not involve implementation details, so they are also contradictory.

What is the difference between a finally code block and a finalize() method?


Talk about the classification of exceptions in Java

Throwable -> Error,Exception
Error:Serious problems, such as memory overflow
Exception ->Runtime exception: RuntimeException,Compile time exception

What is the difference between Exception and Error in Java?

Same as:
  Both inherit from the Throwable class
  Exception is a predictable exception in the running of java program, which is divided into runtime exception and checked exception. Error class generally refers to
  Application interruption caused by virtual machine related problems, such as system crash, virtual machine error, insufficient memory space, etc.


What are the characteristics of object-oriented?

1. Abstraction: the process of constructing a class by summarizing the common characteristics of a class of objects, including data abstraction and behavior abstraction
2. Inheritance: inheritance is the process of obtaining inheritance information from an existing class and creating a new class. Classes that provide inheritance information are called parent classes (superclasses and base classes);
  The class that gets inheritance information is called a subclass (derived class).
3. Encapsulation: encapsulation is generally considered to bind data and methods of operating data, and access to data can only be through defined interfaces.
4. Polymorphism: polymorphism means that objects of different subtypes are allowed to respond differently to the same message.
  To put it simply, you call the same method with the same object reference, but do different things.

The difference between Overload and Override. Can overloaded methods be distinguished according to the return type?

Method overloading and rewriting are ways to achieve polymorphism,
  The difference is that the former implements compile time polymorphism, while the latter implements runtime polymorphism.

Overloading occurs in a class. If a method with the same name has a different parameter list
  (If the parameter type is different, the number of parameters is different, or both are different), it is regarded as overload; overload has no special requirements for return type.
Rewriting occurs between child and parent classes. Rewriting requires the same return type,
  It is more accessible than the overridden method of the parent class and cannot declare more exceptions than the overridden method of the parent class (Richter substitution principle).

Talk about the "six principles and one rule" of object-oriented?

Single responsibility principle
 A class does only what it should do.
Opening and closing principle
 A software entity should be open to extensions and closed to modifications. That is, the software entity should try to expand without modifying the original code.
Dependence Inversion Principle 
The program depends on the abstract interface, not on the concrete implementation.
Richter substitution principle
 You can replace the parent type with a subtype at any time.
Interface isolation principle
 The interface should be small and specialized, not large and complete.
Synthetic aggregation Reuse Principle
 Reuse code using aggregation or composition relationships is preferred.
Dimitt's law
 An object should know as little as possible about other objects, also known as the principle of least knowledge.

How to implement object cloning?

 1). realization Cloneable Interface and override Object Class clone()method;
 2). realization Serializable Interface to realize cloning through object serialization and deserialization, which can realize real deep cloning

   Scan VX for Java data, front-end, test, python and so on


Posted by Kingskin on Thu, 25 Nov 2021 11:11:47 -0800