bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Java/Java Tutorial
Java•Java Tutorial

Java Arrays

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To declare an array, define the variable type with square brackets [ ] :

String[] cars;

We have now declared a variable that holds an array of strings. To insert values to it, you can place the values in a comma-separated list, inside curly braces { } :

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

To create an array of integers, you could write:

int[] myNum = {10, 20, 30, 40};

Access the Elements of an Array

You can access an array element by referring to the index number.

This statement accesses the value of the first element in cars:

Example

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
// Outputs Volvo

Note

Array indexes start with 0: [0] is the first element. [1] is the second element, etc.

Think of an array as numbered boxes, where each box stores an element:

IndexElement
0Volvo
1BMW
2Ford
3Mazda

Change an Array Element

To change the value of a specific element, refer to the index number:

cars[0] = "Opel";

Example

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
// Now outputs Opel instead of Volvo

Array Length

To find out how many elements an array has, use the length property:

Example

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
// Outputs 4

The new Keyword

You can also create an array by specifying its size with new . This makes an empty array with space for a fixed number of elements, which you can fill later:

Example

String[] cars = new String[4]; // size is 4
cars[0] = "Volvo";
cars[1] = "BMW";
cars[2] = "Ford";
cars[3] = "Mazda";
System.out.println(cars[0]); // Outputs Volvo

However, if you already know the values, you don't need to write new . Both of these create the same array:

Example

// With new String[] cars = new String[] {"Volvo", "BMW", "Ford", "Mazda"};
// Shortcut (most common) String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

Note

You cannot write new String[ 4 ] {"Volvo", "BMW", "Ford", "Mazda"} .

In Java, when using new , you either:

  • Use new String[4] to create an empty array with 4 slots, and then fill them later
  • Or use new String[] {"Volvo", "BMW", "Ford", "Mazda"} (without specifying the number of elements) to create the array and assign values at the same time

Tip

The shortcut syntax is most often used when the values are known at the start. Use new with a size when you want to create an empty array and fill it later.

Previous

Java Break and Continue

Next

Java Arrays Loop

This chapter

Overview
49

Lessons

110m

Read time

1. Java Tutorial2. Java Introduction3. Java Getting Started4. Java Syntax5. Java Statements6. Java Output / Print7. Java Output Numbers8. Java Comments9. Java Variables10. Java Print Variables11. Java Declare Multiple Variables12. Java Identifiers13. Java Constants (final)14. Java Data Types15. Java Numbers16. Java Boolean Data Types17. Java Characters18. Java Data Types Example19. Java Non-Primitive Data Types20. Java var Keyword21. Java Type Casting22. Java Operators23. Java Arithmetic Operators24. Java Assignment Operators25. Java Comparison Operators26. Java Logical Operators27. Java Operator Precedence28. Java Strings29. Java String Concatenation30. Java Numbers and Strings31. Java Special Characters32. Java Math33. Java Booleans34. Java If ... Else35. Java Else36. Java Else If37. Java Short Hand If...Else (Ternary Operator)38. Java Nested If39. Java Logical Operators in Conditions40. Java Switch41. Java While Loop42. Java Do/While Loop43. Java For Loop44. Java Nested Loops45. Java For Each Loop46. Java Break and Continue47. Java Arrays48. Java Arrays Loop49. Java Multi-Dimensional Arrays

On this page

Access the Elements of an ArrayChange an Array ElementArray LengthThe new Keyword