img
0
0
0
0
0
0
1
2
3
4
0
2
4
6
8
0
3
6
9
12
0
0
000
0
2
468
0
4
8 12 16
0
6
12 18 24
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 offers convenience when declaring several arrays at the
same time. For example,
int[] nums, nums2, nums3; // create three arrays
creates three array variables of type int. It is the same as writing
int nums[], nums2[], nums3[]; // create three arrays
The alternative declaration form is also useful when specifying an array as a return type for
a method. Both forms are used in this topic.
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. 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 topic,
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
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home