Basic knowledge points of classes and objects-java

The basic principles of java naming include : lowercase the first word of variable names and method names, and capitalize only the first letter of the remaining words; capitalize the first letter of the first word of the interface name and class name; capitalize the constant name completely. The name of the package should consist of lowercase letters.

Identifier

Role: Identifiers are symbols used to name variables, classes, methods, etc. in java programs.

1. Rules for identifiers:

Identifiers also have certain naming rules, not all names can be used.
Rule 1: Identifiers can be composed of letters, numbers, underscores, and dollar signs, but cannot contain other special characters such as %, @, and spaces. Cannot start with a number.
Rule 2: Identifiers cannot be keywords and reserved words of java. But it can contain keywords and reserved words.
Rule 3: Identifiers are strictly case sensitive.
Rule 4: The name of an identifier should best reflect its role.

2. Variable name rules:

Insert picture description here
Note: 1, can not start with a number
2, can not start with other special characters other than underscore and dollar sign
3, can not use java keywords

Memory management in Java : In order to make full use of resources, Java has a system-level thread to track the use of memory. It can reclaim unused memory space when the system is idle, so that programmers can manage from busy memory Liberated in.

In a Java source program, there can be zero or more import statements.

To declare a class, the only permission modifiers that can be used are public and default .

In Java, the root class of all classes is java.lang.Object .

A Java source program file can contain multiple classes, but only one class can use the public modifier , and the name of the class must be the same as the name of the source program file. In addition, when multiple classes are created in the program, the class containing the main() method must be used.

Public, protected, private and default are all access control in java:

public protected default private
This category
The package this class is in ×
Subclasses in other packages × ×
Non-subclasses in other packages × × ×

The role of the new keyword : allocate memory space for the object, call the constructor of the class, and return a reference for the object.
Example:
In java, the operator for memory allocation is new .

The role of the static keyword

The static keyword can be used to modify a code block to indicate a static code block, a modified member variable to indicate a global static member variable, and a modified method to indicate a static method . (Note: You cannot modify ordinary classes, except for internal classes. Why is this?)
In short, the content modified by the static keyword is static.
Static is relative to dynamic. Dynamic refers to that when a Java program is running on the JVM, the JVM will dynamically create objects and store objects (allocate memory) according to the needs of the program. After the object's mission is over, the object will be destroyed by the garbage collector, that is, memory. The recycling is uniformly managed by the JVM and assigned to other newly created objects; static means that when the Java program is not running, the JVM will allocate space for the loaded class to store the content of the static keyword; such as static member variables, the Java class is loaded into the JVM In, the JVM will store the class and the static member variables of the class in the method area. We know that the method area is shared by threads and rarely occurs GC (Garbage Collection, garbage collection, garbage collection) area, so the content modified by the static keyword All are shared globally, and storage space will only be allocated once. So when some content of the class does not belong to the object, but is shared by the object that belongs to the class, you can consider whether to modify it with the static keyword .

1. Decorate the code block

In a class, a code block decorated with static is called a static code block, and vice versa is called an instance code block.
The instance code block will be executed with the creation of the object, that is, each object will have its own instance code block, which shows that the running result of the instance code block will affect the current content, and disappear with the destruction of the object (memory recycling ); And the static code block is the code block that is executed when the Java class is loaded into the JVM memory . Since the loading of the class will only happen once during the JVM running, the static code block will only be executed once .
Because the main function of the static code block is to perform some complex initialization work , the static code block is stored in the method area with the class in the form of the static code block execution result is stored in the method area, that is, the initialization amount is stored in the method area and Shared by threads.

2. Modify member variables

The member variables modified by the static keyword in the class are called static member variables, because static cannot modify local variables (why?), so static member variables can also be called static variables. Static variables are similar to code blocks. When the class is loaded into the JVM memory, the JVM will put the static variables into the method area and allocate the memory, which is also shared by the threads. The access form is: class name. static member name.

