This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.
This is a horrible "bug" in Java that the editor tries to save you from, but fails. Integer x = new Integer(4); Integer y = new Integer(4); if( x == y ) { // GOOD hint here: "Integer values compared using == or !=" System.out.println(x + " == " + y); } else { System.out.println(x + " != " + y); // prints: "4 != 4" } if( x.intValue() == y.intValue() ) { // BAD hint here: "Unnecessary unboxing" System.out.println(x + " == " + y); // prints "4 == 4 " } else { System.out.println(x + " != " + y); } The first hint is GOOD The second hint is BAD!!! You do need to unbox.
Another exemple of erroneous "unnecessary unboxing" hint, In java 8, writing these line : String values = "some text"; Map<Character, Long> freqV = values.chars().boxed().collect(Collectors.groupingBy(integ -> (char) integ.intValue(), Collectors.counting())); // "unnecessary unboxing" hint If I remove the unboxing, the code does not compile...
I ran into this one, today, with AbstractAction: putValue(Action.MNEMONIC_KEY, Integer.valueOf('E')); Netbeans suggests that you remove Integer.valueOf(); however, doing so changes the semantics of the code. By removing the valueOf() call, the 'E' is boxed to Character rather than Integer. The code compiles, because the second method parameter takes an Object, but it crashes at runtime: java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.Integer javax.swing.AbstractButton.setMnemonicFromAction(AbstractButton.java:1256)
Defect, not an enhancement.
(In reply to brucewee from comment #0) > if( x.intValue() == y.intValue() ) { // BAD hint here: "Unnecessary unboxing" > > The first hint is GOOD > The second hint is BAD!!! You do need to unbox. Actually you don't need to unbox ONE of the operands. Once one of the operands is a primitive, the other one will according to JLS 15.21.1 undergo numeric promotion (JLS 5.6.2), which implies unboxing conversion. We may argue whether the result is readable and I would say it's not - I'll suppress the hint for this special case (= binary op or conditional alternative where the other argument is also being unboxed) (In reply to lolo_101 from comment #1) > String values = "some text"; > Map<Character, Long> freqV = > values.chars().boxed().collect(Collectors.groupingBy(integ -> (char) > integ.intValue(), Collectors.counting())); // "unnecessary unboxing" hint > OK, excluding a case when the auto-unboxed type is not assignable to casted-to type (In reply to vicricker from comment #2) > I ran into this one, today, with AbstractAction: > putValue(Action.MNEMONIC_KEY, Integer.valueOf('E')); > Hint suppressed if the target type is a reference & the original passed type and the autoboxed type differs == instance of a different type would be passed after hint is applied.
Fixed in jet-main#47be3a84e239
Integrated into 'main-silver', will be available in build *201504120001* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress) Changeset: http://hg.netbeans.org/main-silver/rev/47be3a84e239 User: Svata Dedic <sdedic@netbeans.org> Log: #243581: improved boxing/unboxing suggestions