3

I am trying to determine if the unboxing method Java uses can be modified. The purpose of this is to be able to do:

class IInteger extends Integer {
  @Override
  public int unboxToPrimitive(){
    return ++val;
  }
}

IInteger i = 10;
System.out.print(i); // 11

That is, overloading the method and having it return ++val instead of val.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
Felix
  • 428
  • 3
  • 9
  • 5
    Final classes like `Integer` cannot be extended - https://stackoverflow.com/questions/5181578/use-of-final-class-in-java – twinklehawk Feb 06 '18 at 15:52
  • 5
    I would be interested in the problem you trying to solve with this override? – lwi Feb 06 '18 at 15:52
  • Something like this is possible in Ruby, but not Java. – Sid Feb 06 '18 at 15:54
  • 2
    @lwi trying to solve the `return i<=1 && i>=1 && i!=1` to equal true problem – Felix Feb 06 '18 at 16:01
  • @twinklehawk Thank you, I noticed, but was hoping perhaps a similar class could be used. As long as I have my IInteger class I don't care what it extends. Should have been more clear. – Felix Feb 06 '18 at 16:03
  • You can download the sources, change them, and recompile the entire JDK class hierarchy, but of course this change will be local to your machine. And no, I haven't tried. :) Maybe there are pitfalls, I'm not aware of. – user unknown Feb 06 '18 at 16:08
  • 2
    @userunknown even if you change the `Integer` class to make it non-final, you still wouldn't be able to box to a custom type, because [the language only defines boxing between specific types](https://docs.oracle.com/javase/specs/jls/se9/html/jls-5.html#jls-5.1.7). – Andy Turner Feb 06 '18 at 16:27
  • @AndyTurner: so there is no unboxToPrimitive-method at all to overwrite? Well, then he has to hack at the compiler level. – user unknown Feb 06 '18 at 17:07
  • @Felix I am wondering when should `i<=1 && i>=1 && i!=1` evaluate to true? If i is an Integer it will be unboxed leading to an always false expression. Or am I missing the point somehow? – lwi Feb 07 '18 at 11:50
  • 1
    @lwi it's one of those brain teasers they sometimes ask at interviews. In javascript for example it can be done by overloading the valueOf method to return ++x so that each time you evaluate it, it returns a different value, thus being able to make the expression return true. It has no use in practical programming. – Felix Feb 08 '18 at 12:10

2 Answers2

8

No. It isn't possible, first Integer is final and cannot be sub-classed. Second, you can't overload operators in Java. Next, you can't have autoboxing for custom types. Finally, you aren't actually using unboxing in your println (you can call print on a reference type).

However, if I understand what you really want, you could use an AtomicInteger and incrementAndGet() (instead of get() and change your other methods as necessary).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thank you for your reply, quite comprehensive. I tried finding out what method gets called when you println an Integer, is it just toString()? – Felix Feb 06 '18 at 15:56
  • @Felix `Object.toString`, which is overloaded in `Integer` see [GrepCode](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Integer.java#Integer.toString%28%29) for an example. – Elliott Frisch Feb 06 '18 at 16:14
4

No, you can't even extend java.lang.Integer as it's a final class.

payloc91
  • 3,724
  • 1
  • 17
  • 45
  • Thank you. I saw that I cannot extend Integer but was hoping maybe there was a loophole somehow. I don't insist on Integer itself as long as I can make my class and use it as in the example. – Felix Feb 06 '18 at 15:58
  • 1
    @Felix you can override `java.lang,Number` though but you can't unbox it as it does not have an equivalent primitive type the `JVM` can convert to... – payloc91 Feb 06 '18 at 16:27