by
PRO
FE S
S OR

SECTION VI - Arrays and the String class
Step 20: Arrays and their Initialization
Examples: (declaration of an array of int numbers)
Example of initialization by using curly braces:
Example of initialization by using the 'new' operator:
numbers = new int[5];
Example:
year[4]; // this is the fifth element of the 'year' array.
See below:
System.out.println(month.length); // number 12 will be displayed.
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.
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.
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):
{
{
// 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:
{
{
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:
{
{
name = "John" + " Smith";
// one containing "John", and the other one containing "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:
{
{
String name2 = "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");
// 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.
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:
{
{
// 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