public class StaticTest {
    
    
	public static void main(String[] args) {
    
    
		System.out.println(D.i);
		System.out.println(new D().i);
	}
}
class D {
    
    
	static {
    
    
		i = 2;
		System.out.println("D : 静态代码块1");
	}
	static int i;
}

Static variables are stored in the information of the class and can be shared between threads. Of course, it also belongs to each object of the class, so static variables can be accessed through the object, but the compiler does not support this and will give a warning .

. The static variables of a class and the loading sequence of the static code blocks of the class. The class will load the static variables first, and then load the static code block, but when there are multiple static variables and multiple code blocks, it will be loaded in the order of writing.

class D {
    
    
	static {
    
    
		i = 2;
		System.out.println("D : 静态代码块1");
	}
	static {
    
    
		i = 6;
		System.out.println("D : 静态代码块2");
	}
	static int i;
}

You can think about the results of the operation.
. Static variables do not need to be explicitly initialized, and the JVM will default to its corresponding default values. For example, byte of the basic data type is 0, short is 0, char is \u0000, int is 0, long is 0L, float is 0.0f, double is 0.0d, boolean is false, and the reference type is uniformly null.
. Since the static variable is shared in the JVM memory and can be changed, then access to it will cause thread safety issues (thread A rewrites at the same time, thread B obtains its value, then the obtained value is the value before modification or the value after modification What?), so when using static variables, consider multi-threading. If you can ensure that static variables are immutable, you can use the final keyword together to avoid thread safety issues; otherwise, you need to use synchronization to avoid thread safety issues, such as using it with the volatile keyword.
. The static key cannot modify local variables, including instance methods and static methods, otherwise it will go against the original intention of the static keyword-sharing.

3. Modification method

A method modified with the static keyword is called a static method, otherwise it is called an instance method. Call by class name. Method name, but you need to note that static methods can directly call static variables and other static methods of the class, and cannot directly call member variables and instance methods (except through object calls).

class D {
    
    
	static {
    
    
		i = 2;
		System.out.println("D : 静态代码块");
	}
	static final int i;
	int j;
	
	static void method() {
    
    
		System.out.println(i);
		System.out.println(new D().j);
		
		method1();
		new D().method2();
	}
	
	static void method1() {
    
    
		System.out.println(i);
	}
	void method2() {
    
    
		System.out.println(i);
	}
}

Note: Since the instance method of the class needs to be called by the object to access, and the static method can be accessed directly through the class name , how does a class start to execute without considering the deployment of the server? The biggest possibility is to start Java through "class name. static method", and I define so many static methods, how does the JVM know the main entrance?
Perhaps, you thought of the main method.
Note: Although the static keyword cannot modify the ordinary class, you can use the static keyword to modify the inner class to make it a static inner class . The meaning of the static keyword itself is sharing, and the method area of ​​the Java class loaded into the JVM memory is also shared by threads, so there is no need to modify the ordinary class with the static keyword.

4. Static import

When importing a package or class with import, you can use static to modify the package name or class to indicate static import. Static import can be compared with dynamic import to deepen understanding.
Dynamic import is when you need to new an object of a class that is not in this package when your program is running, the class will be loaded according to the full path class name; while static import is to load the statically imported class as the class is loaded, so it It is imported in advance (you can use it directly without using class names, methods or attributes).

super keyword

1.在子类中,通过super关键字可以在子类中使用父类的功能
2.super关键字相当于是父类的一个引用该对象
3.如果在构造方法中使用,必须出现在调用位置的第一行

this keyword

Each object has a reference named this, which points to the current object itself, mainly in the following four aspects:
(1) This calls the attributes in this class, which is the member variable in the class. When there is no parameter with the same name as the member variable in the member method, this can be omitted. (The default in the member method is to reference the parameters in the method, not the member variables)
(2) this calls other methods in this class. Among them, this before the member method name can be omitted.
(3) This calls other construction methods in this class. In the construction method, the construction method with different parameter tables in this class can be called through this.
(4) Return the object value. For example, in the code, you can use return this to return a reference to a certain class. At this time, the this keyword represents the name of the class.

Guess you like

Origin blog.csdn.net/zhanlong11/article/details/114272782