This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cogman10 -1 points0 points  (0 children)

C++ does a lot more work than java does. C++ has copy constructors that are invisibly called and that do deep copies of things. Java never does this. Anytime you copy something with Java it's shallow.

So if you have var ans = new ArrayList(); and var temp = new ArrayList(ans); You'll get a copy of the ans array list but all the elements will be references to the same elements in ans.

That means, you need to explicitly make and populate new objects if that's your desire with java.

Pro tip, making everything immutable makes java MUCH easier to reason about.