13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3.2 Fundamental Language Elements553.2.1.4 OperatorsBefore we move <strong>on</strong> to the topic of arrays (which are sort of a hybrid scalar/object type in Java), let’s spend a moment <strong>on</strong> the operators that can be used inexpressi<strong>on</strong>s (Table 3.1). Most deal with numeric or boolean operands. Forcompleteness, we’ll include the operators that deal exclusively with arrays(the “[]”) and classes (“.”, new, and instanceof), even though we haven’tdiscussed them yet.Operators listed <strong>on</strong> the same row in the table have the same precedence.Operators with the same precedence, except for the unary operators, groupfrom left to right. Unary operators group from right to left.3.2.1.5 ArraysExample 3.2 dem<strong>on</strong>strates the array syntax in Java.Example 3.2 Example array syntaxint [] <strong>on</strong>ed = new int[35];// array = new type[size]int alta [] = {1, 3, 5, 14, 11, 6, 24}; // alternative syntax plus// initializati<strong>on</strong>int j=0;for(int i=0; i alta.length) {j = 0;}}// array[index]// array.lengthThe array can be declared with the [] <strong>on</strong> either side of the variable name.While our example uses the primitive type int, array syntax looks just the samefor any objects.Note that in Java, <strong>on</strong>e doesn’t declare the size of the array. It’s <strong>on</strong>ly increating the array with a new that the array gets created to a particular size.(The {...} syntax is really just a special compiler c<strong>on</strong>struct for what isessentially a new followed by an assignment of values.)Multidimensi<strong>on</strong>al arrays follow the syntax of simple arrays, but withadditi<strong>on</strong>al adjacent square brackets, as shown in Example 3.3.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!