More Classes and Objects

Let's talk some more about classes and objects. We'll see some of the more commonly used Java classes, talk about how to use them, and talk more about exactly what classes and objects are. Note: Each programming language handles common tasks differently. We will learn the way Java does it and try to learn it as well as we can, but always remember that we are just seeing the way Java does it. There is nothing right, wrong, special, better, or worse about the way Java does things.

Variables, Classes, Objects, and References

In Java, most of the "things" you use in programs are either variables or objects. For instance, consider the following program:
public class x {
	public static void main (String args[]) {
		int y;
		String z;
		y = 100;
		z = y + " is a number";
		System.out.println (z);
	}
}
In this program, x is a class, y is a variable of type int, and z is a reference to an object of class String. What does it mean to be a variable? y is a "thing" that has the capability to store a single int value. When we use the name y in the program, we are talking about that particular piece of storage; y is identical to that particular piece of storage.

What does it mean to be a reference? z is a variable that refers to a String object. It is not the object itself, but rather a way of getting at the object. Think of it as a handle, like the handle of a suitcase. The handle is not the same as the suitcase, and in fact you can switch the handle with a different handle and still have the same suitecase, but you need some handle to pick up the suitcase. z is not identical to the object that it refers to; it is just a way of getting access to that object.

That's weird. So what's the difference between a variable, an object, and a reference? An object can have methods, like length(), that can act on the object and return values or references to other objects. The methods are accessed through the object's reference. Regular variables with types like int or float can't have methods. The difference is actually a bit more nuanced. Consider the following Java classes:

What will be the output of A? Why?

String Class

We have seen uses of String before in this class and probably in CS 1063 so much of this might be review. A String object stores sequences of characters that we can use for sentences or other textual needs. A String object cannot be changed, i.e. it is immutable. Once a String object gets a value, that value cannot be changed at all. However, we can combine or modify old strings to form new strings and reassign the old references to refer to the new strings, so it seems as though String objects can change.

Here are some methods in String:

Your book mentions a few more methods. Let's look at a little program that uses the length, charAt, and + features:
public class y {
        public static void main (String args[]) {
                String a = new String ("this is a string");
                String b = new String ("");
                for (int i=0; i<a.length(); i++) {
                        b = b + a.charAt (a.length()-1-i);
                }
                System.out.println (b);
        }
}
How many String objects does this program create? How many String references does it create?

Wrapper Classes

Java provides "wrapper" classes for the native data types. Objects of these classes contain a variable of a native type, like int, along with methods for doing useful things that the native type can't do by itself, e.g. converting to and from String. Here is a partial list of the wrapper classes: Two useful methods in the wrapper classes are parseInt(String str) and String toString().

Random Class

The Random class allows the use of a pseudorandom number generator to give numbers that appear random. We have seen an example of Random in the lecture on graphics.

Math Class

The Math class has many useful static methods that provide mathematical functions such as sine, cosine, tangent, absolute value, square root, exponential, etc.

Writing Our Own Class

For your second homework you have already written your own class, but let's think a little more about what goes into that.