Lewis D.
—You want to create a new array data structure in Java, but you’re not sure how to go about it.
In Java, the array object is used to store multiple elements of the same type. These elements are stored in contiguous memory, the length of which is immutable once initialization is complete.
We can declare an array in the same way as any other variable or object in Java, by giving a type and a name:
int[] myArray;
However, this merely declares the array, it does not initialize it. The array has the value null
.
Let’s take a look at the two ways we can initialize our array.
If we already know the element values we want stored in the array, we can initialize the array like this:
myArray = new int[]{0, 1, 2, 3};
Java allows a shorthand of this by combining declaration and initialization:
int[] myArray = {0, 1, 2, 3};
This example is functionally equivalent to the previous example that declared and initialized our array separately. Interestingly, the square brackets that symbolize an array []
can be put on either side of the array name during the declaration with no change in functionality:
int myArray[] = {0, 1, 2, 3};
Next, if you do not know the exact data elements you want in your array when you initialize it, you can instead provide the length of the array, and it will be populated with default values based on the array type:
int myArray[] = new int[4];
After the initialization, the array will contain the following elements.
[0, 0, 0, 0]
Each data type has a default value as follows:
Data Type | Default Values |
---|---|
Byte | 0 |
Short | 0 |
Int | 0 |
Long | 0 |
Float | 0.0 |
Double | 0.0 |
Boolean | false |
Char | ‘\u0000′ which is the Unicode for null |
Object | null |
Remember that, once the array is initialized, the length and type of the array is immutable. However, we can still make changes to the element values contained within the array.
If you’re looking to get a deeper understanding of how Java application monitoring works, take a look at the following articles:
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.