Tuesday July 18, 2000
Quiz 24
1. When a variable is available from its point of declaration throughout an entire class what is it said to have?
2. When a variable is only available from it point of declaration to a } what is it said to have?
3. When a reference to an object in memory is lost, what is that object called?
4. What is the special area of memory reserved for the dynamic allocation of objects during run-time called?
Recall that yesterday we were discussing arrays. Arrays are named collections of entities all of the same type.
That is, we can have an array of ints, floats, doubles, Strings, or other objects. Notice that we cannot mix these up.
So one question might be if I wanted an array to hold data that might be int, float, double, etc., then how would we do it?
Before we continuing discussing arrays let us talk about this topic. The answer to the question is wrapper classes.
Recall that primitive data types cannot be treated as objects. That is we cannot invoke a method on a primitive data type
like we can on an object. Therefore, James Gosling and the Java team at Sun Microsystems devised a means by which we can turn
a primitive data type into an object.
In the package java.lang we find the following classes.
- Integer.java
- Long.java
- Float.java
- Double.java
- Character.java
- Boolean.java
- Byte.java
- Short.java
We have not dealt with data items of type byte and short. But they are the other two primitive data types that Java supports.
The classes are the wrapper classes.
Let's examine the Integer class to understand how these wrapper classes work.
There are two constructors in the Integer class.
public Integer(int value)
public Integer(String s)
It has one important instance variable.
private int value;
The first constructors accepts an int value. So this creates a new Integer object and sets the value of the instance variable
value.
The second constructor accepts as input a String and converts that string to a decimal number and places that value in the
instance variable value.
In order to retrieve the integer value stored in the class there is an instance method.
public int intValue()
So we can now see how the wrapper class functions. The essential part of the class is the integer which is stored in it.
However, that integer must be placed inside a class definition in order to create an object. Consider the following example.
public class Test {
public static int return_value(Object o) {
int a = ((Integer)o).intValue();
return(a);
}
public static void main(String[] args) {
int a = 3;
int b = Test.return_value(a);
System.out.println(b);
}
}
The compiler reports the following error.
Test.java:12: Incompatible type for method. Can't convert int to java.lang.Object.
int b = Test.return_value(a);
^
1 error
So we must convert the integer we want to send into an object. We do this with a wrapper class.
public class Test {
public static int return_value(Object o) {
int a = ((Integer)o).intValue();
return(a);
}
public static void main(String[] args) {
int a = 3;
Integer c = new Integer(a);
int b = Test.return_value(c);
System.out.println(b);
}
}
It compiles successfully, and when run it produces the following output.
3
The other wrapper classes work in the same way.
If we need to store int, floats, doubles, etc. in the same array we could use wrappers classes to turn them into objects.
Recall the syntax to declare a new array.
data-type[] arrayname = new data-type[number of elements];
So if we wanted to declare a new array with 5 ints we could use the following syntax.
int[] numbers = new numbers[5];
We can also declare identifier without actually setting up the array itself.
int[] numbers;
When this code is compiled a new reference to an array is created but set to null. Later we can make a call to new to create
a new array.
The right side of the declaration of an array works like any other call to a constructor. It creates a new object in memory
and references that object by the array name.
The array name can be any legal Java identifier.
When a new array is created the name of the array references the first element in the array.
Java begins counting at 0.
In order to retrieve elements from an array we must have two vital peices of information. We must have the array name and the
position within the array from which we want to retrieve the element.
For example the first element in the array numbers would be retrieved by using the array name and the position 0. In Java we
would write this as
numbers[0]
The position must be enclosed between the brackets.
Arrays can be dynamically created during run-time. For example, we could make an array an instance variable in a class and
wait until the constructor to create a new array. However, once an array is declared we can't change its size during
run-time.
In order to see how to use arrays in practice let's consider the following problems.
- Create an array consisting of 5 ints each initialized to the value 1.
- Create an array consisting of 5 ints each read from the keyboard. Then find the sum of the elements in the array.
- Create an array consisting of 10 doubles each read from the keyboard. Then find the maximum value read in and the minimum
value read in.
- Read a line in from a file, tokenize that line, and place the tokens you find into a String array.
- Create an array whose elements are 5-minute blocks of time from 7:00 am to 11:00 pm. For times after Noon
we will use military time.
For example the array should contain the following.
times[0] = 700;
times[1] = 705;
times[2] = 710;
times[3] = 715;
times[4] = 720;
times[5] = 725;
times[6] = 730;
times[7] = 735;
times[8] = 740;
times[9] = 745;
times[10] = 750;
times[11] = 755;
times[12] = 800;
...
- Rewrite the Sieve of Eratosthenes using an array? Do you find it much easier?