Omar Nassar wrote:
I have red alot that says you have to reuse an object when ever you can.
how can we get benefit from reusing an object in such a case?
All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Campbell Ritchie wrote:object pool; that may be a level too advanced for the current thread
All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Please show us where you saw that. We like to know sources; in this case for the reason shown after the third •Omar Nassar wrote:hello all,
I have red alot that says you have to reuse an object when ever you can. . . .
I suspect that advice may have been given by people with a “little learning”. With a bit more learning, you find that it is dangerous to have two references to the same mutable object. That problem does not apply to immutable types like java.lang.String. Nor does it occur with primitives, e.g. an int. As you see, you only have one object and every reference to that reflects the changes.Alexander Pope wrote:A little learning is a dangerous thing;
Drink deep, or taste not the Pierian spring.
And here is a second way to do it.A few minutes ago, I wrote: . . .
Two ways you can solve the problem.
. . .
API Documentation, first page wrote: Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared.
Campbell Ritchie wrote:Not sure I understand the last post, but I was trying to say that reusing mutable objects is not necessarily as good an idea as OP has been told.
Can you put primitives into an ArrayList? Surely they are boxed to the corresponding wrapper objects. But those wrappers are all immutable.Chris R Barrett wrote: . . . the differences between primitives in an ArrayList and object references in a ArrayList. . . .
Campbell Ritchie wrote:Can you put primitives into an ArrayList?
objects are expensive to create. Where it is reasonable to reuse the same object, you should do so.
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|