PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

Java Arrays

← All stacks

Theory

50/270

Java Arrays

An array is a fixed-size row of boxes, all the same type. Create: int[] a = new int[5]; or int[] a = {10, 20, 30}; Indexes start at 0. First item is a[0]. Length is a.length — a field, not a method.

Loop with for or for-each. Size cannot grow later. If you need a growing list, use ArrayList. Last valid index is a.length - 1. a[a.length] throws ArrayIndexOutOfBoundsException.

Interview line — fixed size, same type, index from 0, length field.

Let's take this on the board with one tiny Main class — no extra files. Java Arrays — output: 10 then 20 then 30. Index 0, 1, 2. a[3] would throw ArrayIndexOutOfBoundsException. Start from main, go line by line, and stop at each print. That output is the proof for Arrays.

Diagram
index →  0    1    2
  array → [10,  20,  30]
           ↑         ↑
         first   length-1
Exam tip

Last index is length - 1. Contrast with ArrayList.

Example

public class Main {
  public static void main(String[] args) {
    int[] a = {10, 20, 30};
    for (int i = 0; i < a.length; i++) {
      System.out.println(a[i]);
    }
  }
}

Java Arrays — output: 10 then 20 then 30. Index 0, 1, 2. a[3] would throw ArrayIndexOutOfBoundsException.

Short notes

  • DefArray = fixed-size row of same-type values.
  • RuleIndex from 0. a.length is a field. Last index = length - 1.
  • RememberSize cannot grow. Use ArrayList when you need grow/shrink.
  • Trapa[a.length] → ArrayIndexOutOfBoundsException.

Questions

1

What is an array?

2

Is length a method on arrays?

3

When do you use ArrayList instead?

Previous← Encapsulation in JavaNextMultidimensional Arrays in Java →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.