Monday, November 3, 2008

Alternative Array Declaration Syntax, Strings & Pointers

Alternative Array Declaration Syntax

 

There is a second form that may be used to declare an array:

 

type[ ] var-name;

 

Here, the square brackets follow the type specifier, and not the name of the array variable.

 

For example, the following two declarations are equivalent:

 

int al[] = new int[3];

int[] a2 = new int[3];

 

The following declarations are also equivalent:

 

char twod1[][] = new char[3][4];

char[][] twod2 = new char[3][4];

 

This alternative declaration form is included as a convenience, and is also useful when specifying an array as a return type for a method.

 

A Few Words about Strings

 

As you may have noticed, in the preceding discussion of data types and arrays there has been no mention of strings or a string data type. This is not because Java does not support such a type—it does. It is just that Java’s string type, called String, is not a simple type. Nor is it simply an array of characters (as are strings in C/C++). Rather, String defines an object, and a full description of it requires an understanding of several object-related features.

 

As such, it will be covered later in this book, after objects are described. However, so that you can use simple strings in example programs, the following brief introduction is in order.

 

The String type is used to declare string variables. You can also declare arrays of strings. A quoted string constant can be assigned to a String variable. A variable of type String can be assigned to another variable of type String. You can use an object of type String as an argument to println( ). For example, consider the following fragment:

 

String str = "this is a test";

System.out.println(str);

 

Here, str is an object of type String. It is assigned the string “this is a test”. This string is displayed by the println( ) statement. As you will see later, String objects have many special features and attributes that make them quite powerful and easy to use. However, for the next few chapters, you will be using them only in their simplest form.

 

A Note to C/C++ Programmers about Pointers

 

If you are an experienced C/C++ programmer, then you know that these languages provide support for pointers. However, no mention of pointers has been made in this post. The reason for this is simple: Java does not support or allow pointers. (Or more properly, Java does not support pointers that can be accessed and/or modified by the programmer.)

 

Java cannot allow pointers, because doing so would allow Java applets to breach the firewall between the Java execution environment and the host computer. (Remember, a pointer can be given any address in memory—even addresses that might be outside the Java run-time system.)

 

Since C/C++ make extensive use of pointers, you might be thinking that their loss is a significant disadvantage to Java. However, this is not true. Java is designed in such a way that as long as you stay within the confines of the execution environment, you will never need to use a pointer, nor would there be any benefit in using one.

 

No comments: