Object Class

Java defines the class Object, which is a parent of every other class. Every class that you write (and every other class in the Java library) extends Object automatically. In fact, even primitive variables like ints and chars can be treated as objects. (Primitive variables are NOT objects, but they can be treated as such because of a feature called auto-boxing. This turns an int into an Integer object, a double into a Double object, etc.) For example, we can store all kinds of values in an Object variable:

Object val1 = 4;
Object val2 = "hello";

//Assumes we have our Person class from the previous section
Object val3 = new Person("Bob", 25);

This is handy when we want to define classes that can store general types of data. If we let them store objects, then they can hold any kind of values. All objects can use the method equals, which compares if two objects are the same. They also all have the method toString, which converts the object into a string.