Java studies a simple algorithm and encounters this output. Well, I didn't really pay attention to it before ......

Source: Internet
Author: User

Java studies a simple algorithm and encounters this output. Well, I didn't really pay attention to it before ......

Java studies a simple algorithm and encounters this output. Well, I didn't really pay attention to it before ......

What is this running principle ?????

 

package org.rui.util;import java.util.HashMap;import java.util.Map;public class A{public static void main(String[] args){Map
 
   m = new HashMap
  
   ();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;}@Overridepublic 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
 
   m3=null;    System.out.println("---------------------------------");m3=new HashMap
  
   ();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
   
    ();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} */
   
  
 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.