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 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.