Dabai became the seventeenth day of the Java software siege lion (object creation and use, memory analysis)

Creation and use of objects

public class OOTest01{
    
    
	publci static void main(String[] args){
    
    
		int i=10;
		Student s = new Student();

		int StuNo=s.no;
		System.out.println("学号 =" + StuNo);//0【默认值】
		System.out.println("学号 =" + s.no);//0【默认值】
		s.no=10;
		System.out.println("学号 =" + s.no);//10

		//再通过类实例化一个全新的对象
		//stu是一个引用
		//stu同时也是一个局部变量
		//Student是变量的数据类型
		Student stu = new Student();
		System.out.println("学号 =" + stu.no);// 0

		System.out.println("学号 =" + Student.no);//编译错误,不能直接采用“类名”的方式访问
		//因为no是实例变量,对象级别的变量,变量存储在java对象的内部,必须先有对象,通过对象才能访问no这个实例变量,不能直接通过“类名”访问
	}
}
//学生类
//学生类是一个模板
//描述了所有学生的共同特征【状态+行为】
//当前类只描述学生的状态信息【属性】
public class Student{
    
    
//类体=属性+方法

//属性【存储数据采用变量的形式】
//由于变量定义在类体当中,方法体之外,这种变量称为成员变量
//所有学生都有学号信息
//但是每一一个学生的学号都是不同的
//所以要访问这个学号必须先创建对象,通过对象去访问学号信息
//学号信息不能直接通过"类"去访问,所以这种成员变量又被叫做:实例变量
//对象又被称为实例,实例变量又被称为对象变量。【对象级别的变量】
//不创建对象,这no变量的内存空间是不存在的,只有创建了对象,这个no变量内存空间才会创建。
int no;
String name;//姓名
boolean sex;//性别
int age;//年龄
String address;//住址
  • N objects can be instantiated through a class,
  • Syntax for instantiating objects:new 类名()
  • new is an operator in java language
  • The role of the new operator is to create an object and open up a new memory space in the JVM heap memory
  • Method area memory: When the class is loaded, the class bytecode code fragment is loaded into this memory space.
  • Stack memory (local variables): When the method code fragment is executed, memory space is allocated for the method and pushed onto the stack memory.
  • Heap memory: new objects are stored in heap memory

forStudent s = new Student ();

  • student is a reference data type
  • s is a variable name
  • new Student() is a student object
  • s is a local variable [stored in stack memory]: reference
  • What is an object? The memory space opened up by the new operator in the heap memory is called an object.
  • What is a reference? The reference is a variable, but this variable holds the memory address of another java object.
  • In the java language, programmers cannot directly manipulate the heap memory. There is no pointer in java, unlike the C language
  • In the Java language, programmers can only access instance variables inside objects in heap memory through "references".

Syntax format for accessing instance variables:

Read data: reference. Variable name
Modify data: reference. Variable name = value
int StuNo=s.no;

Local variables are stored in stack memory,
and instance variables in member variables are stored inside java objects in heap memory

Instance variable is one copy of an object, 100 objects have 100 copies

public class Customer
{
    
    
	int id;
}

public class OOTest02
{
    
    
	public static void main(String[] args){
    
    
		Customer c= new Customer();
		System.out.println(c.id); //0

		c=null;
		//以下程序编译可以通过,因为符合语法
		//运行出现空指针异常
		//空引用访问“实例”相关的数据一定会出现空指针异常
		//java.lang.NullPointerException
		System.out.println(c.id);
	}
}

The data related to the "instance" means that the object must be involved in this visit. This kind of data is instance-related data.

Key: instance variable objects must be created, accessed by reference, you can not directly use the 类名.way of access

Memory analysis

1. JVM (Java Virtual Machine) mainly includes three memory spaces. They are: stack memory, heap memory, and method area memory.

2. There are 1 heap memory and 1 method area memory. One stack memory per thread.

3. When the method is called, the memory space required by the method is allocated in the stack memory, which is called stacking. After the execution of the method ends, the memory space to which the method belongs is released, which is called the pop stack.

4. The main storage in the stack is the local variables in the method body.

5. The code fragments of the method and the entire class are stored in the method area memory, and these code fragments will be loaded when the class is loaded.

6. The java object created by the new operator during the execution of the program is stored in the heap memory. There are instance variables inside the object, so the instance variables are stored in the heap memory.

7. Variable classification:

  • Local variable [declared in method body]
  • Member variable [method in vitro declaration]
    instance variable [the front modifier has no static]
    static variable [the front modifier has static]

8. The static variables are stored in the method area memory. [First memorize meeting]

9. Among the three pieces of memory, the stack memory is the most frequently changed. The method area memory is the first to have data. The garbage collector mainly targets the heap memory.

10. When will the garbage collector [automatic garbage collection mechanism, GC mechanism] consider recycling the memory of a certain java object?

  • When the java object in the heap memory becomes garbage data, it will be recycled by the garbage collector.

11, the Java objects when the heap of garbage will become of it?

  • When there are no more references to it. This object cannot be accessed because the object can only be accessed by reference.

Guess you like

Origin blog.csdn.net/qq2632246528/article/details/112942475