The Java Programming Language:
Arrays, Strings, StringBuffers and StringTokenizers
Arrays in Java are not like those in C and C++, which are the same kinds of primitive structures found in many traditional programming languages. Java arrays are "first-class" objects. But, that having been said, much of what is done with arrays in Java will be very familiar to C and C++ programmers.
The first sign that Java arrays really are objects is that any Java array knows its own length, which is obtained via nameOfArray.length. Note that length is a member variable, not a method. And note that Java multi-dimensional arrays are "arrays of arrays", so this value for a two-dimensional array (for example) will be the number of rows.
Here are some code examples to illustrate various points about one-dimensional arrays:
int[] numbers = { 1, 2, 3, 4 }; // Typical initialization int numbers[] = { 1, 2, 3, 4 }; // Also works final int SIZE = 4; // Make array size a named constant int[] numbers = new int[SIZE]; // Create a new array on the heap for (int i=0; i<SIZE; i++) // Produces same result as numbers[i] = i+1; // either initialization above for (int i=0; i<numbers.length; i++) // Typical array access loop System.out.println(numbers[i]); // Note the differences between the following two declarations int[] a, b; // Both a and b are references to int arrays int a[], b; // a is a reference to an int array // b is just a simple int variable
Here are some code examples to illustrate various points about multi-dimensional arrays:
// Declare an array with 2 rows and 7 columns // E final int NUM_ROWS = 2; final int NUM_COLS = 7; int [][] temperatures = new int[NUM_ROWS][NUM_COLS]; // Declare and initialize an array of 2 rows and 7 columns int [][] temperatures = { { 15, 17, 19, 17, 22, 24, 18 }, { 18, 20, 20, 19, 24, 25, 25 } }; // Typical two-dimensional array access for (int row=0; row<NUM_ROWS; row++) for (int col=0; col<NUM_COLS; col++) System.out.println(temperatures[row][col]);
The Java String class is analogous to the C++ string class, but note the capitalization difference in the spelling of the class name. Also note that the "old fashioned" array-of-character-with-null-at-the-end version of a string that exists in both C and C++ (and is usually called a C-string or a C-style string) is not present in Java.
Here are some things to note about Strings in Java:
The Java String class provides a rich collection of methods for dealing with strings. Here are a few of them, but it's worthwhile checking out the String API any time you are doing serious work with strings:
Here are some code examples to illustrate various points about strings:
String greeting; greeting = "Hello"; System.out.println(greeting + " " + greeting.length()); System.out.println(1 + 2 + greeting + 3 + 4); String str = new String(" there!"); greeting += str; System.out.println(greeting); System.out.println(greeting.charAt(0)); greeting[0] = 'h'; // compile-time error // To get a number from a string, and this is // what we do quite a bit, since we just read // strings from the keyboard and then turn them // into numbers (the "trimming" is often not necessary): String nStr = " 1234567 "; int n = Integer.parseInt(nStr.trim()); String dStr = " 2.71828 "; double dd = Double.valueOf(dStr.trim()).doubleValue(); System.out.println(n + "\n" + dd); // or, analogously String nStr = " 1234567 "; int n = Integer.valueOf(nStr.trim()).intValue(); String dStr = " 2.71828 "; double dd = Double.parseDouble(dStr.trim()); System.out.println(n + "\n" + dd);