Basic types of

  • Byte 8-bit binary (1 byte)
  • Char 16-bit binary
  • Short 16-bit binary
  • Int 32-bit binary
  • Long 64-bit binary
  • Float 32-bit binary
  • Double 64-bit binary
  • Boolean The JVM converts Boolean data to int at compile time, using 1 for true and 0 for false

Automatic boxing and unboxing (introduced in JDK 1.5)

Autoboxing: A method that automatically wraps data of primitive types in wrapper types to enable the use of objects.

Integer a = 2; Equivalent to Integer a = integer.valueof (2);Copy the code

Automatic unpacking: The package type is automatically unpacked to the basic type

The package type is automatically disassembled during calculation

When a wrapper type hits the cache pool

(1) Integer Cache pool

Integer Specifies the range of the cache pool, ranging from -127-128, within which objects used by interge.valueof () hit the cache pool and use the same Integer object.

If it’s new Integer(4) an Integer created this way doesn’t hit the cache, it’s a new object on the heap every time.

Integer.valueof () is the same as new Integer() after the cache pool is exceeded, creating a new object

Short, Character, and Long all hit their respective cache pools when using numbers in the range -127-128

Integer Cache pools after JDK 1.8 can be resized at startup by specifying -xx :AutoBoxCacheMax. Other types of cache pools cannot be resized

(2) Float double has no cache pool

The number of possible floating-point results is infinite up to a certain point, so there is no way to set a cache pool

How to implement cache pooling

The underlying cache pool is a final array, created by the JVM at startup time

String

Creating a String

(1) String a = “aa”; A = string.valueof (“aa”)

  • 0 to 1 objects will be created
  • If there is “AA” in the string cache pool, the object in the cache pool is returned directly, and 0 objects are created
  • If there is no “AA” in the string cache pool, an object is created in the cache pool, and a reference to the object in the cache pool is returned. One object is created

(2) String a = new String(“aa”);

  • One or two objects are created
  • If there is “aa” in the string cache pool, create an object in the heap, return a reference to the object in the heap, and create 1 object
  • If there is no “AA” in the string cache pool, create an object in the cache pool, create another object in the heap, return a reference to the object in the heap, and create two objects

immutability

(1) Immutable strings are not the same thing as immutable final variables

  • Final modifies a variable that cannot point to another object
  • Char [] cannot refer to another object, but a reference to a String can refer to another object.

(2) The char[] array in String cannot be modified

String does not provide methods for modifying char[] arrays. There are no references to the char[] array other than the String itself, so it cannot be modified. Each time a String() object is reassigned, a new object is created. Char [] represents the original array, and no reference can be made to it.

Immutability benefits

(1) If the hash value remains unchanged, you can determine whether the string is the same according to the hash value

A String object, once created, its char[] array is immutable and therefore its hash value is also immutable

(2) Cache pool, which can be reused

Because of immutability, a String cache pool can be used for a lot of reuse, reducing String creation

A String buffer pool

Because of immutability, unlike the Integer buffer pool, when creating a String object, an attempt is made to retrieve the object from the buffer pool at any time.

Before jdk1.7, string cache pools existed in the method area, and after jdk1.7, string cache pools existed in the heap

String.intern () method

Before jdk1.7 because the heap and String constant pool were separate, because string.intern () simply added strings to the String constant pool and returned applications in the constant pool.

String s3 = new String("1") + new String("1"); S3.intern (); // Create 11 objects in the heap. // Create 11 objects in the constant poolCopy the code

After jdk1.7, however, the heap and the String constant pool are joined together. The string.intern () method does not only fetch objects from the constant pool, but also looks for objects in the heap. If it finds the object, it returns a reference to the object in the heap, not the constant pool

String s3 = new String("1") + new String("1"); S3.intern (); // Create 11 objects in the heap. String s4 = "11"; String s4 = "11"; Println (s3 == s4); system.out.println (s3 == s4); // Are objects in the heap, so trueCopy the code

So after jdk1.7, it’s up to you to decide which constant pool or heap object of the same content is created first. The heap is created first, so both the heap and the constant pool point to the heap, and vice versa.

String collection

Objects in the heap can definitely be GC

Before jdk1.7, the string constant pool belongs to the method section, is a permanent generation, will not be GC, so there is an OOM risk

After jdk1.7, the string constant pool is in the heap and can be GC

Thread safety

  • Because strings are immutable, they must be thread-safe
  • StringBuilder is not thread-safe and is often used
  • Stringbuffers are thread-safe and use synchronized internally

final

decorator

