1

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.

c.k.
  • 111
  • 3
  • What language? Can you post your code? – Ravi Y Nov 18 '12 at 10:14
  • Searching for "java copy objet" finds lots of information, like [this SO question](http://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java), [this Wikipedia article](http://en.wikipedia.org/wiki/Clone_\(Java_method\)) – dbr Nov 18 '12 at 10:21

3 Answers3

1

I guess it means you should use methods like Integer#parseInt and Integer#valueOf instead of creating the object directly using a constructor.

The Cat
  • 2,375
  • 6
  • 25
  • 37
0

Kinda sounds like they're fishing for

object.clone();
johv
  • 4,424
  • 3
  • 26
  • 42
0

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.

Community
  • 1
  • 1
Xiao Jia
  • 4,169
  • 2
  • 29
  • 47