Chapter 6 notes static, final and constant design

Posted by ojsimon on Thu, 03 Mar 2022 16:16:25 +0100

Chapter 6 notes static, final and constant design

Section 1 static

  • Static static, a special keyword in Java

    - Variable - Method - class - anonymous method block

Static variable, class common member

  1. The static variable only depends on the existence of the class (accessed through the class)
  2. The values of static variables of all object instances are shared and stored in a common space (stack)

Static method

  1. The static method is directly referenced by the class name
  2. Only static variables can be used in static methods
  3. Non static methods can call static methods, while static methods cannot call non static methods

static block

  1. Called only when the class is first loaded
  2. In the process of running the program, this code only runs once
  3. Execution order: static block > anonymous block > constructor

Section 2 single case mode

  • Singleton mode is also called singleton mode
  • Limit a class to keep only one instance object in the memory space during the whole program running, that is, the class can only be new once
  • Singleton mode: ensure that a class has and only has one object
    - use static to share object instances
    - use the private constructor to prevent external new operations

Section III final

  • final modification
    - class
    - Method
    - field
  • final class cannot be inherited
  • If there is a final method in the parent class, the method cannot be overridden in the child class
  • final variable cannot be assigned again
    1. The value of a basic variable cannot be modified
    2. The object instance cannot modify the pointer to point to a new object (memory space), but can modify the interior of the object

Section 4 constant design and constant pool

Constant design

  • Constant: an amount whose value remains constant
    - there is no const keyword in Java
    - cannot be modified, final
    - no modification / read only / just one copy, static
    - easy access to public
  • Constants in Java
    —public static final
    - constant name all uppercase

Special constants: variables defined in the interface are constants by default

Constant pool

  • Constant pool: only one copy of the same value is stored to save memory and share access
  • Packing class of basic type
PackagingConstant pool
Float,DoubleNo cache (constant pool)

There are two ways to create wrapper classes and strings of basic types

  1. Constant (literal) assignment is created and placed in stack memory (will be constant quantized)
    • Integer a=10;
    • String b="abc";
  2. The new object is created and placed in heap memory (not often quantified)
    • Integer c=new Integer(10);
    • String d=new String("abc");

Tips: fast stack memory reading speed but small capacity; Heap memory is slow to read but large in capacity

Two ways to create an Integer
public class BoxClassTest {
	public static void main(String[] args)
		int i1 = 10;
		Integer i2 = 10;                //Automatic packing
		System.out.println(i1 == i2);   //true
		// The basic type of automatic unpacking is compared with the packaging type, and the packaging type is automatically unpacked
		Integer i3 = new Integer(10);
		System.out.println(i1 == i3);  //true
		// The basic type of automatic unpacking is compared with the packaging type, and the packaging type is automatically unpacked
		System.out.println(i2 == i3); //false
		// Compare two objects and compare their addresses 
		//i2 is a constant, which is stored in stack memory; i3 is a new object, which is stored in heap memory
		Integer i4 = new Integer(5);
		Integer i5 = new Integer(5);
		System.out.println(i1 == (i4+i5));   //true
		System.out.println(i2 == (i4+i5));   //true
		System.out.println(i3 == (i4+i5));   //true
		// i4+i5 makes I4 and i5 automatically unpack as the basic type and get 10
		// Comparing the basic type 10 with the object will enable the object to be unpacked automatically for basic type comparison
		Integer i6 = i4 + i5;  //+The operation makes I4 and i5 unpack automatically and get 10, so i6==i2
		System.out.println(i1 == i6);  //true
		System.out.println(i2 == i6);  //true
		System.out.println(i3 == i6);  //false
        //i3 is a new output object, which is placed in heap memory, while i6 is a constant, which is placed in stack memory and points to different addresses
Two ways to create a String
public class StringNewTest {
	public static void main(String[] args) {
		String s0 = "abcdef";
		String s1 = "abc";
		String s2 = "abc";
		String s3 = new String("abc");
		String s4 = new String("abc");
		System.out.println(s1 == s2); //true is in the same constant pool and points to the same address
		System.out.println(s1 == s3); //false s1 stack memory s2 heap memory
		System.out.println(s3 == s4); //False S3 and S4 are in heap memory, but the pointing addresses are different
		String s5 = s1 + "def";    //The compiler does not optimize variables
		String s6 = "abc" + "def"; //Are constants, and the compiler will automatically optimize them to abcdef
		String s7 = "abc" + new String ("def");//When it comes to new objects, the compiler does not optimize them
		System.out.println(s5 == s6); //false
		System.out.println(s5 == s7); //false
		System.out.println(s6 == s7); //false
		System.out.println(s0 == s6); //true 

		String s8 = s3 + "def";//The compiler does not optimize new objects
		String s9 = s4 + "def";//The compiler does not optimize new objects
		String s10 = s3 + new String("def");//The compiler does not optimize new objects
		System.out.println(s8 == s9); //false
		System.out.println(s8 == s10); //false
		System.out.println(s9 == s10); //false
        //S8, S9 and S10 are all in heap memory, with the same values but stored separately, and the pointing addresses are also different

Section 5 immutable objects and strings

Immutable object

  • Immutable object
    Once created, this object (state / value) cannot be changed
    The intrinsic value of its member cannot be modified
    Packing class of basic type
    String,BigInteger, BigDecimal, etc
  • Mutable object
    - common objects
  • Immutable objects are also passed pointers
  • Because it is immutable, the temporary variable points to the new memory, and the pointer of the external argument remains unchanged

Create immutable objects

  • Immutable object cannot be changed. If it needs to be changed, use clone/new to modify an object
  • All attributes are final and private
  • setter method is not provided
  • The class is final, or all methods are final
  • If the class contains mutable objects, the depth of clone (clonable interface) is required to return copies

Advantages of immutable objects

  1. Read only, thread safe
  2. Concurrent read to improve performance
  3. Reuse

Immutable object disadvantage

  1. Waste space
    When an immutable object is modified, a new space will be opened, and the old object will be shelved until garbage collection

Java string

  • String is the most used class in Java and is a typical immutable object
  • String defined method
    String a = "abc";  //Constant assignment, stack allocation memory
    String b = new String("abc"); //new object, heap allocated memory
  • String content comparison: equals method
  • Whether to point to the same object: pointer comparison==

Addition of strings

String a="abc";
//Since String cannot be modified, it will apply for a new space. The old space still exists, and the pointer points to the new space, which is inefficient
  • In Java, the append method of StringBuffer/StringBuilder class is used for modification
  • The objects of StringBuffer / StringBuffer are variable objects
StringBuffer class
  • synchronization
  • Thread safety
  • Quick modification
  • Out of sync
  • Thread unsafe
  • Faster modification

Topics: Java intellij-idea