java研究一个简单的算法碰到这种输出,哎,以前还真没注意还能这样......
求大神指定这种运行原理叫什么?????
package org.rui.util;
import java.util.HashMap;
import java.util.Map;
public class A
{
public static void main(String[] args)
{
Map<String, String> m = new HashMap<String, String>();
Map m2 = m;
m2.put("a", "ava");
System.out.println(m);
//
B b = new B();
B b2 = b;
b2.setId(100);
b2.setName("abc");
b.setId(50);
B b3=b2;
b3.setName("B3");
System.out.println(b);
System.out.println(b2);
System.out.println(b3);
}
}
/***
* output:
{a=ava}
B [id=50, name=B3]
B [id=50, name=B3]
B [id=50, name=B3]
*/
package org.rui.util;
import java.util.HashMap;
import java.util.Map;
public class B
{
private Integer id;
private String name;
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public String toString()
{
return "B [id=" + id + ", name=" + name + "]";
}
public static void main(String[] args)
{
Map m=new HashMap();
Map m2=m;
m2.put("m2","m2v");
Map<String,String> m3=null;
System.out.println("---------------------------------");
m3=new HashMap<String,String>();
m3.put("m3_1", "m3v_1");
m2.put("m2add3_1", m3);
m2=m3;
System.out.println("m:"+m);
System.out.println("m2:"+m2);
System.out.println("m3:"+m3);
System.out.println("---------------------------------");
m3=new HashMap<String,String>();
m3.put("m3_2", "m3v_2");
m2.put("m2add3_2", m3);
System.out.println("m2xx:"+m2);
m2=m3;
System.out.println("m:"+m);
System.out.println("m2:"+m2);
System.out.println("m3:"+m3);
}
}
/***output:
---------------------------------
m:{m2add3_1={m3_1=m3v_1}, m2=m2v}
m2:{m3_1=m3v_1}
m3:{m3_1=m3v_1}
---------------------------------
m2xx:{m2add3_2={m3_2=m3v_2}, m3_1=m3v_1}
m:{m2add3_1={m2add3_2={m3_2=m3v_2}, m3_1=m3v_1}, m2=m2v}
m2:{m3_2=m3v_2}
m3:{m3_2=m3v_2}
*/