Coding/Learn/java/arrays introduction
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnArrays in Java
21 / 31 0%
Lesson 21 of 31
6 mins read

Arrays in Java

PreviousNext Lesson
In Plain English

An array in Java is a container object that holds a fixed number of values of a single uniform type. The length of an array is established when the array is created and cannot be dynamically resized afterwards.

The Mental Model

"An array is like an egg carton with fixed compartments. A 6-egg carton (length 6) holds exactly 6 items of the exact same type (eggs), numbered from spot 0 to 5."

Deep Dive: How It Works

Declaration with size allocation: `int[] numbers = new int[5];` (allocates 5 integers initialized to 0).

Declaration with literal values: `String[] fruits = {"Apple", "Banana", "Orange"};`

Default values: Numeric arrays initialize to 0/0.0, boolean arrays to false, and object/String arrays to null.

Syntax Blueprint

int[] scores = new int[3];
String[] names = {"Alice", "Bob", "Charlie"};

Square brackets [] declare an array type. Array size is fixed upon instantiation.

PointCore Rules to Remember

Point star
Fixed Capacity: Array size cannot change after creation.
Point star
Homogeneous Type: All elements must match the declared data type.
Point star
Array Literal Syntax: Use curly braces { item1, item2 } for quick initialization.

Common Beginner Traps & How to Fix Them

Mistake:Putting array size in type declaration: int[5] arr = new int[5];

Why it happens: Java prohibits placing the dimension size on the left side of the assignment.

How to fix: Keep brackets empty on the left: int[] arr = new int[5];

Pro Tip: Prefer int[] numbers over int numbers[]: placing brackets by the type is modern Java style.

Live Interactive Example

Hit Run Code to see it live
Creating and Inspecting Arrays
Java 13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Output Console
Click "Run Code" to view the rendered output.
How it works: Array elements are accessed with [0]. languages.length returns total item count.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Create an array of color names

  • PointDeclare a String[] named "colors" with {"Red", "Green", "Blue"}.
  • PointPrint "Second color: " + colors[1].
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates