2 Replies - 845 Views - Last Post: 29 October 2017 - 06:28 AM

#1 Sciuriware   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 17-August 13

new Boolean and new Integer gone in JAVA9

Posted 29 October 2017 - 05:17 AM

Hello, since I installed JAVA9 I get zillions of errors about the use of above constructors.
Why did I code like that? Because you can't refer to a primitive. That's why.
So, a HashMap from an object to int or boolean should be a HashMap to a wrapped value.

The docs state that new Boolean(B) should become Boolean.valueOf(B) :: is that the same for me?
Likewise new Integer(i) >>> Integer.valueOf(i)?

If this is true, what would be the reason for the change?
I easily can replace all occurrences in my sources,
but it feels like a useless conversion.
Thanks in advance.
;JOOP!

Is This A Good Question/Topic? 0
  • +

Replies To: new Boolean and new Integer gone in JAVA9

#2 xclite   User is online

  • I wrote you an code
  • member icon


Reputation: 1387
  • View blog
  • Posts: 4,214
  • Joined: 12-May 09

Re: new Boolean and new Integer gone in JAVA9

Posted 29 October 2017 - 05:32 AM

See here: https://bugs.openjdk...wse/JDK-8176335

Of note:

Quote

Using autoboxing or the `valueOf` method reduces memory footprint compared to the constructors, as the integral box types will generally cache and reuse instances corresponding to small values. The special case of `Boolean` has static fields for the two cached instances, namely `Boolean.FALSE` and `Boolean.TRUE`.


Using "new" basically means you end up with multiple identical instances of things like the number 2, while using valueOf lets the class manage cached instances, such that when you want an Integer of value 2, you get the same one instead of creating garbage.
Was This Post Helpful? 3
  • +
  • -

#3 Sciuriware   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 17-August 13

Re: new Boolean and new Integer gone in JAVA9

Posted 29 October 2017 - 06:28 AM

Thanks for the release note, that makes it clear.
;JOOP!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1