简体   繁体   English

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

[英]why identifier of a wrapper class object does not work as a reference variable

My question involves wrapper classes. 我的问题涉及包装类。 I know that when we store a primitive type literal by using wrapper classes, we are storing it as a object of that wrapper class so object's identifier will be a reference variable (somehow like a pointer in c++). 我知道当我们使用包装类存储基本类型文字时,我们将它存储为该包装类的对象,因此对象的标识符将是一个引用变量(在某种程度上类似于c ++中的指针)。 For instance, in Integer wi = new Integer("56") , wi is a reference variable. 例如,在Integer wi = new Integer("56")wi是一个引用变量。 But if that is true: 但如果这是真的:

  1. Why can I do wi++ or wi +=2 ? 为什么我可以做wi++wi +=2 Why does compiler deal with those reference variables like normal primitive variables? 为什么编译器会处理那些像普通原始变量这样的引用变量? Doesn't a reference variable store reference of a object? 参考变量不存储对象的引用吗?

  2. Given Integer wi = new Integer("56") and int pi = 56 , why does (wi == pi) returns true. 给定Integer wi = new Integer("56")int pi = 56 ,为什么(wi == pi)返回true。 Isn't wi supposed to store a reference (address)? 是不是应该存储引用(地址)?

And another question: When a reference variable is passed to a method as parameter it counts as passing by reference so the modifiction that happens to that reference variable should affect it's value but it doesn't: 另一个问题:当一个引用变量作为参数传递给方法时,它计为通过引用传递,因此发生在该引用变量上的修改应该影响它的值,但它不会:

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?
  }
}

the statement mn.goo(pi) passes the copy of value 86 while mn.show(wi) passes the copy of reference variable which holds the same object. 语句mn.goo(pi)传递值86的副本,而mn.show(wi)传递包含相同对象的引用变量的副本。

  1. why can i do this? 我为什么这样做? wi++ or wi +=2 .i mean why does compiler deal with those reference vriables like normal primitve variables?(doesn't a reference variable store reference of a object?) wi ++或wi + = 2 .i意味着为什么编译器会处理像普通原始变量这样的引用变量?(不是引用变量存储对象的引用吗?)

Because of the concept of autoboxing and auto-unboxing , wi is converted to primitive , incremented, then then converted back to Wrapper 由于autoboxingauto-unboxing的概念, wi被转换为primitive ,递增,然后转换回Wrapper

2.or if we have==>" Integer wi = new Integer("56") " and "int pi = 56" . 2.或如果我们有==>“整数wi =新整数(”56“)”和“int pi = 56”。 why does (wi == pi) returns true. 为什么(wi == pi)返回true。 isn't wi supposed to store refernce (address) 是不应该存储引用(地址)

This is because for Integer wrapper classes, the == will return true for the value till 128 . 这是因为对于Integer包装类, ==将为值返回true,直到128 This is by design 这是设计的

For your doubts regarding passign primitives and object references, Please study these programs 如果您对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 
    }       

}

The output is 输出是

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

Passing object reference to method 将对象引用传递给方法

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;
    }   


}

The output is 输出是

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;
    }   


}

The output is 输出是

Before passing the reference d.height = 10
After passing the reference d.height = 11

The java compiler automatically inserts intValue and Integer.valueOf calls to convert between int and Integer . java编译器自动插入intValueInteger.valueOf调用以在intInteger之间进行转换。 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. 如您所见,行x *= 100并未真正更改传入的Integer对象,它只更改从该Integer对象中提取的int值。

In a similar way, the code wi == pi from the question actually means wi.intValue() == pi , which explains your observation. 以类似的方式,来自问题的代码wi == pi实际上意味着wi.intValue() == pi ,这解释了你的观察。

Java uses the "call by value" concept as described in detail here So in your case x *=100 ; Java使用如详细介绍了“呼叫按价值”的理念, 在这里你的情况下,x * = 100左右; in method show only updates the local variable 在方法show中只更新局部变量

编译器unboxes wi到原始数据type.So现在,这是一个基本数据类型,因为一切都在Java是按值传递,在形式参数的改变不会影响实际arguements。

mn.show(wi);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么创建包装器对象在Java中工作而不是传递本地引用? - Why does creating a wrapper object work in Java instead of passing a local reference? 为什么此引用不起作用? - why does this reference not work? 将派生对象引用分配给基类的变量将不起作用 - Assigning derived object reference to variable of base class won't work 比较器与包装器类的工作方式不同吗? - Does Comparator work differently with wrapper class? 为什么从String []转换为Object只能使用引用? - Why does casting from String[] to Object only work with a reference? Java:如果 Character 是包装器 class,为什么“Character newChar = 'c'”会起作用? 为什么它不需要构造函数? - Java: Why does “Character newChar = 'c' ” work if Character is a wrapper class? How come it doesn't need a constructor? 为什么 Arraylist 保存对象而不是引用变量,而不是 Array 保存引用变量或基元? - Why does Arraylist hold object rather than reference variable, as opposed to Array holding reference variable or primitives? 为什么Java中的包装类不像引用类型? - Why wrapper class in Java doesn't behave like a reference type? Java-对象引用或标识符? - Java - Object reference or identifier? Java - ActionListener class 变量一致性.. 为什么这有效? - Java - ActionListener class variable consistency.. Why does this work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM