The BASE for JAVA LEARNING

by PRO FE S S OR


SECTION VI - Arrays and the String class

Step 20: Arrays and their Initialization


  • An Array is a group of variables of the same type.


  • The first element of an array is the zero element, and the last element is the n-1 element, where n is the number of elements.


  • Arryas are considered Objects.


  • We can make arrays of primitive data types as well as of non-primitive data types.


  • The symbol used to represent an array is the straight or square bracket [ ].


  • An array can be declared by having the [ ] square brackets following the data type, or by having them following the array variable.

    Examples: (declaration of an array of int numbers)

    int [ ] numbers ;
    or int numbers[ ] ;

  • An array can be initialized in 2 ways:

  • By enclosing the initialization values within curly braces { ... , ... , ... }

  • By the 'new' operator.


  • When using the curly braces for initialization, it has to be done at the same time (same line) that the array is being declared.
    Example of initialization by using curly braces:

    int [ ] numbers = { 2, 5, 6, 123, 100 } ;

    If using the new operator, the number of elements has to be stated.
    Example of initialization by using the 'new' operator:

    int[ ] numbers = new int[5]; // 5 element array

    or in 2 different lines of code
    int[ ] numbers;
    numbers = new int[5];

  • When using 'new' to initialize an array, all elements of the array are set to null values.


  • To access an element of an array, the square bracket is used.
    Example:

    year[0]; // this is the first element of the 'year' array.
    year[4]; // this is the fifth element of the 'year' array.

  • The 'length' property yields the number of elements of an Array. It's used by writing the array variable name followed by a dot, followed by 'length'.
    See below:

    int[] month = new int[12];               // an array of integers of 12 elements is created.
    System.out.println(month.length); // number 12 will be displayed.



  • Click here to go to the JAVA Mini-Course page


    The BASE for JAVA LEARNING

    by PRO FE S S OR


    SECTION VI - Arrays and the String class

    Step 21: Multi-dimensional Arrays


    Multi-dimensional Arrays



    Multi-dimensional arrays are also supported in JAVA.

    See below, an example of 2 two-dimensional Arrays initialized, one using the curly braces, and the other one using the 'new' operator.

    int[][] boxA = { {2, 5}, { 7, 11 }, { 1, 21 } } ; // 3 columns, 2 rows each
    System.out.println(boxA[2][1]); // this displays number 21
    System.out.println(boxA[1][0]); // this displays number 7
    System.out.println(boxA[0][0]); // this displays number 2

    // If we want to initialize every element to zero in above array, we could have used the 'new' operator
    int[][] boxB = new int[3][2];
    boxB[2][1] = 21; // Value 21 assigned to element of the 3rd column, 2nd row.
    boxB[1][0] = 7 ; // Value 7 assigned to element of the 2nd column, 1st row.
    boxB[0][0] = 2 ; // Value 2 assigned to element of the 1st column, 1st row.


    Click here to go to the JAVA Mini-Course page


    The BASE for JAVA LEARNING

    by PRO FE S S OR


    SECTION VI - Arrays and the String class

    Step 22: The String class


    The String class

    In JAVA, any Strings can be formulated as Objects, since there is the String class.

    Strings are immutable. That means that after one String is created it cannot be modified, and it remains in memory. It can only be removed by the garbage collector method (to be discussed later).

    To create a String, we can do it by use of the double quotes or by the use of the 'new' operator.

    See Example below (instantiating the String class): class Exercise
    {
    public static void main(String[] args)
    {
    String name = "Professor V";
    // String name = new String("Professor V"); // this is another way of creating a String.
    System.out.println("My name is " + name + "."); // displays "My name is Professor V."
    }
    }

    Example concatenating a String: class Exercise
    {
    public static void main(String[] args)
    {
    String firstName = "John";
    String lastName = "Smith";
    String fullName = firstName + lastName;
    System.out.println("My full name is: " + fullName + "."); // displays "My full name is John Smith."
    }
    }


    By virtue of its immutability, any change or concatenation made to a String, it forces the creation of another String with that change or concatenation.

    Example concatenating a String using same String name: class Exercise
    {
    public static void main(String[] args)
    {
    String name = "John";
    name = "John" + " Smith";
    // By virtue of its immutability, now we have two Strings in Memory,
    // one containing "John", and the other one containing "John Smith".
    System.out.println("My name is " + name + "."); // displays "My name is John Smith."
    }
    }

    There is a difference when creating a String by using double quotes and by using the 'new' operator.

    The following Example illustrates that difference:

    class Exercise
    {
    public static void main(String[] args)
    {
    String name1 = "abc";
    String name2 = "abc";
    // Above we have just created only one area in memory containing "abc".
    // That "abc" is being pointed by the both name1 and name2.
    // This is so, because JAVA goes through the Strings pool to search
    // for any String that contains the same characters, and if found
    // it makes the String points to the same Memory area.

    // Now, if we create a String by using 'new' operator,
    // something different happens. See below:

    String name3 = "xyz";
    String name4 = new String("xyz");
    // By virtue of using the 'new' operator upon one of the Strings above,
    // we have just created two areas of memory containing "xyz".
    // The Strings name3 and name4 each points ot its own "xyz".
    // By using 'new', JAVA does not search the Strings pool.
    }
    }

    The Immutability of Strings makes the program execution faster.

    Click here to go to the JAVA Mini-Course page


    The BASE for JAVA LEARNING

    by PRO FE S S OR


    SECTION VI - Arrays and the String class

    Step 23: The StringBuffer class


    The StringBuffer class


    As an alternative to the String class, there is the StringBuffer class. The main difference between those two classes is the fact that the StringBuffer class is NOT immutable. That is, changes can be made to any StringBuffer Object and it directly affects its Memory contents.

    StringBuffer Objects can only be created as regular Objects, that is by using the 'new' operator.

    StringBuffer class has many methods that can use to manipulate any Object of its kind.

    In the example below, we will be using adding a character to the StringBuffer Object at the third position. This is done by the use of the insert() method. Then after that we will use toString() method to convert the StringBuffer Object into a String Object.

    Example: class Exercise
    {
    public static void main(String[] args)
    {
    StringBuffer name = new StringBuffer("Mathews");

    // Let's use now the insert() method to add the character 't'
    // at the 3rd position, so "Mathews" becomes "Matthews".

    name = name.insert(2, 't'); // character 't' is inserted at the 3rd position.
    System.out.println(name); // displays "Matthews" the StringBuffer Object

    // Let's now create a String Object with the same contents of this String Buffer Object.

    String name2 = name.toString(); // name2 now contains "Matthews"
    System.out.println(name2); // displays "Matthews" the String Object
    }
    }


    Click here to go to the JAVA Mini-Course page