Java Notes
Review the Java array concepts commonly checked in interviews: fixed length, indexing, default values, copying references, and safe loop boundaries.
Use these questions to check whether you understand array behaviour, not just array syntax.
Is an array fixed in size?
Yes. Its length is set when the array is created and cannot change. You can change an existing element, but not add another position. Choose ArrayList when the collection must grow or shrink.
What is the difference between length and length()?
An array has the field array.length. A String has the method text.length(). Writing numbers.length() is a compile-time error; writing name.length is also an error.
What values does a new array contain?
Instance and local arrays receive default values: numeric arrays contain 0, boolean arrays contain false, and reference arrays contain null. Local variables outside an array do not receive automatic values and must be assigned before use.
Does assignment copy an array?
No. It copies the reference, so both variables point to the same array.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Use Arrays.copyOf(original, original.length) when you need an independent copy.
Why does ArrayIndexOutOfBoundsException happen?
Valid indexes run from 0 through array.length - 1. A loop using index <= array.length tries one position too far. The usual safe condition is index < array.length.
How do you pass an array to a method?
Java passes the array reference by value. The method receives its own copy of the reference, but it can still change elements in the shared array. Reassigning the parameter to another array does not change the caller's variable.
Continue the lesson path