• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Less rigorous description of autoboxing?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OCP Java SE11 Programmer I Study Guide, Page 201, Penultimate paragraph.

When discussing autoboxing into Integer, an example is given:



We want to remove the element 1 but the statement at Line 4 actually removes the element at index 1, which is 2, so what remains is 1 instead of 2. To get the desired result, the book recommend that Line 4 be replaced with numbers.remove(new Integer(1)).

This is one solution, but strictly speaking, Integer(int) has been deprecated since Java 9. So for the sake of rigor, it seems better to use numbers.remove(Integer.valueOf(1)), I guess?

An online compiler like Techie Delight accepts new Integer(1) without any problem, while an IDE like Eclipse will tell you that "The constructor Integer(int) is deprecated since version 9" and provide two quick fixes; one of which is adding @SuppressWarnings "deprecation" to main(). Of course, an IDE also accepts new Integer(1).
 
Bartender
Posts: 3705
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the reason for deprecating new Integer is because Java API caches Integers from -128 to 127 range by default (for the cases of autoboxing, to avoid repeating same most often used instances), and new keyword always makes the new object on the Heap.
So, there is no way to control new Integer(1); new Integer(1);-- these will be different references, and == will give false.
While Factory Method is a nice way to control instances creation and cache as needed.
So, surely Integer.valueOf(1) must be used.



 
Ranch Foreman
Posts: 347
8
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are so many things in Java because of its history and steadfast commitment to (almost) never break old code.

Continuing to allow instead of is notable but not nearly the worst.  If the code gets called tons you will waste a little bit of memory.

The fact that you can make an ArrayList containing stray dogs, favorite bookmarks, phone numbers, dangerous drugs, your grades from school and your Grandma's best friends, despite the fact that most sane people think you never should do that...and that arguably you can do so because generics are a "New Feature" from 2004 that we don't want to force people to adopt in slow-changing code...seems to be a much more serious issue to me.

There are courses and other material still "out there" by people who are reasonably highly respected and arguably quite skillful, that go thru the entire Java Collections Framework only showing generics in the few places absolutely needed (or maybe nowhere, but avoiding storing heterogeneous objects only in those cases) and then presenting Generics as a "new feature" right at the end...

I understand why Oracle doesn't want to break old code, but that is not necessarily an excuse for others to teach people to write Java like it is 2003...I agree with the OP and the reply, and am grateful that these book authors very rarely show bad old ways to do things, if they do it is in service of preparing test takers for something they might see on the exam.
 
Miles Jiang
Greenhorn
Posts: 11
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks your explanation @Mikalai and @Jesse. Now I got the point.
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
ScroogeXHTML - a fast and small RTF to HTML5 and XHTML converter library
https://coderanch.com/t/742149/ScroogeXHTML-RTF-HTML-XHTML-converter
reply
    Bookmark Topic Watch Topic
  • New Topic