University of Toronto - Fall 2000
Department of Computer Science

Week 6 - Comparing Objects

Objects vs Primitive Data Types

Primitive Data Type

	int i, j;
	i = 2;
	j = 2;
	if (i == j) {
		System.out.println("same");
	}

Class Data Type

This works for objects too, but not in the same way you might expect:

	Ex e = new Ex();
	e.field = 2;
	Ex f = new Ex();
	f.field = 2;

	if (e != f) {
		System.out.println ("not same");
	}
	if (e.field == f.field) {
		System.out.println ("same");
	}
	e = f;    // Now they refer to the same object
	if (e == f) {
		System.out.println ("same");
	}