The purpose is to declare only that classes cannot be inherited

Declare a variable

  • Basic types: immutable values (for basic types such as int and char, which can be changed for encapsulated classes in the same way as for reference types)
  • Reference type: This reference cannot point to another object (the value of the object can be changed by reflection).

Reflection changes the value of the encapsulation type (as does changing the reference type)

    private static Integer a = 200;
    private static final Integer b = a;

    public static void main(String [] args) throws NoSuchFieldException, IllegalAccessException {
        Class aC = a.getClass();
        Field value = aC.getDeclaredField("value");
        value.setAccessible(true);
        value.setInt(a, 300);
    }
Copy the code

The value of final b has been changed to 300

The declarative approach

This convenience cannot be overridden by subclasses

static

Modify variables

This variable belongs to the Class object of the Class and will have only one copy in memory. All instances share class variables (that is, static variables)

Such as:

private int x = 0; Private static int y = 0; // Static variablesCopy the code

Modification methods

Static methods can only call static methods and use static variables, not non-static variables and methods (because non-static variables and non-static methods may not be loaded).

Static methods are created as the Class is loaded, so they can be called through the Class object.

Modify code block

It is executed once at class initialization, because class loads are only executed once in the same JVM, so static code blocks are executed only once.

Order of execution:

Static code block -> non-static code block -> constructor -> dependency injection -> @postConstruct (execute once) decorated method

Modify classes (inner classes only)

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

A static inner class cannot access the non-static variables and methods of an external class.

A non-static inner class can hold a reference to an instance of an external class, so an instance of an external class cannot be GC. Therefore, inner classes are advised to use only static inner classes to prevent memory overflow.

Clone () method

Java provides a Cloneable interface that you can copy objects by inheriting and implementing the Clone() method. It’s a specification that doesn’t do anything for you

Shallow copy

If all you do in the clone() method is create a reference to the original object, then the shallow copy will refer to the same object as the original reference

Deep copy

If you’re in the clone() method yourself, what you’re doing is creating a new object. And then you manually assign it yourself, so the deep-copy reference points to a different object than the original reference

A reasonable way to copy objects

Clone () doesn’t have much use. To copy, you just need to implement a factory method. The difference is to create a reference or an object.

inheritance

The modifier

Modifiers modify variables and methods

private

Can only be used by code in a class, not by an instance of a class

public

You can use it from an instance of a class, anywhere

protected

Package visible, meaning that belonging to the same package is visible to subclasses (whether subclasses are visible within a package or not).

However, when a subclass of a different package class is not the same as the parent class, using a subclass of a different package class.

  • That is, the parent class A has A protected method call() in package 1
  • Subclass B is in package 2 and can use the call() method of its parent class
  • Subclass C in package 2 can use the call() method of its parent class, but cannot use the call() method of B B = new B(), b.call()

An abstract class

  • Abstract modified class
  • Variables are allowed
  • Method logic is implemented by allowing ordinary methods
  • Allow abstract methods that specify only method names, parameters, returns, and modifiers, without code logic

Everything in an abstract class can be modified with private, public, and protected

interface

  • Interface modified class
  • Variables are allowed: only final static
  • Interface methods are allowed: there is no implementation logic
  • Default methods are allowed: after JDk1.8. Implements the default logic

All contents of an interface must be public (as of Java 9, methods are allowed to be private), and interface methods in interfaces must be overridden (default methods are not mandatory).

Why is there a default method

Starting with Java 8, 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 instantiation

First, interfaces cannot be instantiated, but interfaces can be anonymous classes, eliminating the need to write a class that inherits the interface.

For example, a Comparator is an interface. In addition to creating a class that inherits the interface, you can also create anonymous inner classes as follows, but anonymous inner classes are not referenced, that is, they can only be used as parameters of a method, and cannot be reused.

new Comparator() { @Override public int compare(Object o1, Object o2) { return 0; } @Override public boolean equals(Object obj) { return false; }};Copy the code

A subclass

Superclass constructor

  • You can use super to represent a reference to a parent class
  • Super. a gets the value of the a attribute of the parent class
  • Super () calls a constructor whose parent class has no arguments
  • Super (x, y) calls the constructor whose parent class has arguments

If the parent class does not have a constructor that has no arguments, then all constructors in the subclass must use the super(XXXX) form to initialize the constructor of the parent class.

methods

rewrite

That is, a subclass reimplements the logic of its parent class with the same name as the attribute method

overloading

It is possible to specify a method whose name is the same as its return, but whose argument list is inconsistent (including the argument type and argument order).