Javatpoint Logo
Javatpoint Logo

Array Declaration in Java

In Java, an array is a data structure that allows you to store multiple values of the same type in a single variable. It provides a convenient way to work with collections of data.

The Arrays.toString() and Arrays.deepToString() methods are then used to produce the values of the arrays. For one-dimensional arrays, use the Arrays.toString() function; for multidimensional arrays, use Arrays.deepToString(). The values may be shown more easily thanks to these techniques, which transform the array items into a string representation.

You can use arrays to store and modify data collections if you have a solid knowledge of Java's array declaration syntax.

The article explains how to declare arrays in Java with examples and comments.

Declaration and Initialization of an Array:

In this type of declaration, the array is created and initialized with specific values in a single step.

Declaration of an Array:

The array is constructed with a defined length in this sort of declaration, but the individual items are not initialised.

Declaration and Initialization of a Multidimensional Array:

A multidimensional array having several dimensions, such a 2D or 3D array, is created by this kind of statement. The array's elements can each hold multiple values.

Declaration of an Array with a Specific Size:

The array is constructed with a certain length with this type of declaration, and the components are initialised with default values (such as 0 for numeric types and null for reference types).

Declaration of an Empty Array:

With this kind of declaration, a zero-sized array is created. It may come in handy when you want an empty array with no elements, depending on the situation.

These examples demonstrate different ways of declaring and initializing arrays in Java. You can use these concepts to create and manipulate arrays based on your specific requirements.

Filename: ArrayDeclaration.java

Output:

Numbers:
1 2 3 4 5 
Fruits:
Apple Banana Orange 
Vowels:
     
Matrix:
1 2 3 4 
5 6 7 8 
9 10 11 12 
Names:
John Doe 
Alice Smith 
Bob Johnson 

Mike Brown 
Emily Davis 
Chris Wilson 

Temperatures:
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 
Empty Array Length: 0






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA