Were the Wrapper classes depreciated in Java?

Asked

Viewed 77 times

2

I’m following a sequence of old video lessons, in the video, Java 7 is used and the following code compiles:

Long var3 = new Long("100");

I tried to compile and received the following message:

Note: Wrappers.java uses or overrides a deprecated API. Note:
Recompile with -Xlint:deprecation for details.

1 answer

3


The class did not happen, nor would it make sense, almost all codes that do no trivial things use these classes. But some methods of these classes are now considered obsolete and should use another way, in your case is a constructor and should use valueOf() in the place that is a static factory method of the class (I do not like it very much because it hides the allocation that before was explicit, but who program in Java does not usually care for it, then complains of inefficiency).

It might help because you’re not the first to make this mess: It is ideal to use primitive types in Java?.

  • 1

    "Hides the allocation that was once explicit". Well, with the new he at all times created a new object. For example, for (long i = 0; i < 100; i++) { Long l = new Long(10L); } will create 100 instances of identical objects. So, as this was a practice that abused the misuse, they preferred to launch this alert. So, using the factory, for (long i = 0; i < 100; i++) { Long l = Long.valueOf(10L); } possibly you will only have one object. (OK, I believe 10 is within reach of the permanent cache, then the valueOf will guarantee 1 single object)

  • Some cases he makes the flyweight, but not most then it gets bad in realistic cases. Of course if the person wants to do wrong, he will do it, but many people must think that not even allocation occurs, although those who are bad do not know what is allocation :)

  • 1

    In the documentation itself he says that "It is rarely appropriate to use this constructor". That means they at least have the decency to admit that they can have better codes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.