Skip to main content

Introduction

  • An array is a collection of data of similar type.
  • Array is also called a Homogeneous data structure.
  • Elements of an array are stored in contiguous memory locations.
  • Arrays are objects in Java.

Three tasks to remember while working with arrays:

  1. Array Declaration
  2. Array Construction
  3. Array Initialization
  • Arrays can be constructed with multiple dimensions:
    • 1-D Array
    • 2-D Array
    • etc.

2.10.1 Single Dimensional Arrays

  • A single dimensional array is also called a 1-D array.

Array Declaration

Syntax:

[modifiers]<dataType> <refVarName>[];
[modifiers]<dataType>[]<refVarName>;
[modifiers]<dataType> []<refVarName>;

Examples:

int arr1[];
float []arr2;
String names[];

🧠 Important Note:

  • arr1, arr2, and names are reference variables
  • They store:
    • null OR
    • address of an array object

👉 Initially:

arr1 → null (8 byte s reference)
arr2 → null (8 byte s reference)
names → null (8 byte s reference)

Explanation

  • Array variables do not store actual data directly
  • They store reference (address) to array object
  • Before initialization → value is null

Lab234.java

class Lab234 {

static int arr[];
static String names[];

public static void main(String[] args) {
System.out.println(arr);
System.out.println(names);
}
}

Output:

null
null

Lab235.java

class Lab235 {

public static void main(String[] args) {
int arr[5];
String names[3];

System.out.println("Hello Guys");
}
}

Output:

Compilation Error

👉 Reason:

  • This is invalid syntax in Java
  • Correct way:
int[] arr = new int[5];

String[] names = new String[3];

Quick Summary

  • Arrays = same type collection
  • Stored in continuous memory
  • Array variable = reference (not actual data)
  • Default value = null
  • Must use new for allocation

Array Construction

Syntax

<refVarName>=new <dataType>[<size>];

Examples

1) Integer Array

int arr1[]=new int[3];

👉 Memory representation:

  • Each element = 4 bytes
  • Default value = 0

2) Float Array

float arr2[]=new float[5];
  • Each element = 4 bytes
  • Default value = 0.0

3) String Array

String names[]=new String[3];
  • Each element = 8 bytes (reference)
  • Default value = null

📌 Important Concept: length

  • length is a variable (not a method)
  • Used to get the number of elements in an array

Examples

arr1.length// return s 3
arr2.length// return s 5
names.length// return s 3

Lab236.java

class Lab236 {

public static void main(String[] args) {
int arr1[];
float arr2[];
String names[];

arr1 = new int[3];
arr2 = new float[5];
names = new String[4];

System.out.println(arr1.length);
System.out.println(arr2.length);
System.out.println(names.length);
}
}

Output:

3
5
4

Lab237.java

class Lab237 {

public static void main(String[] args) {
int arr1[];

arr1 = new int[]; // invalid

System.out.println(arr1.length);
}
}

Output:

Compilation Error

👉 Reason:

  • Size must be specified OR values must be provided

✔ Correct ways:

arr1=new int[3];

OR

arr1=new int[]{1,2,3};

Quick Summary

  • Array creation uses new
  • Default values:
    • int → 0
    • float → 0.0
    • String → null
  • length → gives array size
  • Array size must be defined at creation

Array Initialization

Key Points

  • Once an array is constructed, its elements are initialized with default values based on the data type.
  • You can also assign your own values using index positions.

Example: Default Initialization

int arr[];
arr=new int[3];

📊 Memory Representation

  • Size = 3 → Only 3 elements stored
  • Default values = 0

📌 Index Meaning

  • arr[0] → first element → value 0
  • arr[1] → second element → value 0
  • arr[2] → third element → value 0

Manual Initialization

arr[0]=10;
arr[1]=20;
arr[2]=30;

📊 After Initialization

📌 Accessing Elements

  • arr[0] → 10
  • arr[1] → 20
  • arr[2] → 30

Lab238.java

class Lab238 {

public static void main(String[] args) {
int arr[];
arr = new int[3];

System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);

System.out.println();

for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}

Output

0
0
0

0
0
0

Lab239.java

class Lab239 {

public static void main(String[] args) {
int arr[];
arr = new int[3];

for (int i = 0; i < arr.length; i++) System.out.println(arr[i]);

System.out.println("------");

arr[0] = 10;
arr[1] = 20;
arr[2] = 30;

for (int i = 0; i < arr.length; i++) System.out.println(arr[i]);
}
}

Output

0
0
0
------
10
20
30

Quick Summary

  • Arrays get default values automatically
  • Use index (arr[i]) to assign values
  • Looping helps access all elements easily
  • Index starts from 0

Lab240.java

class Lab240 {

public static void main(String[] args) {
String arr[];
arr = new String[3];

for (int i = 0; i < arr.length; i++) System.out.println(arr[i]);

System.out.println("------");

arr[0] = "Java";
arr[1] = "Learning";
arr[2] = "Center";

for (int i = 0; i < arr.length; i++) System.out.println(arr[i]);
}
}

