【问题标题】:Need your help understanding java programming concept [closed]需要您帮助理解 Java 编程概念 [关闭]
【发布时间】:2014-07-01 15:49:40
【问题描述】:
public class Concept {    
    int num;    
    public static void main(String args[])    
    {    
        Concept obj1=new Concept();    
        Concept obj2=obj1;    
        obj1.num=100;    
        obj2.num=200;    
        System.out.println(obj1.num);    
        System.out.println(obj2.num);    
        //obj1.num=null;    

        obj2.num=500;    

        System.out.println(obj1.num);    
        System.out.println(obj2.num);    
    }   

}     

这里,这是一个简单的java程序。

输出是

200
200
500   
500

这里的内存分配是如何工作的?以及如何将 null 设置为 Obj1.num=null?

【问题讨论】:

  • 你能更具体地说明你想要什么吗?

标签: java class object memory memory-management


【解决方案1】:

obj2obj1 具有相同的引用,因此修改其中任何一个的状态将反映在两个变量中,因为它们指向相同的引用。

这是一张图片来解释对象引用的行为(在这个例子中使用Person类来解释):

来源:https://stackoverflow.com/a/7034719/1065197

请注意,您不能取消 int 或 Java 中的任何原始值,因此 obj1.num = null 无效。但是,如果将num 声明为Integer,则可以将其值设置为null,因为Integer 是原语int包装类

【讨论】:

    【解决方案2】:

    你同时问了两个问题。


    Java 使用引用,因此通过设置obj2 = obj1,值不复制,两个引用引用同一个对象。通过obj1 设置num 的值与通过obj2 设置相同。

    在内存中是这样的:

            +-----------------+
    obj2 -> |  Concept        | <- obj1
            +-----+-----+-----+
            | num | int | 200 |
            +-----+-----+-----+
    

    如果您使用以下代码,则会构造两个对象:

    Concept obj1 = new Concept();
    obj1.num = 200;
    Concept obj2 = new Concept();
    obj2.num = 300;
    

    那么内存应该是这样的:

            +-----------------+         +-----------------+
    obj1 -> |  Concept        | obj2 -> |  Concept        |
            +-----+-----+-----+         +-----+-----+-----+
            | num | int | 200 |         | num | int | 300 |
            +-----+-----+-----+         +-----+-----+-----+
    

    因此,从obj1 修改num 将设置与obj2 不同的内存部分。


    您不能将null 分配给int:它是一个原始类型。如果您想要一个具有null 值的整数,请尝试Integer

    【讨论】:

      【解决方案3】:

      Java 不是 C++,

      Concept obj1=new Concept();    
      Concept obj2=obj1; // obj2 is a reference to obj1.
      

      我想你想要,

      Concept obj1=new Concept();    
      Concept obj2=new Concept();
      

      此外,您不能将原始类型(如 int)设置为 null。您可以将 Integer 包装器设置为 null,

      Concept {    
       // int num;
       Integer num = null;
      

      【讨论】:

        【解决方案4】:

        您不能,因为您将num 声明为int,这是一个原始类型

        你能做什么? 你可以使用它的wrapper classInteger:

        Integer num;
        ...
        num = null; // valid since num is now an object
        

        【讨论】:

          【解决方案5】:
          public class Concept {    
              int num;    
              public static void main(String args[])    
              {    
                  Concept obj1=new Concept();    // Only one Concept object created. The reference obj1 is pointing to it.
                  Concept obj2=obj1;    // now 2 references obj1 and obj2 are pointing to the same object.
                  obj1.num=100;    // since both references are pointing to the same object, the changes they make will be on the `same` object
                  obj2.num=200;    
                  System.out.println(obj1.num);    
                  System.out.println(obj2.num);    
                  //obj1.num=null;    
          
                  obj2.num=500;    
          
                  System.out.println(obj1.num);    
                  System.out.println(obj2.num);    
              }   
          
          }     
          

          【讨论】:

            【解决方案6】:
            Obj1.num=null;
            

            num 不能设置为 null,因为 num 是原始 int,而不是对象。在 Java 中,只有对象引用可以设置为 null。

            至于内存处理。我在下面注释了你的代码来解释发生了什么。

            public class Concept {    
                int num;    
                public static void main(String args[])    
                {    
                    Concept obj1=new Concept();      // A new object is created.  Ref stored in obj1
                    Concept obj2=obj1;               // The ref from obj1 is copied to obj2
                    obj1.num=100;                    // obj1 and obj2 now hold the same ref to the same object
                    obj2.num=200;                    // overwrites previous assignment; the java compiler is likely to strip out the previous line as it is redundant
                    System.out.println(obj1.num);    
                    System.out.println(obj2.num);    
                    //obj1.num=null;    
            
                    obj2.num=500;                    // as before
            
                    System.out.println(obj1.num);    
                    System.out.println(obj2.num);    
                }   
            
            }    
            

            值得注意的是,与其他一些语言不同,Java 总是在堆上创建其对象。一个受其管理的内存区域,它为我们收集垃圾。它不会在堆栈上创建对象,因此 Object 类型的字段和变量始终是对该对象的引用。

            【讨论】:

            • +1 表示 单行 cmets
            猜你喜欢
            • 2013-05-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多