Docsity

Java Programming: Arrays and Operators, Assignments of Computer Science

Partial preview of the text

Download Java Programming: Arrays and Operators and more Assignments Computer Science in PDF only on Docsity!

cs402 - Spring 2003^

Introduction to JavaHandout-2a

cs402 - Spring 2003^

Runtime internals – stack & heap•^ Stack: a run-time data structure. Used to doautomatic memory management in

block-

structured^ languages– Lifetime of storage allocated on stack is tied to thescope in which it was allocated• Heap: all Java objects are allocated on the heap– Lifetime of storage allocated on heap is independent ofthe scope in which it was allocated

cs402 - Spring 2003^

Arrays (ii)• Indexing starts from zero• Array indexes are checked at run-time– If subscript attempts to access element outsidethe bounds of array, the program will raiseexception and cease execution

cs402 - Spring 2003^

Arrays (iii)• Arrays are like objects– The language specification says so– Array types are reference types, just like objecttypes– Arrays are allocated with “new” operator– Arrays are always allocated on heap not stack– The parent class of all arrays is Object; you cancall any of the methods of Object on an array

cs402 - Spring 2003^

Arrays (v) Ex: public static void main(String args[]) {int i=0, n, k;n = args.length; // number^ of arguments (the// total number^ of Strings in args[]k = args[i].length();^ // length of string at// index i^ in args[]}

cs402 - Spring 2003^

Arrays (vi)• Declaring an array only creates a reference int days[]; // days can hold a reference to// to any size array of int • You must make the reference point to anarray before you can use it days = new int[7]; • Once the array object has been created itcannot change in size

cs402 - Spring 2003^

Arrays (viii)• You can initialize an array in its declaration usingan array initializer byte b[] = { 0, 1, 2,^ 3,^ 4,^2 };^ //^ array^ of

6 bytes String^ weekendDays[]^ =^ {“Sat”,

“Sun”,^ };

•^ Can’t use an array initializer anywhere out side adeclaration weekendDays^ =^ {“Sat”,^ “Sun”,

};^ //^ Error weekendDays^ =^ new^ String[]

{“Sat”,^ “Sun”,^ };^ //^ Ok

cs402 - Spring 2003^

Arrays (ix)• The language specification says there are no multi-dimensional^ arrays in Java• Java only has arrays of arrays^ and it callsthem arrays of arrays Ex:Fruit plums[][]; // array of^ arrays whose elements// are^ Fruit objectsplums = new Fruit[5][6];^ // array[5] of array[6]plums [i] = new Fruit[7];^ // Okplums [i][j] = new Fruit(); // individual Fruit

cs402 - Spring 2003^

Arrays (xi)• If you don’t instantiate all dimensions atonce, then you have to instantiate the mostsignificant dimensions first Ex:int apple[][] = new int[5][];^ // Okint apple[][] = new int[5][6]; // Okint apple[][] = new int[][3];^ // Error

cs402 - Spring 2003^

Operators (i)• The order of operand evaluation is welldefined in Java– Expressions are evaluated left-to-right– The left operand is evaluated before the rightoperand of a binary expression; true even forthe assignment operator– In an array reference the expression before the[] is fully evaluated before any part of the indexis evaluated

cs402 - Spring 2003^

Associativity• There are three factors that influence theultimate value of an expression:– Precedence^ indicates that some operations bindmore tightly than others– Associativity^ is the tie breaker for deciding thebinding when we have several operators ofequal precedence strung together– Order of evaluation^ tells the sequence, for eachoperator, in which the operands are evaluated