Below we will use code to explain how the data is stored in the heap and stack.
1 Essentials objects are stored in the heap.
2 object references are stored in the stack.
Package Com.king.practise;
public class Student {
Private String name= "Zhang San";
private int age=10;
private int id=1000000;
Student () {
}
Student (String name,int age,int ID) {
This.name=name;
This.age=age;
This.id=id;
}
Public Student copystudent (Student s) {
return new Student (S.getname (), S.getage (), S.getid ());
}
Public String GetName () {
return name;
}
public int getage () {
return age;
}
public int getId () {
return ID;
}
public void SetName (String name) {
This.name=name;
}
public void Setage (int.) {
This.age=age;
}
public void setId (int id) {
This.id=id;
}
}
Package Com.king.practise;
public class People {
public static void Main (string[] args) {
String name= "Jone";
int age=22,newage=23;
int id=133384;
Student s1=new Student (name,age,id);
S1.setage (NewAge);
S1.setname ("Jone white");
Student s2=s1;
System.out.println ("Studnet1");
System.out.println (S1.getid () +s1.getname () +s1.getage ());
S1.setage (29);
System.out.println ("Studnet2");
System.out.println (S2.getid () +s2.getname () +s2.getage ());
System.out.println ("Studnet1");
System.out.println (S1.getid () +s1.getname () +s1.getage ());
}
}
The result is this:
Studnet1
133384Jone White23
Studnet2
133384Jone White29
Studnet1
133384Jone White29
How to understand the result, we obviously change is the object S1 attribute, why the S2 property also changed.
In fact, the key is the correct understanding of student s2=s1;
First S2,S1 The two variables are reference (pointers) to the heap area object, which is the address of the object.
Studnet s2=s1 means to create a reference (address) with S1 to the same object, in other words s2 and s1
The address values are the same, they all point to the same object in the heap, and not a new object is created in the heap.
Keep in mind that you can create a new object in the heap only with the new keyword, so how can you make two
public class People {
public static void Main (string[] args) {
String name= "Jone";
int age=22,newage=23;
int id=133384;
Student s1=new Student (name,age,id);
S1.setage (NewAge);
S1.setname ("Jone white");
Student s2=new Student ();
System.out.println ("Student2");
System.out.println (S2.getid () +s2.getname () +s2.getage ());
S2=S1;
System.out.println ("Student1");
System.out.println (S1.getid () +s1.getname () +s1.getage ());
System.out.println ("Student2");
System.out.println (S2.getid () +s2.getname () +s2.getage ());
S1.setname ("new name");
System.out.println ("Student1");
System.out.println (S1.getid () +s1.getname () +s1.getage ());
System.out.println ("Student2");
System.out.println (S2.getid () +s2.getname () +s2.getage ());
}
}
Student2
1000000zhang San10
Student1
133384Jone White23
Student2
133384Jone White23
Student1
133384new Name23
Student2
133384new Name23
This uses two new but s2=s1, and the effect is the same as the above, all pointing to the same object, but
S2 previously pointed to an object without a pointer pointing to it.
So how do you create two real clones of an object? I mean the real object that creates two properties in the heap.
You can call the call Copystudent () method.
Package Com.king.practise;
public class People {
public static void Main (string[] args) {
String name= "Jone";
int age=22,newage=23;
int id=133384;
Student s1=new Student (name,age,id);
S1.setage (NewAge);
S1.setname ("Jone white");
Student s2=s1.copystudent (S1);
System.out.println ("Student2");
System.out.println (S2.getid () +s2.getname () +s2.getage ());
S2.copystudent (S1);
System.out.println ();
System.out.println ("Student1");
System.out.println (S1.getid () +s1.getname () +s1.getage ());
System.out.println ("Student2");
System.out.println (S2.getid () +s2.getname () +s2.getage ());
S1.setname ("new name");
System.out.println ("Student1");
System.out.println (S1.getid () +s1.getname () +s1.getage ());
System.out.println ("Student2");
System.out.println (S2.getid () +s2.getname () +s2.getage ());
}
}
Results:
Student2
133384Jone White23
Student1
133384Jone White23
Student2
133384Jone White23
Student1
133384new Name23
Student2
133384Jone White23
In this way, it really creates different objects with the same properties.
I am a rookie, if there are mistakes I hope you correct me.
This article from the "Best Scenery" blog, reproduced please contact the author!
Java Memory allocation Summary