Talking about the data characteristics of Java

1. What kinds of data types are there? What is the difference between them?
2. What happened in the process of variable assignment?
3. When is the object created?

type of data

Data types are divided into two types: basic types and reference types , among which String, Integer, etc. are commonly used reference types.
Insert picture description here

Talking about stack and heap

  • Because stacks and heaps are not the focus of this blog, I will only briefly mention the knowledge points that need to be used today. When we declare two variables a and b in Java as shown below, two references will be generated in the stack. Since a is a variable of the basic type int, no object is created when a is declared , a only points to the value 8 in the stack; b is a variable of the wrapper class String (reference type), so when b is declared, b points to the stack An address through which the computer can find the "ok" string object we created.
  • When we assign values ​​to two variables, one object only assigns the value in the stack to another object. So when we assign values ​​between two reference type variables, the system does not automatically create an object in the heap, but makes their addresses point to the same object.
    Insert picture description here

Compare these methods of modifying parameters

  • First we need to create a Student class
public class Student {
    
    
	public String name;
	public int age;
	
	public Student(){
    
    }
	
	public Student(String name,int age){
    
    
		this.name=name;
		this.age=age;
	}
}
  • Then create a ChangeDemo class, let’s try three methods to modify the age in Student cxd to see what the result will be
public class ChangeDemo {
    
    
	public void change_1(int age){
    
    
		age=age+1;
	}
	public void change_2(Student stu){
    
    
		stu.age=stu.age+1;
	}
	public void change_3(Student stu){
    
    
		stu=new Student(stu.name,stu.age+1);
	}
	public void test(){
    
    
		//创建学生的初始对象
		Student cxd=new Student("cxd",20);
		
		//下面这三行代码分别单独测试,测试某一条时记得把其他两条注释掉!
		change_1(cxd.age);
		change_2(cxd);
		change_3(cxd);
		
		System.out.println(cxd.name+"的年龄是"+cxd.age);
	}
	
	public static void main(String[]args){
    
    
		ChangeDemo cd=new ChangeDemo();
		cd.test();
	}
}
  • Observe the differences between the three methods, compare their running results, and carefully consider what happened when they were running.
    Insert picture description here
    Sentence interpretation:
  • The parameter we pass in the method change_1 is cxd.age, and the system has automatically executed a int age=cxd.age;statement when calling this method . This time is equivalent to that we have declared a variable age of type int on the stack, and the value of this age is equal to cxd.age. At this time, age and cxd.age are independent of each other, and modifying age will not affect cxd.age.
    -
  • The parameter we pass in change_2 is cxd, which is a variable of the Student class and belongs to the type of reference variable. When calling this method, the system has also automatically executed a Student stu=cxd;statement, which also declares a variable stu with the same address as cxd on the stack. Note that because the addresses of cxd and stu both point to the same object at this time, when we modify stu at this time, we will modify the value of the object pointed to by the stu address. Finally, we output cxd.age, which is of course the value after the output has been modified.
    Insert picture description here
  • The parameters passed in method change_3 and method change_2 are the same, so the first step is the same. The statement stu=new Student(stu.name,stu.age+1);creates a new Student object, and stu is updated to point to the address of this new object. At this time, the new Student object is changed, and the original Student object remains unaffected.
    Insert picture description here
  • Small summary: When we use ordinary methods to pass parameters, what we pass is only the contents of the stack.

When was the String object created?

  • Let's try to run the following piece of code under the main function of any class, and observe the results, which are quite different. Similarly, think about what happened in these processes?
public static void main(String[]args){
    
    
		String a="ab";
		String b="ab";
		String c="abc";
		String d="ab"+"c";
		String e=new String("abc");
		String f=a+"c";
		String g=a+"c";
		
		System.out.println(a==b);
		System.out.println(c==d);
		System.out.println(c==e);
		System.out.println(c==f);
		System.out.println(f==g);
	}
  • The output is shown below:
    Insert picture description here

  • Here is an explanation of a small concept: we use the symbol " == " and the method " .equals() " to compare whether two String classes are equal , the former judges whether the addresses of the two variables in the stack are not The same (that is, comparing whether the two of them point to the same object), the latter judges whether the values ​​represented by the two strings are the same. (That is to say, two strings may have the same content, but they are two different objects)

  • In this series of codes, our String c, d, e, f, g are all "abc", but when we use "==" to judge whether they are equal, a different answer appears, below The blogger will explain one by one.

  • a==b First of all, we need to understand String a="ab";what is executed in this statement? This statement means that when declaring variable a, first look for a ready-made "ab" object in the heap , and create an "ab" object if it does not exist; if there is one, let a point to This object already exists. So, when we create a String a="ab" and then create String b="ab", the variable b will point to the "ab" created by a. At this point, we judge "a==b", that is, whether the addresses of a and b are the same, the answer is of course true.

  • c==d In the previous example, we mentioned that String a==“xxx” is a special way of creating objects. Here c and d use this method of creating objects, so they point to the same An object.

  • c==e When we use the new method to create an object, a new object will be created in the heap. So the variable pointing to the object created by the new method will of course not be equal to any variable.

  • The output of c= =f;f==g is also false, indicating that f and g have also created new objects.

Guess you like

Origin blog.csdn.net/weixin_44710412/article/details/113578303