Integer in = new Integer(123); 
Integer b = in; 
b =10; 
System.out.println(in); 输出 的是 123 ; 我要的效果是 输出  10  如何才能实现  我想将 in的引用 传给 b

解决方案 »

  1.   

    java里 对象 是 引用传递的
      

  2.   

    Integer in = new Integer(123); 
    Integer b = in; 
    b =10; 
    in = b;
    System.out.println(in);
      

  3.   

    我想将 in的引用 传给 b *************
    应该这样说 你想将in的引用指向b.
    Integer in = new Integer(123); 
    Integer b = 10; 
    in = b;//这样才能将in这个引用也指向b 也就是=10
    System.out.println(in); 
      

  4.   

    引用没有传递,引用只能改变自身的值来改变指向--值就是它指向对象的地址.
    java里面参数传递只有值传递.
    Integer in = new Integer(123); //创建一个引用 in 指向 一个新的Integer对象123;
    Integer b = in; //创建一个引用b,指向in指向的对象
    b =10; //引用b指向对象10
      

  5.   

    StringBuffer str =new StringBuffer("123");
    StringBuffer str1 =str;
    str1.append("456");
    System.out.println(str);类似这样的 代码

    Integer in = new Integer(123);
    Integer b =in;
    b=10;
    System.out.println(b);
      

  6.   

    关键在这里
    b =10
    这样相当于
    b=new Integer(10)
    b又重新指向了一个对象,它跟in没有关系了.
    你说的b改变in也改变,这在普通类中可以实现,不过在Integer中实现不了,integer没有改变值的方法,要
    想改变,就必须new
    所以这里用Integer不合适。
      

  7.   

    不用 Integer  用 什么  告诉我下啊  着急
      

  8.   

    StringBuffer和Integer处理机制不一样
      

  9.   


    package test;public class A {    /**
         * @param args
         */
        public static void main(String[] args) {
            MyInteger in = new MyInteger(123);
            MyInteger b = in;
            b.setNum(10);
            System.out.println(in.getNum());     }}
    class MyInteger{
        public MyInteger(int num){
            this.setNum(num);
        }
         private int num;
        public int getNum() {
            return num;
        }    public void setNum(int num) {
            this.num = num;
        }
    }
      

  10.   


    两个代码不一样是因为,StringBuffer是可变的,而Integer因为和int会自动转换,所以Integer是不可变的--在常量池中只能有一份.
    so    
    str1.append("456");改变了对象.
    而b=10; 不能改变原来的对象,而是指向了一个新的,其实也不叫新的,也在常量池中.String也是一样的.
      

  11.   

     int []a={1,2};
       int []b=a;
        b[0]=3;
    这样就可以实现了  楼主
      

  12.   


    你这里为什么是输出in呢?那肯定是123啊,你输出b看看是不是10.
      

  13.   

    Integer in = new Integer(123); 
    Integer b = in; 
    b =10; 
    System.out.println(in); 输出 的是 123 ; 
    你的想法有意思,不过Integer做不了,因为这个需要深度克隆的支持。
    参考这个public class A implements Cloneable {
    public int index;
    public A() {
    ;
    }
    public Object clone() {  
     A o = null;   
            try {   
                o = (A) super.clone();   
            } catch (CloneNotSupportedException e) {   
                e.printStackTrace();   
            }   
         return o;   
    }
    }测试:
    public class ClonseTest {
    public static void main(String[] args) {
    A a = new A();
    A b = new A();
    a.index=123;   
    a = b; 

    b.index = 10;
    System.out.println(a.index);
    }
    }
      

  14.   

    public class ClonseTest { 
    public static void main(String[] args) { 
    A a = new A();
    a.index=123;   
    A b = (A)a.clone(); 

    b.index = 10;
    System.out.println(a.index);
    System.out.println(b.index);