Lesson 2: Objects, Classes and Reference Types
Primitive types hold simple values, but most programs work with objects: values that bundle data together with the methods that operate on it. A String is an object that holds text and provides methods such as length and substring; a Scanner is an object that reads input; and the classes you write yourself define new kinds of objects, such as a BankAccount or a Student. The distinction between primitives and objects is fundamental in Java, and it shapes how values are stored, compared, and passed to methods.
A class is the blueprint for objects, and an object is an instance of a class. The class defines the fields, the data each object holds, and the methods, the actions each object can perform. When you write new Student("Maya", 11), you are calling a constructor, a special method with the same name as the class that creates the object and sets up its fields. The variable that holds the object does not hold the object itself: it holds a reference, an address pointing to where the object lives in memory, and two variables can hold references to the same object.
That reference semantics has consequences you must understand. Assigning one object variable to another, as in Student b = a;, does not copy the object: it copies the reference, so a and b now point to the same object, and a change made through b is visible through a. This is called aliasing, and it is a common source of exam questions and of real bugs. Primitives behave differently: assigning one int variable to another copies the value, so the two variables are independent afterwards. When a question asks what changes after an assignment, first ask whether the variable holds a value or a reference.
Comparing objects needs care as well. The == operator compares references for objects, asking whether two variables point to the same object, not whether the objects have equal contents. Two String objects with the same text are not == unless they are the same object, and the correct test is the equals method, as in a.equals(b). For primitives, == compares values, which is what you expect. The null reference deserves a mention too: a reference variable that points to no object holds null, and calling a method on null causes a NullPointerException, one of the commonest runtime errors in Java.
The declared type of a variable and the class of the object it points to are two different things, and keeping them separate is the key to everything that follows about inheritance. A variable declared as Object can hold a String, a Scanner, or any other object, because every class in Java is a kind of Object, but through that variable you can only use the methods that Object itself provides. The object's own class decides what the method calls actually do, as you will see when a subclass overrides a method. Methods come in two kinds as well: instance methods, which are called on an object and can use its fields, and static methods, which belong to the class itself, such as Math.sqrt, and are called on the class name rather than on an object.
A worked example
Worked example: consider the code below, which creates two references to the same StringBuilder object. The aliasing means that appending through one reference changes what the other sees, while the == comparison on the two String objects compares references, not contents.
StringBuilder sb1 = new StringBuilder("hello");
StringBuilder sb2 = sb1; // alias: same object
sb2.append(" world");
System.out.println(sb1); // prints "hello world"
String a = new String("hi");
String b = new String("hi");
System.out.println(a == b); // false: different objects
System.out.println(a.equals(b)); // true: same contents
The println of sb1 shows 'hello world' because sb1 and sb2 refer to one object, and the append changed that object. The two String objects hold the same text but are different objects, so == is false while equals is true. In free-response questions, draw the memory picture: boxes for variables, arrows for references, and objects with their fields. Tracing with a diagram makes aliasing questions straightforward, while trying to hold the picture in your head invites mistakes.
Key Terms
- Object
- A value that bundles data (fields) with methods, created from a class.
- Class
- The blueprint defining the fields and methods of a kind of object.
- Constructor
- A special method, named like its class, that creates and initializes an object.
- Reference
- A value pointing to where an object lives in memory; object variables hold references.
- Aliasing
- Two variables referring to the same object, so a change through one is visible through the other.
- null
- A reference value pointing to no object; calling a method on it causes an error.
Discussion Questions
- Why does == give a surprising answer for two String objects with the same text?
- What is the difference between a class and an object?
- If two variables alias the same object, what happens when you change the object through one of them?
Practice Questions
- String s1 = new String("abc"); String s2 = s1; Does s2 == s1 evaluate to true?
- What does new Scanner(System.in) do?
- A reference variable holds null. What happens if you call a method on it?
- How do you test whether two String objects contain the same text?
- int a = 5; int b = a; b = 7; What is a now, and why is this different from objects?
Show answers
- Yes, because s2 holds a copy of the reference in s1, so both point to the same object.
- It calls the Scanner constructor to create a new Scanner object that reads from standard input.
- A NullPointerException is thrown at run time.
- Use the equals method, as in s1.equals(s2).
- a is still 5, because primitives are copied by value, so b is independent of a afterwards.