Output

null
null
null
------
Java
Learning
Center

Array Declaration and Construction

Key Concept

  • You can declare and construct an array in a single statement.

Syntax

<dataType><refVarName>[]=new <dataType>[<size>];
<dataType>[]<refVarName>=new <dataType>[<size>];

Examples

int arr[] = new int[5];

int[] arr = new int[5];

🧠 Important Points

1️⃣ Reference Variable Memory

  • When you declare an array:
    • 8 bytes memory is allocated for the reference variable
    • It is initialized with null
arr → null (8 byte s)

2️⃣ Array Object Memory Allocation

  • Memory for elements is allocated based on array size.

📊 Representation

  • Example: new int[5]
    • Creates 5 memory blocks
    • Index range: 0 → 4

Quick Summary

  • Arrays can be:
    • Declared + constructed together
  • Reference variable:
    • Stores address of array
    • Default = null
  • Memory:
    • Allocated dynamically
    • Based on size
  • Index:
    • Always starts from 0

3) Memory blocks will be initialized with default value as per array data type.

Example (int array):

Address: 99723

Index: 0 1 2 3 4
Value: 0 0 0 0 0

4) Memory will be allocated for length variable and will be initialized with size of an array.

Address: 99723

Index: 0 1 2 3 4
Value: 0 0 0 0 0

length = 5

5) Array object address will be assigned to reference variable.

arr → 99723 (8 byte s)

Memory at 99723:

Index: 0 1 2 3 4
Value: 0 0 0 0 0

length = 5

👉 Notes:

  • 4 bytes memory required for each block (int array)
  • Length always requires 4 bytes memory

Lab241.java

class Lab241 {

public static void main(String[] args) {
int arr[] = new int[3];

System.out.println("Len :" + arr.length);

for (int i = 0; i < arr.length; i++) System.out.println(arr[i]);

System.out.println("------");

arr[0] = 10;
arr[1] = 20;
arr[2] = 30;

for (int i = 0; i < arr.length; i++) System.out.println(arr[i]);
}
}

Lab242.java

class Lab242 {

public static void main(String[] args) {
String arr[] = new String[3];

for (int i = 0; i < arr.length; i++) System.out.println(arr[i]);

System.out.println("------");

arr[0] = "Java";
arr[1] = "Learning";
arr[2] = "Center";

for (int i = 0; i < arr.length; i++) System.out.println(arr[i]);
}
}

Lab243.java

class Lab243 {

public static void main(String[] args) {
int arr[] = new int[5L];
int arr1[] = new int[5.0f];
}
}

Lab244.java

class Lab244 {

public static void main(String[] args) {
byte b = 5;
int arr1[] = new int[b];

short s = 4;
int arr2[] = new int[s];
System.out.println(arr1.length);
System.out.println(arr2.length);

char ch = 'A';
int arr3[] = new int[ch];
System.out.println(arr3.length);

int arr4[] = new int[5];
System.out.println(arr4.length);

int arr5[] = new int[10 + 20 / 5];
System.out.println(arr5.length);

double d = 12.345;
int arr6[] = new int[(int) d];
System.out.println(arr6.length);
}
}

Lab245.java

class Lab245 {

public static void main(String[] args) {
int arr[] = new int[2147483647];
System.out.println(arr.length);
}
}

Lab246.java

class Lab246 {

public static void main(String[] args) {
int arr[] = new int[2147483648];
System.out.println(arr.length);
}
}

Lab247.java

class Lab247 {

public static void main(String[] args) {
int arr[] = new int[0];
System.out.println(arr.length);
}
}

Lab248.java

class Lab248 {

public static void main(String[] args) {
int arr[] = new int[-1];
System.out.println(arr.length);
}
}

Diagram Explanation

int arr[] = new int[0];

arr → 99733

Memory:
length = 0

👉 Note:

  • Length always requires 4 bytes memory

Lab249.java

class Lab249 {

public static void main(String[] args) {
int arr[] = new int[1];
System.out.println("LEN: " + arr.length);
System.out.println(arr[0]);
}
}

Lab250.java

class Lab250 {

public static void main(String[] args) {
int arr[] = new int[0];
System.out.println("LEN: " + arr.length);
System.out.println(arr[0]);
}
}

Lab251.java

class Lab251 {

public static void main(String[] args) {
int arr[] = null;
System.out.println("LEN: " + arr.length);
}
}

Lab252.java

class Lab252 {

public static void main(String[] args) {
int arr[] = null;
System.out.println(arr[0]);
}
}

Lab253.java

class Lab253 {

public static void main(String[] args) {
int arr[] = null;
System.out.println(arr[-1]);
}
}

Lab254.java

class Lab254 {

public static void main(String[] args) {
int arr[] = new int[3];
System.out.println(arr.length);
arr.length = 30;
System.out.println(arr.length);
}
}

