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.
"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 thevalueOf
will guarantee 1 single object)– Jefferson Quesado
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 :)
– Maniero
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.
– Jefferson Quesado