简体   繁体   English

为什么整数类对象的引用中的增量操作整数值?

[英]why is increment in reference of integer class object manipulates integer value?

I have a basic doubt, we can increment reference of Integer class object, while not reference of any other class(D class has 1 Integer data member and one parameterized constructor).我有一个基本的疑问,我们可以增加 Integer 类对象的引用,而不是任何其他类的引用(D 类有 1 个 Integer 数据成员和一个参数化构造函数)。

Integer x=new Integer(10); x++;
    D d=new D(10); d++;

here both x and d are reference still we are able to increment Integer reference while not any other reference.这里 x 和 d 都是引用,我们仍然可以增加整数引用而不是任何其他引用。 I am missing something very basic, please help me out.我缺少一些非常基本的东西,请帮助我。

Integer x=new Integer(10); x++;

Here java compiler will do the un-boxing of the object, which converting the Integer object into primitive int .这里Java编译器将做un-boxing的对象,该对象将所述的Integer对象到原始int Then the increment operation will be performed.然后将执行增量操作。 This is only defied for java primitives, the auto-boxing is converting primitive to wrapper object and reverse of it is call un-boxing .这仅适用于 java 原语, auto-boxing将原语转换为包装器对象,反之则调用un-boxing This is what is happening here.这就是这里发生的事情。

The auto-boxing or un-boxing which is an automatic conversion, it is defined for java primitives only. auto-boxingun-boxing是一种自动转换,它仅针对 java 原语定义。 So it can not be performed on other objects.所以它不能在其他对象上执行。 Remember, the object are not just memory references like C or C++ that we can increment them.请记住,对象不仅仅是像C or C++这样的内存引用,我们可以增加它们。

This is because of Autoboxing for wrapper classes which was introduced from jdk 1.5.这是因为从 jdk 1.5 引入的包装类的自动装箱。 Internally java will convert the integer reference to int and increment it.在内部,java 会将整数引用转换为 int 并递增它。 You can't increment the objects.你不能增加对象。

Java has no way of overloading operators for arbitrary classes. Java 无法为任意类重载运算符。 Thus, D d; d++因此, D d; d++ D d; d++ is just not available. D d; d++只是不可用。 You can do:你可以做:

public class D {
    private int value;
    public D( int v ){
        value = v;
    }

    public void increment(){
        value++;
    }
}

Now you can do现在你可以做

D d = new D(10);
d.increment();

and d's value will be 11. d 的值为 11。

According to Oracle Java Documentation , The automatic conversion of primitive data types into its equivalent wrapper type is known as autoboxing and opposite operation is known as unboxing .根据Oracle Java 文档,将原始数据类型自动转换为其等效的包装器类型称为autoboxing而相反的操作称为unboxing Whenever we use object of wrapper class in an expression, automatic unboxing and autoboxing is done by JVM.每当我们在表达式中使用包装类的对象时,JVM 都会完成自动拆箱和自动装箱。

Integer object;
object = 100;        //Autoboxing of int
++object;

When we perform an increment operation on Integer object, it is first unboxed, then incremented and then again reboxed into Integer type object.当我们对 Integer 对象执行递增操作时,它首先被拆箱,然后递增,然后再次重新装箱为 Integer 类型的对象。 You can also take a look at How java auto boxing/unboxing works?您还可以查看Java 自动装箱/拆箱如何工作?

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM