我的问题涉及包装类。我知道,当我们使用包装器类存储原始类型文字时,我们将其存储为包装类的对象,因此对象的标识符将是一个引用变量(在某种程度上类似于c++中的指针)。例如,在Integer wi = new Integer("56")中,wi是一个引用变量。但如果是这样的话:
wi++或wi +=2?为什么编译器要处理那些引用变量,比如普通的原语变量?引用变量不是存储对象的引用吗?Integer wi = new Integer("56")和int pi = 56,为什么(wi == pi)返回true。难道不是应该存储一个参考资料(地址)吗?另一个问题是:当引用变量作为参数传递给方法时,它被认为是通过引用传递的,因此,发生在该引用变量上的修饰符应该会影响它的值,但它不会:
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?
}
}发布于 2013-08-23 12:54:12
语句mn.goo(pi)传递值86的副本,而mn.show(wi)传递包含相同对象的引用变量的副本。
由于autoboxing和auto-unboxing的概念,wi被转换为primitive,增量,然后再转换回Wrapper。
2.或者如果我们have==>“Integer =(”56“)和"int =56”。为什么(wi == pi)返回true。不应该存储参考资料(地址)吗?
这是因为对于Integer包装器类,==将返回值true直到128。这是故意的
如果您对密码原语和对象引用有疑问,请研究以下程序
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-23 13:35:56
java编译器自动插入intValue和Integer.valueOf调用,以便在int和Integer之间进行转换。例如,下面是问题中的代码片段:
void show(Integer x){
x *=100 ;
}下面是真正发生的事情:
void show(Integer x) {
int unboxed = x.intValue();
unboxed *= 100;
}如您所见,行x *= 100并不真正更改传入的Integer对象,它只更改从该Integer对象提取的int值。
类似地,问题中的代码wi == pi实际上意味着wi.intValue() == pi,这解释了您的观察。
发布于 2013-08-23 13:01:15
Java使用了“按值调用”的概念,详细描述了这里,因此在您的示例中,x *=100;在方法中只更新局部变量
https://stackoverflow.com/questions/18403342
复制相似问题