Memory is divided into heap memory and stack memory, test test=new test () is displayed in memory, the first part in the stack memory is allocated an address new Test () in the heap memory allocation allocation equivalent address room number
Packagestudy1; Public classTest { Public Static voidMain (String args[]) {test test1=Newtest (); intDate=9; Birthday B1=NewBirthday (7,7,1970); Birthday B2=NewBirthday (7,7,1990); Test1.change1 (date); Test1.change2 (B1); Test1.change3 (B2); System.out.println ("Date:" +date); B1.display (); B2.display (); } Public voidChange1 (inti) {i= 1234;//the date value is not modified here, and the date value copy to I } //Once the method is executed, the allocated object memory disappears Public voidChange2 (Birthday b) {//b same address as B1b=NewBirthday (1,1,2010);//b pointing to the new object will not be the original B1 address, assigning a new address, nor modifying the B1//cannot modify the value in B1 } Public voidChange3 (Birthday b) {B.setday (22); }}classBirthday () {Private intDay ;Private intmonth;Private intYear ; Public voidSetday (intDay ) { This. Day =Day ;} Public voidSetmonth (intmonth) { This. Month =month;} Public voidSetyear (intYear ) { This. Year =Year ;} PublicBirthday (intDintMinty) { Day=d;month=m;year=y;} Public intGetDay () {returnDay ;} Public intGetMonth () {returnmonth;} Public intgetYear () {returnYear ;} Public voiddisplay () {System.out.println ( day+ "--" + month + "--" +year+ "--");}}
Memory Analysis in Java