how to create an array of int in java

How to Create an Array of Ints in Java

By Daniel King
AD

In Java, an array of integers is a fundamental data structure that allows you to store multiple integer values under a single name. It's a necessity when you need to handle a collection of numbers, and knowing how to create one is a crucial skill. Let's delve into three common ways to create an array of integers in Java.

Before we start, ensure you have imported the necessary Java library. For integer arrays, the int data type from the java.lang package is required. Here's how you can import it:

Declaring and Initializing an Integer Array

An array in Java must be declared and initialized before you can use it. The general syntax for declaring an array is dataType[] variableName;. For an integer array, the dataType would be int.

AD
Arrays in Java 💻 | Complete Java Array Guide
Arrays in Java 💻 | Complete Java Array Guide
Java Arrays Explained | 1D & 2D Arrays, Indexing & Examples | Day 17
Java Arrays Explained | 1D & 2D Arrays, Indexing & Examples | Day 17
How to create a String or int Array in Java? Example Tutorial
How to create a String or int Array in Java? Example Tutorial

Initialization, on the other hand, involves actually creating the array and assigning values to it. Here's how you can do that:

Declaration and Initialization in One Line

You can declare and initialize an integer array in a single line of code. Here's the syntax:

Java Arrays Tutorial: Declare, Create and Initialize
Java Arrays Tutorial: Declare, Create and Initialize
Java 2D Arrays Explained | Rows, Columns, Matrix & Examples | Day 18
Java 2D Arrays Explained | Rows, Columns, Matrix & Examples | Day 18
Student Marks Calculator Using Array in Java 📊 | Java Program
Student Marks Calculator Using Array in Java 📊 | Java Program

int[] arrayName = new int[arraySize];

Where arrayName is the name of your array, and arraySize is the number of elements you want in your array. The number inside the square brackets [] determines the size of the array.

AD

Initialization with Values

Instead of initializing the array with default integer values (zero), you can populate it with a set of values at the time of creation. Here's how you can do that:

What are Arrays in Java?
What are Arrays in Java?
an image of a computer screen with the words and numbers on it, including two arrows
an image of a computer screen with the words and numbers on it, including two arrows
☕ Java Arrays Made Easy | Complete Doodle Cheat Sheet for Beginners
☕ Java Arrays Made Easy | Complete Doodle Cheat Sheet for Beginners
how to print a specific element in array using java
how to print a specific element in array using java
Java Arrays Cheat Sheet (2026) | Complete Java Arrays Guide for Beginners & Students |
Java Arrays Cheat Sheet (2026) | Complete Java Arrays Guide for Beginners & Students |
an open book with instructions on how to use the array array in windows and linux
an open book with instructions on how to use the array array in windows and linux

int[] arrayName = {value1, value2, value3, ...};

In this case, replace value1, value2, value3, ... with the integer values you want to store in the array.

AD

Creating an Empty Integer Array

There might be times when you want to create an array without initializing it with values. To do this, you just need to declare and create the array without providing any initial values. Here's how:

Java Multithreading Explained | Threads, Runnable & Thread Methods | Day 34
Java Multithreading Explained | Threads, Runnable & Thread Methods | Day 34
an array sheet with the names and numbers for different types of programming languages on it
an array sheet with the names and numbers for different types of programming languages on it
Java Arrays Explained 📦 | 1D & 2D Arrays
Java Arrays Explained 📦 | 1D & 2D Arrays

Declaration and Creation, No Initialization

Declaration: int[] arrayName;

Creation: arrayName = new int[arraySize];

AD

The size of the array in this case is arraySize. After creating the array, you can add elements to it using the index notation arrayName[index] = value;.

Creation and Initialization with Size

Alternatively, you can create and initialize the array with a specified size, but without providing any initial values. Here's how:

int[] arrayName = new int[arraySize];

This creates an array with arraySize number of elements, but these elements are initialized with the default integer value (zero).

Thus, you can create arrays in Java with different approaches, depending on your requirements. Practice these methods to perfect your use of integer arrays, a crucial part of Java programming.

And remember, always close your code execution with a forward-looking thought. Is there any other type of array you wish to create in Java? The possibilities are vast, so keep exploring and expanding your knowledge base.

AD