Creating & Using ArraysJ8 Home « Creating & Using Arrays

In this lesson we look at all types of class declaration and extension and how to use interfaces and the package and import statements. We examine the declaration, initialization and usage of primitives, arrays, enums, and objects as static, instance, local variables and their respective scopes. After this we cover correct use of overriding and overloading of methods as well as identifying legal return values for methods. we investigate constructors to determine if a default constructor will be created, and if so, determine the behaviour of that constructor. We finish of this lesson by studying class instantiation and how to instantiate nested and non-nested classes.

Lets take a look at the points outlined at Oracle Java SE 8 Programmer I for this part of the certification.

  • Creating and Using Arrays
    1. Declare, instantiate, initialize and use a one-dimensional array.
    2. Declare, instantiate, initialize and use multi-dimensional arrays.

ArraysTop

An array in Java is an object that contains a collection of values which can be a collection of primitive data types or a collection of reference variable types.

  • An array of primitives data types is a collection of values that constitute the primitive values themselves.
  • An array of reference variable types is actually a collection of pointer values which point to the memory address where each object is stored on The Heap.

Whichever variable type the array holds, primitive or reference, the array itself is still an object. In Java you can define a one dimensional array which is an object that refers to a collection of values repeated one or more times, or multi dimensional arrays which are a collection of array objects (arrays of arrays). Multi dimensional arrays may contain the same number of elements in each row or column and are known as regular arrays or an uneven number of elements in each row or column which are known as irregular arrays.

Array Creation

Array creation is a three step process as outlined below and can be achieved using separate statements or combined into a single statement.

  1. Array declaration
  2. Array allocation
  3. Array element initialization

Array Notes

As you can see from the table above there is a lot of ways to create arrays in Java. There are several points about arrays shown in the table above that we will highlight again here before we go into some code examples:

  • Array indexes are zero-based.
  • When declaring an array the square brackets can appear after the type, after the array name or in the case of multi dimensional arrays a combination of both.
  • After allocation of an array, each element of the array is initialized with the default for the array type:
    • object - null
    • boolean - false
    • char - /u0000
    • integer types (byte, short, int and long) - 0
    • floating-point types (float, double) - 0.0
  • For multiple statement array creation the new keyword is mandatory.
  • For single statement array creation the size of the array is calculated by the number of values that are assigned to the array and should not be specfied.

Related Java Tutorials

Fundamentals - Primitive Variables
Fundamentals - Method Scope
Objects & Classes - Arrays
Objects & Classes - Class Structure and Syntax
Objects & Classes - Reference Variables
Objects & Classes - Reference Variables - The new Operator
Objects & Classes - Methods - Overloaded Methods
Objects & Classes - Methods - Overloaded varargs Ambiguities
Objects & Classes - Instance Variables & Scope
Objects & Classes - Constructors
Objects & Classes - Enumerations
OO Concepts - Abstraction - Abstract Classes
OO Concepts - Inheritance - Overriding Methods
OO Concepts - Inheritance Concepts - Accessing Superclass Overrides
Inheritance Concepts -Superclass Constructors
OO Concepts - Interfaces
OO Concepts - Nested Classes
Flow Control - Methods - Overridden Methods & Exceptions
API Contents - Packages
API Contents - Packages - Imports
API Contents - Packages - Static Imports