Sunday, 8 June 2025

Difference Between Array and ArrayList in Java

 Both Array and ArrayList are used to store multiple values in Java — but they work a little differently.

Let’s look at what makes them different, and when to use each one.


Array

  • Fixed size (you can’t change it once created)

  • Can store primitive types like int, double, etc.

  • Syntax:

    int[] numbers = new int[5];

    numbers[0] = 10;


    ArrayList

  • Can grow and shrink in size

  • Only works with objects, not primitives (use Integer instead of int)

  • Comes from java.util.ArrayList

  • Syntax:

    import java.util.ArrayList;

    ArrayList<Integer> numbers = new ArrayList<>();

    numbers.add(10);

No comments:

Post a Comment

🚀 Top 3 Java Projects Every Beginner Should Build (With Code Ideas)

 Tired of just printing "Hello World"? It’s time to build something real. 💻 If you're learning Java and want to stand out, ...