How to Initialize an Array in Java?9 May 2025 | 2 min read Java initialize array is basically a term used for initializing an array in Java. We know that an array is a collection of similar types of data. The array is a very important data structure used for solving programming problems. The word element is used for the values stored in different positions of the array. In order to use the Array data structure in our code, we first declare it, and after that, we initialize it. Declaration of an ArrayThe syntax of declaring an array in Java is given below. Here, the datatype is the type of element that will be stored in the array, square bracket[] is for the size of the array, and arrayName is the name of the array. Initializing an ArrayOnly the declaration of the array is not sufficient. In order to store values in the array, it is required to initialize it after declaration. The syntax of initializing an array is given below. In Java, there is more than one way of initializing an array which is as follows: 1. Without assigning values In this way, we pass the size to the square braces[], and the default value of each element present in the array is 0. Let's take an example and understand how we initialize an array without assigning values. ArrayExample1.java Output: ![]() 2. After the declaration of the array In this way, we initialize the array after the declaration of it. We use the new keyword assigning an array to a declared variable. Let's take an example and understand how we initialize an array after declaration. ArrayExample2.java Output: ![]() 3. Initialize and assign values togetherIn this way, we declare and initialize the array together. We don't do both the declaration and initialization separately. Let's take an example and understand how we do both the thing together: ArrayExample3.java Output: ![]() All the above three ways are used based on the requirement of the functionality. |
In Java, threads are independent units of execution that can run concurrently, allowing for improved processing speed by utilizing multiple CPU cores. Thread safety in Java refers to the property of a program or code that can handle and execute correctly regardless of the number of...
4 min read
Finding the greatest number of values that may be chosen from the array so that the total number of 1s in their binary representation is at most K is the task when an array of integers nums[] and a positive integer K is given. Example 1: Input: int...
3 min read
In the realm of number theory, Kaprekar numbers hold a special place due to their intriguing properties. Named after the Indian mathematician D. R. Kaprekar, these numbers possess the unique characteristic that they can be split into two parts, whose squares can be added to yield...
5 min read
In this section, we will discuss the vertical order traversal of a binary tree in Java and the different approaches to achieve it. In the vertical order traversal, we print the node of the binary tree vertically, i.e., from top to bottom. For example, consider the...
8 min read
The Adapter design pattern in Java is a way to make two objects with different interfaces work together. Sometimes, we have objects that we want to use together, but their interfaces are not compatible. In such cases, we can use the Adapter pattern. The adapter pattern acts...
4 min read
Fonts are a fundamental aspect of any graphical user interface, and Java provides robust support for handling and displaying fonts. Whether we develop a desktop application using Swing or a web application with JavaFX, understanding how to work with fonts is essential for creating visually...
9 min read
A palindrome linked list is a linked list in which the sequence of its elements reads the same forwards and backwards. To determine if a linked list is a palindrome, we need to compare the first half of the list with the reversed second half while...
12 min read
In this section, we will learn what is Peterson number and how can we check whether a given number is Peterson or not through a Java program. Peterson Number A number is said to be Peterson if the sum of factorials of each digit is equal to the...
2 min read
In this article on Java programming Language, we are going to see an elaborate explanation of the term " UTF " and its conversion. We are going to learn the different forms of Programming Language, their uses, and their nature while coding. What is a Unicode? Unicode...
4 min read
Difference Between List and Set in Java In JDK 2.0, we used to use Vectors, Arrays, and Hashtable to group the objects into a single unit. In JDK 8, Collection framework come in to existence that provides several interfaces to work with a collection of data. List...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India