Java Ch 3 Flashcards
(33 cards)
How do you cast back and forth between objects and primitive data types?
Not possible to do directly.
You can use classes with the names of primitive data types to create objects with fixed values, and then convert those objects back to a primitive data type using expressions.
For instance,
Integer dataCount=new Integer(7801);
creates an object of the Integer type with fixed value 7801.
int newCount=dataCount.intValue();
will then return am integer value of 7801.
What operator creates instances of a class?
new
How do you convert a String object (note the capital letter S) to a number like an int or float?
The parseInt() class method of the Integer class, or an analogous method. For example,
String yar=”26”;
int penn=Integer.parseInt(pennsylvania);
Name the primitive classes that can be converted back and forth to String class. What are these classes called?
Boolean, Byte, Character, Double, Float, Integer, Long, Short, Void
Object wrappers
Auto boxing and unboxing?
Convert primitive data type to object, and vice versa.
What does the StringTokenizer class do?
Divides a string into a series of shorter strings called tokens, by using a specific character or characters as a delimiter.
07/03/75 could be a string, and the forward slash could act as a delimiter. L
What are the default delimiters used in tokenizing?
Spaces, tabs, newlines, returns, or form feed characters. These are automatically used as delimiters if they appear in the string.
What are the 3 things that happen when you use the new operator?
A new instance of the given class is created
Memory is allocated for the new instance
A constructor is called, which is defined in the given class
What does a constructor do?
Initializes the new object and its variables, creates any other objects that the object needs, and performs any additional operations the object requires to initialize itself.
Can a class have multiple constructors?
Yes, each with a different number or type of arguments.
When you use the new command, you can specify different arguments in the argument list, and the correct constructor for those arguments is already called.
Discuss TokenTester class.
Having multiple constructor definitions allow the TokenTester class to accomplish different things with different uses of the new operator.
Constructors in a class must have different numbers and types of arguments, because this is how they are differentiated.
What happens if a class defines no constructors, and a new instance of the class is created?
A constructor with no arguments is called. This constructor simply calls the same constructor in its superclass.
What does the static command do?
Sets a class variable
Can you use an object to change the name of a class variable?
Yes.
How is a method called?
Object name on the left, then a period, then method name, then arguments in parentheses
All method calls must have what punctuation?
Parenthesis after them
What has to be true for an object of one class to be cast into an object of another class?
When both classes are related by inheritance, and one is a subclass of the other
You can use an object of a ______ wherever a _______ is expected. (Subclass/superclass) when is the reverse not true?
Subclass, superclass
The reverse is only true if all the needed behavior is available to the superclass. Subclasses often have behavior that superclasses don’t.
In those cases, you must cast explicitly.
What happens when you explicitly cast a superclass object to a subclass? How is this done?
The object gains all the methods and variables that the subclass defines.
If butt is the destination class, and teeth is the object, then
(butt)teeth
Will cast the object to the new class.
Which comparison operators will work for comparing objects? How does this work?
== and !=
They determine if both sides if the operator refer to the same object
How do you see if two different String objects have the same value? How do you see if two String names refer to the same object?
If booger and fart are both String objects, then
booger.equals(fart)
will give either true or false.
booger==fart
will return true or false depending on if they refer to the same object
Are String literals optimized in Java? Discuss.
Yes. This means you must use the new operator to explicitly create a separate string if you want to have two equal strings that are no considered to refer to the same object.
How do you find an object’s class?
If ass is a variable that is assigned to an object, then
String name=ass.getClass().getName();
will give the Class name. getClass returns a Class object that represents the object’s class. That same Class object’s getName method returns a string with the class name.
Code a basic string tokenizer with a default delimiter. How would you change it to a non-default delimiter?
import java.util.StringTokenizer;
class TokenTester {
public static void main(String[ ] arguments){ StringTokenizer vern1;
String quote="Verny verns"; vern1=new StringTokenizer(quote); System.out.println("tk1:"+vern1.nextToken()); System.out.println("tk2:"+vern1.nextToken());
} }
To use non-default delimiter, put a comma after the quote in the new StringTokenizer arguments, then put your desired delimiter in double quotes.