answersLogoWhite

0

Arrays are objects in Java that store multiple variables of the same type. Arrays can hold either primitives or object references, but the array itself will always be an object on the heap, even if the array is declared to hold primitive elements. In other words, there is no such thing as a primitive array, but you can make an array of primitives.

Declaring an Array

Arrays are declared by stating the type of element the array will hold, which can be an object or a primitive, followed by square brackets to the left or right of the identifier.

Declaring an array of primitives:

int[] Values; // brackets before name (recommended)

int Values []; // brackets after name (legal but less readable)

// spaces between the name and [] legal, but bad

Declaring an array of object references:

Ferrari[] Ferraris; // Recommended

Ferrari Ferraris[]; // Legal but less readable

When declaring an array reference, you should always put the array brackets immediately after the declared type, rather than after the identifier (variable name). That way, anyone reading the code can easily tell that, for example, Values is a reference to an int array object, and not an int primitive.

We can also declare multidimensional arrays, which are in fact arrays of arrays. This can be done in the following manner:

String[][][] occupantName; // recommended

String[] ManagerName []; // Real bad coding practice, but legal

The first example is a three-dimensional array (an array of arrays of arrays) and the second is a two-dimensional array. Notice in the second example we have one square bracket before the variable name and one after. This is perfectly legal to the compiler, but if you write code like this, anyone who is going to manage your code in future is sure going to curse you badly.

It is never legal to include the size of the array in your declaration. Yes, before you ask me, let me admit that you can do that in some other languages, which is exactly why you might see a question or two that include code similar to the following:

int[5] races;

The preceding code won't make it past the Java compiler. Remember, the JVM doesn't allocate space until you actually instantiate the array object. That's when size matters and not when you declare the array variable.

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What does it mean to have a string split in java?

To have a string split in Java means that a string array, containing substrings (can be delimited by elements of a specified string or Unicode character array), is returned.


What happens if an array overrun occurs in java?

If you mean that you try to access an index outside of the bounds of the array, then it will result in an IndexOutOfBoundsException being thrown.


Can ragged arrays created by using java?

I assume you mean that you have a number of rows, and that not all rows have the same number of "cells". Yes, in Java a two-dimensional array is implemented as an array of arrays (each item in the top-level array is, in itself, an array); a 3-dimensional array is an array of arrays of arrays, etc.; and there is no rule stating that all secondary (etc.) arrays must have the same number of elements.


What does long term mean?

It means explain it or use details


What does long responses mean?

It means explain it or use details


What does long term response mean?

It means explain it or use details


What is subroutine in java?

Subroutine mean what (in java)?


What does Systemout mean in java?

System.out refers to the Java console.


What do you mean by multithread program in java?

A Program in Java that spawns multiple threads is called a multithreaded program in Java.


What does the word java mean?

Java is an island in Indonesia, but in slang it means coffee.


How do you find the HCF of all the numbers in an array java?

I guess you mean the highest common factor. Use a loop. Start assuming that the hcf is the first number in the array. Call this "result". Find the hcf of "result" and the second element, assign this to "result". Find the hcf of "result" and the third element, and copy this back to result, etc.


What does the term Java logging mean?

Java logging is data logging for the Java platform. Logging is a term in software for recording activity. Therefore Java logging is recording activity for Java.