Reputation: 111
My last last java assignment that I am all okay except this two lines below. What is the intend of it, and how would I do that? Anybody giving a clue about that are appreciated.
I am not allowed to use primitives on this one
Do not assign given Wrapper Class objects (Integer, Char, Double, etc.) directly. Use a copy of objects.
Yesterday ı talked to my assistant and he sad that when you assign the value like this:
Integer asd ;
asd = new Integer(10);
Instead of this:
Integer asd ;
asd = 10 ;
it copies the object and assigns the value. Then (I think) the old record gets eligible for garbage collector.
Upvotes: 1
Views: 160
Reputation: 4259
You may have to serialize and unserialize it. Then you can get another copy of the object. See https://stackoverflow.com/a/2836659/940313
btw, Object.clone()
is protected
by default, so you may not be able to copy an object in this way, unless the corresponding class really implements clone()
and declares it as public
.
Upvotes: 0
Reputation: 2475
I guess it means you should use methods like Integer#parseInt and Integer#valueOf instead of creating the object directly using a constructor.
Upvotes: 1