Java Ch 3 Flashcards

(33 cards)

0
Q

How do you cast back and forth between objects and primitive data types?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

What operator creates instances of a class?

A

new

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you convert a String object (note the capital letter S) to a number like an int or float?

A

The parseInt() class method of the Integer class, or an analogous method. For example,

String yar=”26”;
int penn=Integer.parseInt(pennsylvania);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name the primitive classes that can be converted back and forth to String class. What are these classes called?

A

Boolean, Byte, Character, Double, Float, Integer, Long, Short, Void

Object wrappers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Auto boxing and unboxing?

A

Convert primitive data type to object, and vice versa.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the StringTokenizer class do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the default delimiters used in tokenizing?

A

Spaces, tabs, newlines, returns, or form feed characters. These are automatically used as delimiters if they appear in the string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the 3 things that happen when you use the new operator?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does a constructor do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can a class have multiple constructors?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Discuss TokenTester class.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What happens if a class defines no constructors, and a new instance of the class is created?

A

A constructor with no arguments is called. This constructor simply calls the same constructor in its superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the static command do?

A

Sets a class variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can you use an object to change the name of a class variable?

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How is a method called?

A

Object name on the left, then a period, then method name, then arguments in parentheses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

All method calls must have what punctuation?

A

Parenthesis after them

16
Q

What has to be true for an object of one class to be cast into an object of another class?

A

When both classes are related by inheritance, and one is a subclass of the other

17
Q

You can use an object of a ______ wherever a _______ is expected. (Subclass/superclass) when is the reverse not true?

A

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.

18
Q

What happens when you explicitly cast a superclass object to a subclass? How is this done?

A

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.

19
Q

Which comparison operators will work for comparing objects? How does this work?

A

== and !=

They determine if both sides if the operator refer to the same object

20
Q

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?

A

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

21
Q

Are String literals optimized in Java? Discuss.

A

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.

22
Q

How do you find an object’s class?

A

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.

23
Q

Code a basic string tokenizer with a default delimiter. How would you change it to a non-default delimiter?

A

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.

24
Memory management in Java is ______and _______.
Dynamic automatic
25
How do you get the length of a string?
String name dot length booger.length()
26
How do you get the character at a specific position in a string?
booger.charAt(6) 6 is arbitrary
27
How do you get a substring?
booger.substring(15, 31); Numbers arbitrary
28
How do you get the location of the first appearance of a specific character or character string in a string?
booger. indexOf('q') | fart. indexOf("booger")
29
How do you make a string print as all caps?
farts.toUpperCase()
30
What does Math.max do? What does the opposite?
It can display the highest of a series of ONLY TWO values Math.min
31
How does nesting work in single line statements?
If a method returns am object, you can call methods of that object in the same statement: customer.cancelOrder().fileComplaint();
32
What happens if you set an object value to refer to another object's value, via an = statement, then change the value of the object being referred to?
Both objects have the new value, because of the reference