What is the difference between a class and an object?
A class is a blueprint that defines a type’s data and behavior; an object is an instance of that class created during program execution.
Study Java Objects, Classes, and Strings with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is the difference between a class and an object?
A class is a blueprint that defines a type’s data and behavior; an object is an instance of that class created during program execution.
What does assignment copy for an object reference?
A class-type variable stores a reference to an object, not the object’s contents directly. Assigning one reference to another copies the reference.
What does null mean for a Java reference?
null means a reference does not currently refer to any object. Calling an instance method through that reference causes a NullPointerException.
What defines a Java constructor?
A constructor initializes a new object. It has the same name as its class, has no return type, and runs when invoked with new.
How does Java choose among overloaded constructors?
Java selects an overloaded constructor whose parameter types and number match the arguments. If any constructor is declared, Java does not automatically add a no-parameter constructor.
How do instance and static method calls differ?
An instance method is called on an object, such as text.length(). A static method belongs to the class and is called through its class name, such as Math.sqrt(81.0).
Why does word.toUpperCase() not change word?
String objects are immutable: their characters cannot be changed. Methods such as toUpperCase() return a new string, so the result must be saved to change a variable’s reference.
What does "computer".substring(0, 4) return?
substring(0, 4) returns characters at indices 0 through 3 because the start is inclusive and the end is exclusive. For "computer", the result is "comp".
How should Java compare String contents?
Use equals to compare string contents. The == operator compares object references, so it does not normally test whether two strings contain the same characters.
What are first and second after this trace?
first remains "AP" and second becomes "AP CSA". Because String is immutable, += creates a new string and assigns its reference only to second.
What are autoboxing and unboxing?
Autoboxing converts a primitive to its wrapper object, such as int to Integer. Unboxing converts a wrapper object back to its primitive, such as Integer to int.
How do parseInt and valueOf differ?
Integer.parseInt("123") returns the primitive int 123, while Integer.valueOf("123") returns an Integer wrapper object.