Tutorial 3: Basics: Char, String, Array


Overview on this Tutorial:

1. Character and String (CharTest.java)

2. One Dimensional Array (ArrayTest.java)

3. Multi-dimensional Array (ArrayTest2.java)


Character and String (CharTest.java):

1. "char"is another primitive type, like "int" and "boolean".

2. You can declare a "char" variable and assign value to it.

// Single quota for character
char a = 'A';

3. A "char" variable can only be used to store one single character.

4. There are some special characters in Java:

Special Character Meaning
' ' Space
'\n' End of line
'\t' Tab

5. To store a "string of characters", we have to use "String".

// Double quota for string
String h = "Hello ";

Practice: You can try to print out "String" of Special Characters with Normal Characters.

6. "String" actually is not a primitive type, but it is a class. The detail of class will be discuss later.

7. Apart form 'int' addition, '+' is also used for String concatenation.

String x = "abc" + "def";

8. If one side in the operator '+' is not a String, it will automatically convert to String. (Of course, another side must be a String).

String y = "abc" + 123;
String z = "abc" + 'd';

9. The function "String.valueOf()" can be used to convert another type (such as "int" and "boolean") to String.

10. The argument for the function "System.out.println()" actually needs to be a String.

int x = 123;
String h3 = String.valueOf(x);

// We cannot have: System.out.println(x);
System.out.println(h3);

Practice: Open the MIDP help page, try access the page for the class "String".

[Method: Start-> All Programs -> J2ME Wireless Toolkit 2.2 -> Documentation -> MIDP 2.0 (JSR 118)]

 


One Dimensional Array (ArrayTest.java):

1. In mathematics, instead of using "x", "y", "z", we can define a set of variables as "x1", "x2", "x3".

2. In programming, we can do similar thing by using Array.

3. To declare an array:

int intArray1[ ];
boolean [ ] boolArray;

4. To allocate space for the array:

intArray1 = new int[3];
boolArray = new boolean[3];

5. Note that 3 is the size of the array. There are 3 elements in intArray1: intArray[0], intArray[1], intArray[2].

6. Therefore the index of an array of size n is from 0 to n-1.

7. You can get the length of intArray1 by "intArray1.length".

Practice: Try to output intArray1[3], intArray2[3] and boolArray[0]. See what happens and try to explain.


Multi-dimensional Array (ArrayTest2.java):

1. In mathematics, we can use define variable with a combination of subscripts, say "x11", "x12", "x13", "x21", "x22", "x23", "x31", "x32", "x33", and so on.

2. In programming, we can have multi-dimensional array.

3. Considering the two-dimensional array, to declare:

int [ ][ ] a;

Note: we say that "a" is an array of int array.

4. To allocate space for the first dimension:

a = new int[3][ ];

Note: a[0], a[1] and a[2] are int arrays now.

5. To allocate space for the second dimension:

for (i=0; i<3; i++) {
    a[i] = new int[4];
}

Note: a[0], a[1] and a[2] are int arrays with size 4 now . Therefore we have 12 integer variables: a[0][0], a[0][1], a[0][2], a[0][3], a[1][0], a[1][1], a[1][2], a[1][3], a[2][0], a[2][1], a[2][2], a[2][3]. Actually, we may allocate different size for the three arrays a[0], a[1] and a[2].

6. A simpler approach to declare and allocate space for a two-dimensional array:

int [ ][ ] b = new int[3][4];

7. Similarly, we can declare and allocate space for a three-dimensional array:

int [ ][ ][ ] c = new int[2][3][4];

Practice: Try to write a program output a "board" with "*" and "-", based one a two-dimensional boolean array "star" with size of "size" times "size". Assume the variables "star" and "size" are hard-coded.

For example, assume "size" is 3. And the values of the elements of "star" are:

star[0][0] = true; star[0][1] = true; star[0][2] = true;
star[1][0] = true; star[1][1] = false; star[1][2] = true;
star[2][0] = false; star[2][1] = false; star[2][2] = false;

Then, the output should be:

***
*-*
---

Change the values of "size" and "star", to see different output.