[英]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: 但如果这是真的:
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? 参考变量不存储对象的引用吗?
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)
传递包含相同对象的引用变量的副本。
- 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
由于autoboxing
和auto-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编译器自动插入intValue
和Integer.valueOf
调用以在int
和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. 如您所见,行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
,这解释了你的观察。
编译器unboxes wi
到原始数据type.So现在,这是一个基本数据类型,因为一切都在Java是按值传递,在形式参数的改变不会影响实际arguements。
mn.show(wi);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.