Arrays and References

Chapter 1
Java Fundamentals Arrays and References
(updated by Dan Fleck)
© 2006 Pearson Addison-Wesley. All rights reserved
1-1
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
© 2006 Pearson Addison-Wesley. All rights reserved
1-2
Arrays
Figure 1-7
One-dimensional array of at most seven elements
© 2006 Pearson Addison-Wesley. All rights reserved
1-3
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];
© 2006 Pearson Addison-Wesley. All rights reserved
1-4
Arrays
Figure 1-8
A two-dimensional array
© 2006 Pearson Addison-Wesley. All rights reserved
1-5
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
© 2006 Pearson Addison-Wesley. All rights reserved
1-6
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
© 2006 Pearson Addison-Wesley. All rights reserved
1-7
Primitive Data Types
Figure 1-5
Primitive data types and corresponding wrapper classes
© 2006 Pearson Addison-Wesley. All rights reserved
1-8
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
© 2006 Pearson Addison-Wesley. All rights reserved
1-9
References
int myVar = 23;
MyObject myObj = new MyObject(12, “I was here”);
Memory
myVar
23
<--primitive
myObj
0x121AB450
<--reference
..
..
0x121AB450
12
0x121AB470
I was here
© 2006 Pearson Addison-Wesley. All rights reserved
1-10
References
Memory
myVar
23
myObj
0x121AB450
..
..
<--primitive (literal)
<--reference Really passes in
23
method(myVar);
0x121AB450
12
0x121AB470
I was here
public void method(int x) {
x = 45;
}
.
x
23
© 2006 Pearson Addison-Wesley. All rights reserved
myVar = ?
1-11
References
Memory
myVar
23
myObj
0x121AB450
..
..
<--reference Really passes in
0x121AB450
method(myObj);
0x121AB450
12
0x121AB470
I was here
.
x
<--primitive (literal)
0x121AB450
© 2006 Pearson Addison-Wesley. All rights reserved
public void method(MyObject x) {
x.myNumber = 45;
x.myString = “Was I here?”;
}
myObj = ?
myObj.myNumber = ?
myObj.myString = ?
1-12
References
Memory
myVar
23
myObj
0x121AB450
otherVar
23
67
..
..
0x121AB450
12
0x121AB470
I was here
© 2006 Pearson Addison-Wesley. All rights reserved
<--primitive (literal)
<--reference
int otherVar = myVar;
otherVar = 67;
myVar = ?
23
67
1-13
References
Memory
myVar
23
myObj
0x121AB450
otherObj
0x121AB450
..
..
0x121AB450
276
12
0x121AB470
I was here
© 2006 Pearson Addison-Wesley. All rights reserved
<--primitive (literal)
<--reference
MyObject otherObj = myObj;
otherObj.myNumber = 276;
myObj.myNumber = ? 276
1-14
References
Memory
myVar
23
myObj
0x121AB450
otherObj
0x121AB450
..
..
0x121AB450
276
12
0x121AB470
I was here
© 2006 Pearson Addison-Wesley. All rights reserved
MyObject otherObj = myObj;
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
1-15
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);
© 2006 Pearson Addison-Wesley. All rights reserved
1-16
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”;
© 2006 Pearson Addison-Wesley. All rights reserved
1-17