autoboxing - constant expression required in Java behaviour change for int and Integer -


for little code , willing save boxing/unboxing hassle introduced because further have use int constant integer (mandated generics method call), went simplified example

enum soq {     typea,     typeb,     typec;     public static final int width = 10;      public static soq of(int i) {         switch (i) {             case uiordinals.typea_ord:                 return typea;             case uiordinals.typeb_ord:                 return typeb;             case uiordinals.typec_ord:                 return typec;         }         throw new unsupportedoperationexception("not supported yet."); //todo : implement     }      public static class uiordinals {          public static final int typea_ord = width * 1;         public static final int typeb_ord = width * 2;         public static final int typec_ord = width * 3;     } } 

to

enum soqbox {     typea,     typeb,     typec;     public static final integer width = 10;      public static soqbox of(int i) {         switch (i) {             case uiboxordinals.typea_ord:                 return typea;             case uiboxordinals.typeb_ord:                 return typeb;             case uiboxordinals.typec_ord:                 return typec;         }         throw new unsupportedoperationexception("not supported yet."); //todo : implement     }      public static class uiboxordinals {          public static final integer typea_ord = width * 1;         public static final integer typeb_ord = width * 2;         public static final integer typec_ord = width * 3;     } } 

no such big deal.

as said went way because helper method relaying on 'width'. method (which called often) needed generic type parameters (no primitive allowed), there came need introducing integer.

well seems obvious eyes typennn_ord still constant, , still compile-time-constant that's not point of view of java compiler : "constant expression required" error.

just wanted know why in simple case ? has boxing, how , why ?

meanwhile ,i went primitive int, hoping moderns jdk jdk 8 , jdk 9 won't waste effort in boxing/unboxing.

by way, subsidiary, know how java competes on point nowadays ? should still minimize ?

auto-unboxing works calling appropriate method in background. integer.intvalue() in case. cannot call method in case statement must primitive value check.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -