Working with Objects in Java. Instructor Jeff Zhuk

Calling Methods . References to Object . Castings and Converting Objects and Primitive Types . Comparing Objects . Copying Objects . Determining the Class of an Object .

Calling the method System.out.println

/**
* This class is just an example of calling the method System.out.println
*/
public class TestString {
    public static void main(String arg[]) {
        String string = "We are learning Java";
        System.out.println("The string is:" + string);
        System.out.println("Length of this string:" + string.length());
    }
} 
System is a class. Its variable is out that refers to an object of type PrintStream. The object System.out has an instance method named println(). To call a class method, use dot notation: an instance of the class or the class itself is on the left side of the dot and the name of the method and its argument is on the right side of the dot.
References to Object

import java.awt.Point;

/**
 * In this program, we declare two variables of type Point
 * We create a new Point object and assign it to pt1. 
 * Then we reference pt1 to pt2. 
 * pt1 and pt2 are two variables pointing to the same single object.
**/
public class ReferencesTest {
    public static void main(String args[]) {
        Point pt1, pt2;
        pt1 = new Point(50,50);
        pt2 = pt1;
        pt1.x = 100;
        pt1.y = 100;
        System.out.println("Point1: " + pt1.x + "," + pt1.y );
        System.out.println("Point2: " + pt2.x + "," + pt2.y);
    }   // same values of x and y are printed for both points
}       // as both pt1 and pt2 point to the same single Point object

Casting Primitive Types: (typename) value

typename is the name of the type (int, float, boolean), value is an expression to be converted

Example:

    int z = (int) (x/y);



Casting Objects: (classname) object

classname is the name of the class to cast the object to, object is a reference to the object.

Example: Consider GreenApple extends Apple ....

        Apple apple = new GreenApple(); // yes, always valid
	GreenApple green = (GreenApple) apple;	// not always valid !
Converting Primitive Types To Objects and vice versa
The java.lang package includes wrapper classes that correspond to each primitive data type.

The following line of code creates an instance of the Intejer class with the value 7:

	Integer intObject = new Integer(7);
	int theInt = intObject.intValue();  //returns 7
Comparing Objects: using "==" to compare references and Object.equals(obj) to compare values
class CompareTest {
	public static void main (String args[]) {
		String string1, string2;
		string1 ="comparing objects" ;
		string2 = string1;
		
		System.out.println("The same object? " + (string1 == string2));

		string2 = new String(string1);
	
		System.out.println("The same object?" + (string1 == string2));
		System.out println("The same value?" + String1.equals(string2));
	}
}
Copying Objects using Object.clone()
	String str1,str2;
	str1 = "copying";
	str2 = (String)str1.clone(); // clone() returns Object - cast is needed !

Determining the Class of an Object

To find out the class of an object use getName() method of class Class :

Object obj = vector.elementAt(0); // get an object of unknown type from a vector	
String className = obj.getClass().getName(); // get a class name for the object
The other way to determine whether the object is an instance of the named class is instanceof :
    if(apple instanceof GreenApple) {
        GreenApple green = (GreenApple) apple;

Back To Java as OOP Language