Chapter 3 Flashcards
How do you create an object? What is the name of its process?
An object is created via the “new” operator. The The “new” operator calls the constructor, which is a special method that sets up the object.
What does it mean to declare an object reference variable?
When writing for example: String pizza; you’re not creating an object of type String called pizza, but you’re creating a reference to the object, so not the object per se will be saved, but its connection to it (its address).
What’s the difference between a primitive variable and an object variable?
A primitive variable contains the value itself (num1 = 38;) while an object variable contains the address of the object (name1 = new String(“Steve Jobs”).
What are aliases?
Aliases are two or more object references that refer to the same object.
What’s the difference between:
String chocolate = new String(“…”)
and
chocolate = “…”;
The second one is more brief, but they’re equivalent.
Can a string object be modified?
No. But modified versions of the original can be created with several methods of the String class.
What is the index in a string?
In a string, each written character has an identificative number starting from 0. The letter “o” of “hello” would be at index 4.
What is a class library?
A class library is a collection of classes that we can use when developing programs.
Without importing a specific class from a package, how could you use in a program? Take the Scanner class for example.
Instead of
import java.util.Scanner,
we could call the class by writing its full name
java.util.Scanner.
What is the function of NumberFormat and DecimalFormat? How are they implemented?
NumberFormat and DecimalFormat are used to format output data by presenting it in a particular way.
Example:
NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
or
NumberFormat fmt2 = NumberFormat.getPercentInstance();
and then writing
(“Subtotal: “ + fmt1.format(subtotal));
DecimalFormat fmt = new DecimalFormat (“0.###”)
and
(“Subtotal + fmt1.format(subtotal));
What is an enumerated type declaration?
An enumerated type declaration lists all possible values for a variable of that type.
Example: enum Season(winter, spring, summer, fall)
the values inside the brackets are the only that can be assigned to the variable Season.
The variable Season will act therefore as a class. Example:
Season time; declaration of variable time as type Season.
time = Season.fall
What is the ordinal value of an emurated type?
Each enumerated type is stored as an integer, called its ordinal value.
The first value in an enumerated type is 0, the second is one, and so on.
What are Wrapper Classes and where do we use them?
Wrapper classes are classes that create objects containing primitive values.
Integer age = new Integer(40); is an object type Integer holding the value 40.
Wrapper classes are used in situations where a primitive value will not suffice.
What is autoboxing?
Autoboxing is the automatic conversion of a primitive value to a corresponding wrapper object.
Example:
Integer obj;
int num = 42;
obj = num;
What is the Type Comparison Operator?
The instanceof operator compares an object to a specified type.
You can use it to test if an object is an instance of a class, of a subclass, ecc.
Example: Integer obj1 = new Integer(3); System.out.println("obj1 instanceof Integer: " + (obj1 instanceof Integer));
Result: obj1 instanceof Integer: true