Home > Back-end >  Ask bosses on JAVA stack, heap, refer to a few questions
Ask bosses on JAVA stack, heap, refer to a few questions

Time:10-01



Arguably the first figure quoted references is the inside of the heap memory address, int and I can understand the operation of the ArrayList, int is the basic data types, saved on the stack is ArrayList object on the heap, but I can't understand the operation of the Integer, Integer area more than 10000 method can save the constant pool, by rights should be based on the object on the heap, but two references for Integer operations, is this why

CodePudding user response:

Didn't see you want to say, is to want to ask, why a + b=1 will affect?
If it is the problem, you made a mistake, any type, through=assignment, the assignment after may has nothing to do with the original object, that is to say, the left of the=variable to reference another object (the object might and the original is not the same object)
Integeg b=a;//b points to a reference object
A +=1;//a new object to the Integer (100001), has not had a reference to the object, but b or reference the original a reference object, so b did not change

But, not through=to modify the object, but modify attributes, it won't be the same, to change the attributes will not lead to variable pointing to the new object, so it changes will also change the original reference object, in the example is your f.a dd (1);//here you didn't use to change the f=, but by calling the add method to change the attributes of the object, f because of f and g refer to the same object, so the g also see change, if you are in the f.a dd (1); After, add f=new ArrayList<> (a);//=to change for other objects of f, you print g again see if it went with the f into the empty List? Here tell you g don't empty, that is to say, will not happen because g f=xx, likewise, Integet b will not change because of a=xx,

So attention should be paid to=modify variables, and change the properties through the method of difference,



CodePudding user response:

reference 1st floor qybao response:
didn't see you want to say, is to ask, why a + b=1 will affect?
If it is the problem, you made a mistake, any type, through=assignment, the assignment after may has nothing to do with the original object, that is to say, the left of the=variable to reference another object (the object might and the original is not the same object)
Integeg b=a;//b points to a reference object
A +=1;//a new object to the Integer (100001), has not had a reference to the object, but b or reference the original a reference object, so b did not change

But, not through=to modify the object, but modify attributes, it won't be the same, to change the attributes will not lead to variable pointing to the new object, so it changes will also change the original reference object, in the example is your f.a dd (1);//here you didn't use to change the f=, but by calling the add method to change the attributes of the object, f because of f and g refer to the same object, so the g also see change, if you are in the f.a dd (1); After, add f=new ArrayList<> (a);//=to change for other objects of f, you print g again see if it went with the f into the empty List? Here tell you g don't empty, that is to say, will not happen because g f=xx, likewise, Integet b will not change because of a=xx,

So attention should be paid to=modify variables, and change the properties through the method of difference,
yes you're right