6

我的问题涉及包装类。我知道,当我们使用包装类存储原始类型文字时,我们将其存储为该包装类的对象,因此对象的标识符将是一个引用变量(有点像 c++ 中的指针)。例如, inInteger wi = new Integer("56")wi一个参考变量。但如果这是真的:

  1. 为什么我可以做wi++wi +=2?为什么编译器会像处理普通原始变量一样处理那些引用变量?引用变量不存储对象的引用吗?

  2. 给定Integer wi = new Integer("56")and int pi = 56,为什么(wi == pi) 返回 true。wi 不应该存储引用(地址)吗?

还有一个问题:当引用变量作为参数传递给方法时,它被视为通过引用传递,因此对该引用变量的修改应该会影响它的值,但它不会:

public class Main {
  void show(Integer x){
    x *=100 ;
  }

  void goo(int x){
    x *=100 ;
  }

  public static void main(String[] args) {
    Main mn = new Main() ;
    Integer wi = new Integer("86");
    int pi = 86 ;

    mn.goo(pi);
    System.out.println(pi); //output = 86

    mn.show(wi);
    System.out.println(wi); //output = 86, shouldn't it be 8600?
  }
}
4

4 回答 4

4

该语句mn.goo(pi)传递值的副本,86同时mn.show(wi)传递包含相同对象的引用变量的副本。

  1. 为什么我可以这样做?wi++ 或 wi +=2 。我的意思是为什么编译器像普通的原始变量一样处理那些引用变量?(引用变量不存储对象的引用吗?)

由于 and 的概念autoboxingauto-unboxing转换wiprimitive,递增,然后转换回Wrapper

2.或者如果我们有==>" Integer wi = new Integer("56") " 和 "int pi = 56" 。为什么 (wi == pi) 返回 true。不应该存储引用(地址)

这是因为对于Integer包装类,==直到的值将返回 true 128。这是设计使然

对于您对 passign 原语和对象引用的疑问,请研究这些程序

class PassPrimitiveToMethod
{
    public static void main(String [] args)
    {
        int a = 5;
        System.out.println("Before Passing value to modify() a = " + a);
        PassPrimitiveToMethod p = new PassPrimitiveToMethod();
        p.modify(a);
        System.out.println("After passing value to modify() a = " + a);
        // the output is still the same because the copy of the value is passed to the method and not the copy of the bits like in refrence variables
        // hence unlike the reference variables the value remains unchanged after coming back to the main method

    }   


    void modify(int b)
    {
        b = b + 1;
        System.out.println("Modified number  b = " + b);
        // here the value passed is the copy of variable a
        // and only the copy is modified here not the variable 
    }       

}

输出是

Before Passing value to modify() a = 5
Modified number  b = 6
After passing value to modify() a = 5

将对象引用传递给方法

class PassReferenceToMethod
{
    public static void main(String [] args)
    {
        Dimension d = new Dimension(5,10);
        PassReferenceToMethod p = new PassReferenceToMethod();
        System.out.println("Before passing the reference d.height = " + d.height);
        p.modify(d);            // pass the d reference variable
        System.out.println("After passing the reference d.height = " + d.height);
        // the value changes because we are passing the refrence only which points to the single and same object
        // hence the values of the object are modified 
    } 

    void modify(Dimension dim)
    {
        dim.height = dim.height + 1;
    }   


}

输出是

class PassReferenceToMethod
{
    public static void main(String [] args)
    {
        Dimension d = new Dimension(5,10);
        PassReferenceToMethod p = new PassReferenceToMethod();
        System.out.println("Before passing the reference d.height = " + d.height);
        p.modify(d);            // pass the d reference variable
        System.out.println("After passing the reference d.height = " + d.height);
        // the value changes because we are passing the refrence only which points to the single and same object
        // hence the values of the object are modified 
    } 

    void modify(Dimension dim)
    {
        dim.height = dim.height + 1;
    }   


}

输出是

Before passing the reference d.height = 10
After passing the reference d.height = 11
于 2013-08-23T12:54:12.253 回答
2

The java compiler automatically inserts intValue and Integer.valueOf calls to convert between int and Integer. For example, here's a code snippet from the question:

void show(Integer x){
  x *=100 ;
}

And here is what really happens:

void show(Integer x) {
  int unboxed = x.intValue();
  unboxed *= 100;
}

As you can see, the line x *= 100 does not really change the Integer object you pass in, it only changes the int value extracted from that Integer object.

In a similar way, the code wi == pi from the question actually means wi.intValue() == pi, which explains your observation.

于 2013-08-23T13:35:56.230 回答
0

Java使用此处详细描述的“按值调用”概念 因此在您的情况下 x *=100 ; 在方法中显示只更新局部变量

于 2013-08-23T13:01:15.200 回答
0

编译器wi将原始数据类型拆箱。所以现在,它是一种原始数据类型,由于 Java 中的所有内容都是按值传递的,因此形式参数的更改不会影响实际参数。

mn.show(wi);
于 2013-08-23T13:01:46.047 回答