首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么包装类对象的标识符不用作引用变量

为什么包装类对象的标识符不用作引用变量
EN

Stack Overflow用户
提问于 2013-08-23 12:49:49
回答 4查看 1.8K关注 0票数 6

我的问题涉及包装类。我知道,当我们使用包装器类存储原始类型文字时,我们将其存储为包装类的对象,因此对象的标识符将是一个引用变量(在某种程度上类似于c++中的指针)。例如,在Integer wi = new Integer("56")中,wi是一个引用变量。但如果是这样的话:

  1. 为什么我可以做wi++wi +=2?为什么编译器要处理那些引用变量,比如普通的原语变量?引用变量不是存储对象的引用吗?
  2. 给定Integer wi = new Integer("56")int pi = 56,为什么(wi == pi)返回true。难道不是应该存储一个参考资料(地址)吗?

另一个问题是:当引用变量作为参数传递给方法时,它被认为是通过引用传递的,因此,发生在该引用变量上的修饰符应该会影响它的值,但它不会:

代码语言:javascript
复制
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?
  }
}
EN

回答 4

Stack Overflow用户

发布于 2013-08-23 12:54:12

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

  1. 我为什么能这么做?wi++或wi +=2 .i的意思是编译器为什么要处理那些引用变量,比如普通的primitve变量?(引用变量不是存储对象的引用吗?)

由于autoboxingauto-unboxing的概念,wi被转换为primitive,增量,然后再转换回Wrapper

2.或者如果我们have==>“Integer =(”56“)和"int =56”。为什么(wi == pi)返回true。不应该存储参考资料(地址)吗?

这是因为对于Integer包装器类,==将返回值true直到128。这是故意的

如果您对密码原语和对象引用有疑问,请研究以下程序

代码语言:javascript
复制
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 
    }       

}

输出是

代码语言:javascript
复制
Before Passing value to modify() a = 5
Modified number  b = 6
After passing value to modify() a = 5

将对象引用传递给方法

代码语言:javascript
复制
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;
    }   


}

输出是

代码语言:javascript
复制
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;
    }   


}

输出是

代码语言:javascript
复制
Before passing the reference d.height = 10
After passing the reference d.height = 11
票数 4
EN

Stack Overflow用户

发布于 2013-08-23 13:35:56

java编译器自动插入intValueInteger.valueOf调用,以便在intInteger之间进行转换。例如,下面是问题中的代码片段:

代码语言:javascript
复制
void show(Integer x){
  x *=100 ;
}

下面是真正发生的事情:

代码语言:javascript
复制
void show(Integer x) {
  int unboxed = x.intValue();
  unboxed *= 100;
}

如您所见,行x *= 100并不真正更改传入的Integer对象,它只更改从该Integer对象提取的int值。

类似地,问题中的代码wi == pi实际上意味着wi.intValue() == pi,这解释了您的观察。

票数 2
EN

Stack Overflow用户

发布于 2013-08-23 13:01:15

Java使用了“按值调用”的概念,详细描述了这里,因此在您的示例中,x *=100;在方法中只更新局部变量

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18403342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档