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.

2
  • What language? Can you post your code? Commented Nov 18, 2012 at 10:14
  • Searching for "java copy objet" finds lots of information, like this SO question, this Wikipedia article Commented Nov 18, 2012 at 10:21

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Or it could mean, "don't use autoboxing". I.e. don't do Integer myInt = 10;.
0

Kinda sounds like they're fishing for

object.clone();

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.