Operator overloading in Java (Integer wrapper class)?

I came across an example in the OCJP book. It says:

Integer y=new Integer("20");
y++;   (un-wraps it)
System.out.println(y);

      

Now this will print 21. Hence, this makes me think, how did the compiler even know that in y++

it should expand it to an int and increment it? Integer is a regular class (Wrapper class maybe?) Is this an overload of an operator internally?

Is there a way to do this for my own custom class, if possible?

+3


source to share


1 answer


Java uses the Autoboxing and Unboxing feature to expand this integer and increase it. You cannot implement this functionality in your custom classes. It is only available for those wrapper classes for Java primitive types.



+1


source







All Articles