Previous | Table of Contents | Next |
14.2.9.4. Are Arrays Objects?
It is useful to consider arrays to be a separate kind of reference type from objects. In some ways, though, arrays behave just like objects. As we saw, arrays use the object syntax .length to refer to their length. Arrays may also be assigned to variables of type Object, and the methods of the Object class may be invoked for arrays. (Object is the root class in Java, which means that all objects can be assigned to a variable of type Object and all objects can invoke the methods of Object.)
The evidence suggests that arrays are, in fact, objects. Java defines enough special syntax for arrays, however, that it is still most useful to consider them a different kind of reference type than objects.
14.2.9.5. Declaring Array Variables and Arguments
In C, you declare an array variable or array function argument by placing square brackets next to the variable name:
void reverse(char strbuf[], int buffer_size) { char buffer[500]; ... }
In Java, you would have to declare buffer as an array variable, and then allocate the array itself with new, but otherwise you could use the same syntax, with the array brackets after the variable or argument name.
However, Java also allows you to put the array brackets after the type name instead. So you could rewrite this code fragment to look something like this:
void reverse(char[] strbuf, int buffer_size) { char[] buffer = new char[500]; ... }
In a lot of ways, this new array syntax is easier to read and easier to understand. (It doesnt work in C, by the way, because pointers make Cs type declaration syntax a real mess.) The only problem with this new syntax is that if you get in the habit of using it, it will make it harder for you when you (hopefully only occasionally!) have to switch back and program in C.
Java even allows you to mix the declaration styles, which is something you may find occasionally useful (or frequently confusing!) for certain data structures or algorithms. For example:
// row and column are arrays of byte. // matrix is an array of an array of bytes. byte[] row, column, matrix[]; // This method takes an array of bytes and an // array of arrays of bytes public void dot_product(byte[] column, byte[] matrix[]) { ... }
A final point to note about array declarations is that (as weve seen throughout this section) the size of an array is not part of its type as it is in C. Thus, you can declare a variable to be of type String[], for example, and assign any array of String objects to it, regardless of the length of the array:
String[] strings; // this variable can refer to // any String array strings = new String[10]; // one that contains 10 Strings strings = new String[20]; // or one that contains 20. .XE arrays
Strings in Java are not null-terminated arrays of characters as they are in C. Instead, they are instances of the java.lang.String class. Java strings are unusual, in that the compiler treats them almost as if they were primitive typesfor example, it automatically creates a String object when it encounters a double-quoted constant in the program. And, the language defines an operator that operates on String objectsthe + operator for string concatenation.
An important feature of String objects is that they are immutablei.e., there are no methods defined that allow you to change the contents of a String. If you need to modify the contents of a String, you have to create a StringBuffer object from the String object, modify the contents of the StringBuffer, and then create a new String from the contents of the StringBuffer.
Note that it is moot to ask whether Java strings are terminated with a NULL character (\u0000) or not. Java performs runtime bounds checking on all array and string accesses, so there is no way to examine the value of any internal terminator character that appears after the last character of the string.
Some of the more important String methods are length(), charAt(), equals(), compareTo(), indexOf(), lastIndexOf(), and substring().
Previous | Table of Contents | Next |