Categories: Uncategorized

Learn Java – How to Declare an Array?

An array in Java is an object. However, this object receives special treatment of the language itself and JVM (as with String).

Declaring and Initializing Arrays in Java

The array object gathers together other objects that share a common supertype and to which we can refer by an index (an integer number). Arrays in Java are immutable. This means that there is no way to modify the array size – to accommodate more or fewer elements – after it was created. If the array size in use is not enough, we have to create another array and make a copy from the former, element by element, to the new.

All array elements share a common type. This type characterizes the type of the array.  The array object does not have an associated class or rather have several. As a type A, which can be any Java type (class, interface, enum, etc), an array A is written as A []. The brackets with nothing inside them represents that this type is an array.

//A type of the array variable
A [] variable

The array can also be created using primitive types. The way of declaring it is similar the previous one. The Java syntax also supports the array symbol (“[]”) may be placed in the variable name:

// Array of integers with 5 positions.
int [] integers = new int [5];
// Can be declared also as
 int integers [] = new int [5]

This second form is traditional in other languages, and though possible in Java, is not recommended. Code writing good practices suggests the use of the first form of declaration instead of the second and is standard practice. This happens because int [] represent a different class of int objects. Incidentally, in this case, int isn’t an object, while an array is always an object.

In Java, any variable is composed of two phases: the declaration and initialization. It’s no different for the array. In the declaration we say what type the array is and what is its size. The array size must be explicitly informed since, in Java, this size will never change. Here are some examples:

// Array of integers with 5 positions.
int [] integers = new int [5];
// String array with 8 positions.
String [] strings = new int [8];
// Product array with 20 positions.
Product [] products = new product [20];

With arrays, we still have another step. When initializing the array, the positions are automatically initialized by the JVM. For primitive numeric types, the numbers 0 is used, for Boolean, false, and object elements are initialized with null. Usually, these are not the values that we want in the array. So, for arrays have an additional step where we will make the allocation of values. We can do this explicitly or by using instructions for or while.

// Array of even numbers with 5 positions.
int [] evennumbers = new int [5];
// Assigning manually
evennumbers [0] = 0;
evennumbers [1] = 2;
evennumbers [2] = 4;
evennumbers [3] = 6;
evennumbers [4] = 8;
// Assigning in a cycle.
for (int i = 0; i <evennumbers.length; i   ) {
    evennumbers [i] = i * 2;
}

Note that to be able to use the assignment with for we use the length property of the array. This property contains the array number of positions (for example, 5). Also, note that we use a variable to access the position of the array. This is the most straightforward and useful use of the array since any position may be referred to as a variable. However, this variable can only be of type int.

The manual assignment can be a quite large and heavy task to do, but there is another way to assign values to an array:

int [] evennumbers = new int [] {0,2,4,6,8};

Using curvy brackets, we can write the values for each position in the array. Note that, in this example, we don´t need to declare the size of the array, which must be equal to the numbers written between the curvy brackets elements.

If you want to explore more about Java, visit our videos section! Below are some examples:

You can also follow some of the broadcasters who program in Java, as below:

chase1263070

CallumC

Another cool way to find out interesting things about jQuery is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

2 weeks ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

1 month ago

Highest Stable Coin Yields – (W12 – 2024)

It’s another week and like always we have to explore the top yield platforms for…

1 month ago

Binance Auto Invest – the Best Innovation in Crypto since Sliced Bread

At a time where we’re constantly seeking tools and strategies to simplify our crypto investments,…

1 month ago

Highest Stable Coin Yields – March 2024

As we kick off another week, it's time to explore the top yield platforms for…

2 months ago

Education Ecosystem Featured on Business Insider

We're excited to share that Education Ecosystem was recently featured in an article on Business…

2 months ago