1

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 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. I am missing something very basic, please help me out.

iprashant
  • 155
  • 1
  • 2
  • 11
  • The simple reason is autoboxing/unboxing for Primitive Wrapper classes. Check this out : https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html – SacJn Sep 28 '15 at 05:25

4 Answers4

2
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. 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. This is what is happening here.

The auto-boxing or un-boxing which is an automatic conversion, it is defined for java primitives only. 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.

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
  • 1
    thanks sir, this unboxing concept can be implemented to any custom class? – iprashant Sep 28 '15 at 05:23
  • 1
    @uniquephase Autoboxing is between a simple type (int, byte,...) and its related class (Integer, Byte,...). Your question doesn't make sense. If you need to "increment" an object of class D, implement D.increment(). – laune Sep 28 '15 at 05:24
  • 1
    @uniquephase no, it can not be. There's no way to get that to compile. Also operator overloading is not supported in java. [Reference](http://stackoverflow.com/a/17619784/2254048). – YoungHobbit Sep 28 '15 at 05:25
  • 1
    @YoungHobbit sir what i thought to implement was if i have a class which has some integer members. If after creating object and initialization, i do increment in reference increment all member values. There are other ways to do this but with lack of knowledge of autoboxing and looking at integer class reference behavior i stuck at this. Thanks for the help! – iprashant Sep 28 '15 at 05:32
  • @uniquephase for that you need to provide your own implementation. Just a simple method which will increment the values. – YoungHobbit Sep 28 '15 at 05:33
1

This is because of Autoboxing for wrapper classes which was introduced from jdk 1.5. Internally java will convert the integer reference to int and increment it. You can't increment the objects.

Shriram
  • 4,343
  • 8
  • 37
  • 64
  • 1
    thanks man! is it possible to implement autoboxing so that if i increment reference to my class it will increment data members of my class? – iprashant Sep 28 '15 at 05:19
  • 2
    @uniquephase, you can not implement autoboxing for your class. Instead you can expose simple method like `incrementX()` in your `D` class which will internally **increment** value of `x` in your `D` class. – Naman Gala Sep 28 '15 at 05:28
1

Java has no way of overloading operators for arbitrary classes. Thus, D d; d++ is just not available. 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.

laune
  • 31,114
  • 3
  • 29
  • 42
  • 1
    you are right, but i was stuck at reference increment. From method we can always manipulate data member values. Thanks! – iprashant Sep 28 '15 at 05:36
1

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. Whenever we use object of wrapper class in an expression, automatic unboxing and autoboxing is done by 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. You can also take a look at How java auto boxing/unboxing works?

Community
  • 1
  • 1
sifho
  • 645
  • 5
  • 10