Here I want to say, not Java primary and learning Java do not disorderly look, otherwise ~~~~

You will doubt life, because it will waste your time!!

This is an interview for a Java junior development engineer.

1. Data types

Basic types of

Short /16, int/32, long/64, Boolean /, char/16, float/32, double/64Copy the code

Boolean has only two values, true and false, and can be stored in 1bit, but the size is not specified. The JVM converts the Boolean type to int at compile time, using 1 for true and 0 for false. The JVM supports Boolean arrays, but does so by reading and writing byte arrays. > How to get information: JAVA Architecture Crash Notes

Packing type

Each basic type has a corresponding package type, and the assignment between the basic type and its corresponding package type is done using automatic packing and unpacking.

Integer.valueof (2) int y=x // unboxing xi.ntValue ()Copy the code

Buffer pool

The difference between new Integer(123) and integer.valueof (123) is that new Integer(123) creates an object each time. Integer.valueof (123) uses objects in the cache pool, and multiple calls to the same object retrieve a reference. Integer x = new Integer(123); Integer y = new Integer(123); System.out.println(x == y); // false Integer z = Integer.valueOf(123); Integer k = Integer.valueOf(123); System.out.println(z == k); // trueCopy the code

The valueOf() method is simple to implement. It checks whether the value is in the cache pool and returns the contents of the cache pool if it is.

2, the String

An overview of

String is declared final, so it cannot be inherited. (Wrapper classes such as Integer cannot be inherited.)

In java8, strings use char arrays internally to store data.

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];
} 
Copy the code

After java9, implementations of the String class switch to byte arrays to store strings, and use the coder to identify which encoding was used.

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final byte[] value;

    /** The identifier of the encoding used to encode the bytes in {@code value}. */
    private final byte coder;
} 
Copy the code

The value array is declared final, which means that once the value array is initialized, it cannot reference any other array. And there’s no way to change the value array inside a String, so it’s guaranteed to be immutable.

The benefits of immutability

1. You can cache hash values

Because String hash values are often used, such as String as the key of a HashMap. The immutable nature makes the hash value immutable, so it only needs to be evaluated once.

2, String Pool needs

String constant Pool. If a String object has already been created, the reference will be retrieved from the String Pool. String Pool can only be used if String is immutable.

3. Security

String is often used as a parameter, and String immutability ensures that the parameter is immutable.

4. Thread safety

String immutability is inherently thread-safe and can be used safely across multiple threads.

String Pool

The String constant Pool holds all of these String literals, which are determined at compile time. Not only that, you can also use the String intern() method to add strings to the String Pool at run time.

  • When a string is calledintern()Method if ‘String

If a String already exists in a Pool that equals that String (using equals), return a reference to the String in the Pool. Otherwise, a new String is added to String Pool and a reference to the new String is returned. > How to get information: JAVA Architecture Crash Notes

For example, s1 and S2 create two different strings using new String(), while S3 and S4 get the same String reference using s1.intern() and s2.Intern (). Intern () first puts “AAA” into the String Pool and then returns a reference to the String, so S3 and S4 refer to the same String.

String s1 = new String("aaa"); String s2 = new String("aaa"); System.out.println(s1 == s2); // false String s3 = s1.intern(); String s4 = s2.intern(); System.out.println(s3 == s4); // trueCopy the code

If you create a String as a “BBB” literal, the String is automatically placed in the String Pool.

String s5 = "bbb";
String s6 = "bbb";
System.out.println(s5 == s6);  // true
new String("abc") 
Copy the code

There are two common String objects in this way (provided there is no “ABC” String object in the String Pool).

ABC is a String literal, so compile time creates a String object in the String Pool that points to this “ABC” String literal and using new creates a String object in the heap.


3, operation

Parameter passing

Java parameters are passed to methods as values, not references.

When you pass an object as an argument to a method, the object name actually represents the address; that is, the address of the object is passed to the method. Objects can be changed at run time.

Changing the value of an object’s field in a method changes the value of the field in the original object because it refers to the same object.

Float and double

Java cannot implicitly perform a downward transformation because it would reduce accuracy

Literals are of type double. You cannot assign 1.1 directly to float because this is a downward transition.

Float f=1.1 // This will compile an error 1.1f literals are only float types // join Java development communication with 756584822 blow water chat float f=1.1f hermit type conversionCopy the code

Because the literal 1 is an int, it is more precise than short, so you cannot implicitly cast an int down to a short

4. Keywords

final

1, the data

Declare data as constants, either at compile time or after initialization at run time that cannot be changed.

For base types, final leaves the data unchanged

For reference types, final leaves the reference unchanged and therefore cannot refer to other objects, but the referenced object itself can be modified.

final int x = 1; // x = 2; Final A y = new A(); y.a = 1;Copy the code

2, methods,

Declaration methods cannot be overridden by subclasses.

Private methods are implicitly specified as final. If a method defined ina subclass has the same signature as a private method in the base class, the subclass’s method does not override the base class method, but defines a new method in the subclass.

3, class,

Declare that classes are not allowed to be inherited.

static

1. Static variables

Static variable: Also known as a class variable, that is, the variable belongs to the class. All instances of the class share a static variable and can access it directly through the class name. Only one copy of a static variable exists in memory. > How to get information: JAVA Architecture Crash Notes

Instance variables: Without creating an instance, an instance variable is created, which lives and dies with the instance.

public class A { private int x; Private static int y; Public static void main(String[] args) {// int x = A.x; // Non-static field 'x' cannot be referenced from a static context A a = new A(); int x = a.x; int y = A.y; }}// Join Java development communicationCopy the code

2. Static methods

  • Static methods exist when the class is loaded and do not depend on any instance. Any static method must have an implementation, that is, it cannot be abstract.
  • Static methods can only access static fields and static methods of the class they belong to. Methods cannot have the this and super keywords, so they are associated with specific objects.

3. Static statement blocks

A static statement block is run once during class initialization

public class A { static { System.out.println("123"); } public static void main(String[] args) { A a1 = new A(); A a2 = new A(); }}Copy the code

4. Static inner class

A non-static inner class depends on an instance of an external class, which means that an external instance must be created before it can be used to create a non-static inner class. Static inner classes do not.

5. Initialization sequence

Static variables and static statement blocks take precedence over instance variables and normal statement blocks, and the order in which they are initialized depends on the order in which they are placed in the code.

Public static String staticField = "static "; Static {system.out.println (" static statement block "); } public String field = "instance "; System.out.println(" plain statement block "); }Copy the code

Finally, the constructor is initialized

Public InitialOrderTest() {system.out.println (" constructor "); }Copy the code

In the case of inheritance, the initialization sequence is:

Parent class (static variable, static statement block) Subclass (static variable, static statement block) Parent class (Instance variable, ordinary statement block) Subclass (constructor) Subclass (instance variable, ordinary statement block) Subclass (constructor)Copy the code

5, inheritance,

Access permissions

There are three access modifiers in Java: private, protected, and public. Without access modifiers, this means package-level visibility.

You can add access modifiers to a class or its members (fields and methods).

