Download presentation
Presentation is loading. Please wait.
Published byJuliet Knight Modified over 9 years ago
1
© 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)
2
© 2006 Pearson Addison-Wesley. All rights reserved1-2 Arrays Collection of elements with the same data type Array elements have an order Support direct and random access One-dimensional arrays –Declaration example final int DAYS_PER_WEEK = 7; double [] maxTemps = new double[DAYS_PER_WEEK]; –Length of an array is accessible using data field length –Use an index or subscript to access an array element
3
© 2006 Pearson Addison-Wesley. All rights reserved1-3 Arrays Figure 1-7 One-dimensional array of at most seven elements
4
© 2006 Pearson Addison-Wesley. All rights reserved1-4 Arrays One-dimensional arrays (continued) –Initializer list example double [] weekDayTemps = {82.0, 71.5, 61.8, 75.0, 88.3}; –You can also declare array of object references Multidimensional arrays –Use more than one index –Declaration example final int DAYS_PER_WEEK = 7; final int WEEKS_PER_YEAR = 52; double[][] minTemps = new double[DAYS_PER_WEEK][WEEKS_PER_YEAR];
5
© 2006 Pearson Addison-Wesley. All rights reserved1-5 Arrays Figure 1-8 A two-dimensional array
6
© 2006 Pearson Addison-Wesley. All rights reserved1-6 Arrays Passing an array to a method –Declare the method as follows: public double averageTemp(double[] temps, int n) –Invoke the method by writing: double avg = averageTemp(maxTemps, 6); –Location of array is passed to the method Cannot return a new array through this value –Method can modify content of the array
7
© 2006 Pearson Addison-Wesley. All rights reserved1-7 Variables Represents a memory location Contains a value of primitive type or a reference Its name is a Java identifier Declared by preceding variable name with data type double radius; // radius of a sphere String name; // reference to a String object
8
© 2006 Pearson Addison-Wesley. All rights reserved1-8 Primitive Data Types Figure 1-5 Primitive data types and corresponding wrapper classes
9
© 2006 Pearson Addison-Wesley. All rights reserved1-9 References Data type used to locate an object Java does not allow programmer to perform operations on the reference value Location of object in memory can be assigned to a reference variable
10
© 2006 Pearson Addison-Wesley. All rights reserved1-10 References int myVar = 23; MyObject myObj = new MyObject(12, “I was here”); myVar 23 Memory myObj 0x121AB450 12 I was here 0x121AB470........ <--primitive <--reference
11
© 2006 Pearson Addison-Wesley. All rights reserved1-11 References myVar 23 Memory myObj 0x121AB450 12 I was here 0x121AB470........ <--primitive (literal) <--reference method(myVar); public void method(int x) { x = 45; } myVar = ? Really passes in 23 x 23.
12
© 2006 Pearson Addison-Wesley. All rights reserved1-12 References myVar 23 Memory myObj 0x121AB450 12 I was here 0x121AB470........ <--primitive (literal) <--reference method(myObj); public void method(MyObject x) { x.myNumber = 45; x.myString = “Was I here?”; } myObj = ? myObj.myNumber = ? myObj.myString = ? Really passes in 0x121AB450 x 0x121AB450.
13
© 2006 Pearson Addison-Wesley. All rights reserved1-13 References myVar 23 Memory myObj 0x121AB450 12 I was here 0x121AB470........ <--primitive (literal) <--reference int otherVar = myVar; otherVar = 67; myVar = ? 23 otherVar 23 67
14
© 2006 Pearson Addison-Wesley. All rights reserved1-14 References myVar 23 Memory myObj 0x121AB450 12 I was here 0x121AB470........ <--primitive (literal) <--reference MyObject otherObj = myObj; otherObj.myNumber = 276; myObj.myNumber = ? 0x121AB450 otherObj 276
15
© 2006 Pearson Addison-Wesley. All rights reserved1-15 References myVar 23 Memory myObj 0x121AB450 12 I was here 0x121AB470........ MyObject otherObj = myObj; 0x121AB450 otherObj 276 276 I was here myVar otherObj Balloon analogy: Every object is a balloon Every reference is a piece of string Every variable can hold on to a piece of String
16
© 2006 Pearson Addison-Wesley. All rights reserved1-16 References - Questions How do I copy an object so I end up with two distinct instances? What do I do if I want a method to modify the value of an int? for example: divideByTwo(int number) ? What do I do if I want a method to modify an array? For example: sortArray(int [] myArray) ? Can I modify the values in an object like this: addFortySeven(myObject.myInt);
17
© 2006 Pearson Addison-Wesley. All rights reserved1-17 Named Constants Have values that do not change Declared as a variable but using the keyword final final static int MAX_VALUE=2345; final static String DAY1 = “Monday”;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.