Lab255.java

class Lab255 {

public static void main(String[] args) {
int arr[] = new int[3];
arr[0] = 23;
arr[1] = 65;
arr[2] = 98;

int arr2[] = arr;

System.out.println("Len :" + arr.length);
System.out.println("Len :" + arr2.length);

arr = new int[5];

System.out.println("Len :" + arr.length);

for (int i = 0; i < arr.length; i++) System.out.println(arr[i]);

System.out.println("Len :" + arr2.length);

for (int i = 0; i < arr2.length; i++) System.out.println(arr2[i]);
}
}

Lab256.java

class Lab256 {

public static void main(String[] args) {
final int arr[] = new int[2];

System.out.println("Len :" + arr.length);
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println();

arr[0] = 99;
arr[1] = 88;

System.out.println(arr[0]);
System.out.println(arr[1]);
}
}

💡 Quick Understanding

  • new int[0] → valid, but accessing arr[0] → Exception
  • arr = null → accessing anything → NullPointerException
  • arr.lengthread-only, cannot modify
  • Reference copy (arr2 = arr) → both point to same array
  • final array → reference cannot change, but values can

Lab257.java

class Lab257 {

public static void main(String[] args) {
final int arr[] = new int[2];
arr = new int[4];
arr = null;
}
}

Lab258.java

class Lab258 {

public static void main(String[] args) {
byte brr[] = new byte[3];
int arr[] = brr;
System.out.println("Hello Guys");
}
}

Lab259.java

class Lab259 {

public static void main(String[] args) {
int arr[] = new int[5];
System.out.println(arr + 1);
}
}

Lab260.java

class Lab260 {

public static void main(String[] args) {
char ch[] = new char[3];
ch[0] = 'J';
ch[1] = 'L';
ch[2] = 'C';
System.out.println(ch);
}
}

Lab261.java

class Lab261 {

public static void main(String[] args) {
int arr[] = new int[3];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
System.out.println(arr);
}
}

Lab262.java

class Lab262 {

static int arr[];

public static void main(String[] args) {
System.out.println(arr);
}
}

Lab263.java

class Lab263 {

static char arr[];

public static void main(String[] args) {
System.out.println(arr);
}
}

Array Declaration, Construction and Initialization

✔ You can declare, construct and initialize an array in one statement also.

Syntax

<dataType><refVarName>[]= {<val1>,<val2>,<val3>, ...,<valN>};

Examples

int arr[] = { 10, 20, 30 };

String names[] = { "Java", "Manish", "DK" };

💡 Quick Understanding

  • final array → reference cannot change (arr = new int[4] → error)
  • Array type mismatch (byte[]int[]) not allowed
  • arr + 1 → invalid operation
  • System.out.println(char[]) → prints characters ✔
  • System.out.println(int[]) → prints reference ✔
  • Static arrays → default value is null

Lab264.java

class Lab264 {

public static void main(String[] args) {
int arr[] = { 10, 20, 30 };
System.out.println("Len :" + arr.length);

for (int i = 0; i < arr.length; i++) System.out.println(arr[i]);

String names[] = { "Java", "Manish", "DK" };
System.out.println("Len :" + names.length);

for (int i = 0; i < names.length; i++) System.out.println(names[i]);
}
}

Lab265.java

class Lab265 {

public static void main(String[] args) {
int arr[] = {};
System.out.println("Len :" + arr.length);

for (int i = 0; i < arr.length; i++) System.out.println(arr[i]);
}
}

Anonymous Arrays

✔ When you create an array without specifying the name, then it is called an Anonymous Array.

Syntax

new <dataType>[] {<val1>,<val2>,<val3>, ...,<valN>};

Examples

new int[]{10,20,30};
new String[]{"Java","World","Balan"};

✔ Anonymous arrays usually can be used with method calls.

Lab266.java

class Lab266{
public static void main(String[] args){
int arr[]=null;
arr={99,88,66,77};
System.out.println("Len :"+arr.length);
}
}


Lab267.java

class Lab267 {

public static void main(String[] args) {
int arr[] = null;
arr = new int[] { 99, 88, 66, 77 };

for (int i = 0; i < arr.length; i++) System.out.println(arr[i]);
}
}

Lab268.java

class Lab268 {

public static void main(String[] args) {
show(new int[] { 99, 88, 66, 77 });
}

static void show(int arr[]) {
for (int i = 0; i < arr.length; i++) System.out.println(arr[i]);
}
}

Lab269.java

class Lab269{
public static void main(String[] args){
show(new int[4]{99,88,66,77});
}

static void show(int arr[]){
for(int i = 0;i<arr.length;i++)
System.out.println(arr[i]);
}
}


💡 Quick Understanding

  • {99,88,66,77} cannot be assigned directly after declaration
  • Must use 👉 new int[]{...}
  • Anonymous arrays are mostly used in method calls
  • new int[4]{...} invalid syntax