Java arrays: how do length, length() and size() differ? What is a shallow copy, and how are 2D arrays stored?
Short answer
Arrays use length (a field), String uses length(), collections use size(). Every built-in way to copy an array is a shallow copy.
3 points to say:
- Shallow copy: only the outer shell is copied, the elements are shared.
- A 2D array is an array of arrays, copy each row.
- Print with
Arrays.toString, compare withArrays.equals.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.