Chapter 3 Flashcards
What is the primary task when writing a Java program?
Defining a set of classes that your program will use
What statement would you use if you wanted to create a variable called “name” which held the text string, “Joe”?
String name = new String(“Joe”);
What is the name of the class that is typically used to divide a string into shorter strings called tokens?
StringTokenizer
it’s in the java.util package
What do we call the special method that gets called when you use the “new” operator to create an object of a class?
a constructor
How does Java handle “garbage collection”?
Java automatically clears memory of any objects that are no longer in use.
Using dot notation, how would you refer to a variable called “driver” in an object called “car”?
car.driver
If the driver of a car was represented in dot notation as “car.driver”, how could you represent the age of that driver?
car.driver.age
this is called “chaining”
What statement would you use to declare a class variable called lastName and assign a value of “Jones” to it?
static String lastName = “Jones”;
static is the keyword that specifies the variable is a class variable
When retrieving or changing the value of a class variable, how do you refer to that variable using dot notation?
[object instance].[variable]
OR
[class].[variable]
(For readability, the second method is preferable since it is, after all, a class variable]
When reading code, what distinguishes methods from other program elements?
parentheses
Example: cancelOrder()
What method can be used to print a number in currency format?
System.out.format( );
In the parentheses, you use a percent sign followed by one or more letter flags, then a comma and the variable to be formatted.
What is an object reference?
An address indicating where the object’s variables and methods are stored
What special flexibility does Java demonstrate when processing a group of concatenated variables where at least one of them is a string?
Java treats the whole group as a String.
What is casting?
Converting a value from one data type to another
Which primitive data type can not be cast to any other data type?
Booleans