  • Class visibility means that other classes can use this class to create instance objects
  • Member visibility means that other classes can use instance objects of this class to access the modified member.
  • Protected is used to modify members, meaning that in inheritance the member is visible to subclasses, but the access modifier has no meaning for the class.

If a method of a subclass overrides a method of the parent class, the access level of that method in the subclass is not allowed to be lower than that of the parent class. This is to ensure that subclass instances can be used wherever parent instances can be used, ensuring that the in-substitution rule is met. > How to get information: JAVA Architecture Crash Notes

Fields should never be shared, because doing so gives the client control over how the field is modified. We can use public getters and setters to replace public fields, so that we can control the modification behavior of fields.

Abstract classes and interfaces

1.An abstract class

Both abstract classes and abstract methods are declared using the abstract keyword. If a class contains abstract methods, the class must be declared abstract.

The biggest difference between abstract classes and ordinary classes is that abstract classes cannot be instantiated, only inherited.

public abstract class AbstractClassExample { protected int x; private int y; public abstract void func1(); public void func2() { System.out.println("func2"); } } public class AbstractExtendClassExample extends AbstractClassExample { @Override public void func1() { System.out.println("func1"); // AbstractClassExample ac1 = new AbstractClassExample(); // 'AbstractClassExample' is abstract; cannot be instantiated AbstractClassExample ac2 = new AbstractExtendClassExample(); ac2.func1();Copy the code

2, interfaces,

An interface is an extension of an abstract class. Prior to java8, it could be considered a completely abstract class, that is, it could not have any method implementation.

Starting with java8, interfaces can also have default method implementations, because interfaces that do not support default methods are too expensive to maintain. Prior to Java 8, if an interface wanted to add a new method, all classes that implemented the interface were modified so that they implemented the new method.

Interface members (fields + methods) are public by default and are not allowed to be private or protected. Since Java 9, it has been possible to define methods as private so that some reusable code can be defined without exposing methods.

Interface fields are static and final by default.

Rewriting and overloading

1, rewrite

In inheritance, a subclass that implements a method that is identical in method declarations to its parent class.

In order to satisfy the li substitution principle, rewriting has the following three limitations:

  • Subclass methods must have greater access than or equal to the parent method

  • The return type of a subclass method must be the parent method return type or a subtype

  • The exception type thrown by a subclass method must be either a parent or a child

2, overloading

A method that exists in the same class has the same name as an existing method but has at least one different parameter type, number, and order.

It should be noted that if the return value is different and everything else is the same it’s not an overload.


reflection

Each Class has a Class object that contains information about the Class. When a new class is compiled, a.class file of the same name is generated, the contents of which hold the class object.

Class loading is equivalent to the loading of Class objects, and classes are dynamically loaded into the JVM the first time they are used. You can also control the loading of classes using class.forname (” com.mysql.jdbc.driver “), which returns a Class object.

Reflection can provide information about a class at run time, and the class can be loaded at run time, even if the class’s.class does not exist at compile time.

Class provides support for reflection along with java.lang.reflect. The Java.lang. Reflect library consists of three classes:

  • Field: The fields associated with a Field object can be read and modified using the get() and set() methods

  • Method: You can use the invoke() Method to call a Method associated with a Method object

  • Constructor: Use Constructor and newInstance() to create new objects

Advantages of reflection:

  • Extensibility: Applications can use user-defined classes from outside by creating instances of extensible objects with fully qualified class names.
  • Class browser and visual development environment: A class browser needs to be able to enumerate members of a class. Visual development environments, such as ides, can benefit from the type information available in reflection to help programmers write correct code.
  • Debuggers and test tools: Debuggers need to be able to examine private members of a class. Reflection can be used by testing tools to automatically invoke discoverable API definitions defined in classes to ensure high code coverage in a set of tests

Disadvantages of reflection:

Although reflection is powerful, it should not be abused. If a function can be done without reflection, it’s best not to.

  • Performance overhead: Reflection is designed for dynamically typed parsing, so the JVM cannot optimize this code. Therefore, reflective operations are much less efficient than those that are not. We should avoid using reflection in code that is frequently executed or in programs that require high performance.
  • Safety restrictions: Using reflection techniques requires that the program be run in an environment with no safety restrictions. This is a problem if a program must run in an environment with security restrictions, such as an Applet.
  • Internal exposure: Because reflection allows code to perform operations that are not normally allowed (such as accessing private properties and methods), using reflection can cause unintended side effects.

In the end, I wish you all success as soon as possible, get satisfactory offer, fast promotion and salary increase, and walk on the peak of life.

If you can, please give me a three-way support for me