6.170 / Spring 2001 / Lecture 2 Card Answers

Handout L2-QA

Question: What does the keyword this. mean?
Answer: this refers to the object whose code is being executed. It can be derefenced by . for a field reference or a method call. Preceding a field reference or method call by this. is optional. For instance, the code:
class Professor {
   boolean tenured;
   ...

   boolean isTenured() {
      return this.tenured;
   }
}
is equivalent to:
class Professor {
   boolean tenured;
   ...

   boolean isTenured() {
      return tenured;
   }
}

Question: What are the names for the parts of System.out.println("a")?
Answer: System is the name of a class in the Java library. In this class, there is a static field called out, whose value defines the default location for printed output from your program (which by default is your terminal window). println is a method of the object System.out.

Question: How do I create a new object?
Answer: A new object can be created by calling a constructor for a class. The syntax for doing this uses the new keyword. The name of a constructor for a class is the name of the class. So, if class Professor contains the constructor:

public Professor(String name, boolean tenured) { 
   ...
}
then a new Professor object can be created (and assigned to a new variable p) using the statement:
Professor p = new Professor("Michael Ernst", false);

Question: What is the difference between primitives and objects?
Answer: Primitive types are not classes, and so primitives contain a single piece of data and do not contain methods. So, you cannot call methods on primitives.

Question: In the vector diagram shown in class, why is there only one "mit" (and "MIT") object that v and x both pointed to?
Answer: As an optimization, if two String literals have the same value, Java will create just one String to be shared. So, even though "mit" was created at two different points in the code, there is only one String with value "mit" created. This optimization is specific to Strings.

Information about this behavior is available in the Java Language Specification section on String Literals. In short,

Question: What is the difference between value and reference/object equality?
Answer: Reference equality (==) tests whether two variables name the same object. Value equality (.equals()) tests whether two variables name objects with the same value; that is, the observable state of the objects are the same. The precise definition of having the same value can be defined by the programmer, since equals() must be implemented for any class that you write (whereas == cannot be overridden). For any implementation of equals(), (x == y) should imply that x.equals(y).

Question: What is the difference between mutable and immutable objects?
Answer: Immutable objects have a value that is defined upon creation. After the object is created, the value will never change. For example, the number "1" is always "1"; it will never change to the number "2"; so the Integer class in Java is immutable. Mutable objects have dynamic state that can change over time. The elements of a vector can change over time, so the Vector class in Java is mutable.


Back to Index Card Answers.
Back to the 6.170 home page.
For problems or questions regarding this page, contact: 6.170-webmaster@mit.edu.
$Id: lec02-qa.html,v 1.2 2001/02/12 20:16:59 mistere Exp $