Chapter 5 – Defining Classes II Flashcards

1
Q

What are static methods?

A
These methods do not require a calling object. For declaring, add the keyword static:
>public static int coolStaticMethod() { ... }
With a static method, you normally use the class name in place of a calling object. Since it does not need a calling object, a static method cannot refer to an instance variable of the class, nor can it invoke a nonstatic method of the class (unless it creates a new object of the class and uses that object as the calling object). Another way to phrase it is that, in the definition of a static method, you cannot use an instance variable or method that has an implicit or explicit this for a calling object.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s a static variable?

A

A class can have static variables as well as static methods. A static variable is a variable that belongs to the class as a whole and not just to one object. Each object has its own copies of the instance variables. However, with a static variable, there is only one copy of the variable, and all the objects can use this one variable. Thus, a static variable can be used by objects to communicate between the objects. One object can change the static variable, and another object can read that change.

Declaration example:
> private static int SuperSecret;

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

What is the default initialization of static variables?

A

false for booleans, null for classes, and whatever the zero for a given primitive type is.

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

Do you have to import Math to access its methods?

A

No.

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

What do floor and ceil do?

A

These are Math methods that floor or ceiling a result to a double.
>double exact = 7.56;
>int lowEstimate = (int)Math.floor(exact); // 7
>int highEstimate = (int)Math.ceil(exact); // 8

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

Math constants: what does Math have as static constants and how are these different than normal static variables?

A

E and PI, which are e and pi. These are different to normal static variables in that they have the keyword final so that they cannot be altered.

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

What are wrapper classes?

A
Every primitive type has a corresponding wrapper class. A wrapper class allows you to have a class object that corresponds to a value of a primitive type. Wrapper classes also contain a number of useful predefined constants and static methods. Example:
Integer integerObject = new Integer(42);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the integer class?

A

The wrapper class for the primitive type int is the predefined class Integer.

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

What’s boxing?

A

The process of going from a value of a primitive type to an object of its wrapper class is sometimes called boxing.

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

What’s unboxing?

A
The process of going from an object of a wrapper class to the corresponding value of a primitive type is sometimes called unboxing. Example:
>int i = integerObject.intValue();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the other wrapper classes (aside from integer)?

A

The wrapper classes for the primitive types byte, short, long, float, double, and char are Byte, Short, Long, Float, Double, and Character, respectively. The methods for converting from the wrapper class object to the corresponding primitive type are intValue for the class Integer, byteValue for the class Byte, shortValue for the class Short, longValue for the class Long, floatValue for the class Float, doubleValue for the class Double, and charValue for the class Character.

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

What’s automatic boxing?

A

Versions of Java past 5.0 have ways of automatically boxing:
>Integer numberOfSamuri = 47;
>Double price = 499.99;
>Character grade = ‘A’;

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

What’s automatic unboxing?

A
The newer versions of Java also have automatic unboxing:
>Integer numberOfSamuri = new Integer(47); 
>int n = numberOfSamuri;
>Double price = new Double(499.99);
>double d = price;
>Character grade = new Character('A'); 
>char c = grade;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you use wrapper classes to get largest and smallest values in Java?

A
You can use the associated wrapper class to find the value of the largest and smallest values of any of the primitive number types. 
>Integer.MAX_VALUE;
>Integer.MIN_VALUE;
>Double.MAX_VALUE;
>Double.MIN_VALUE;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the parseDouble method?

A

Converts between a String representation of a double and a double:
>Double.parseDouble(“199.98”); // 199.98

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

What is the parseInt method?

A

Converts between a String representation of an int and an int:
>Int.parseInt(“199”); //199

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

Explain the Character wrapper class methods: toUpperCase, toLowerCase, isUpperCase, isLowerCase, isWhitespace, isLetter, isDigit, isLetterOrDigit.

A
>Character.toUpperCase('a') // 'A'
>Character.toLowerCase('A') // 'a'
>Character.isUpperCase('a') // false
>Character.isLowerCase('a') // true
>Character.isWhitespace(' ') // true
>Character.isLetter('5') // false
>Character.isDigit('5') // true
>Character.isLetterOrDigit('&') // false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What methods does Boolean have?

A

Boolean.TRUE and Boolean.FALSE, which are the Boolean objects corresponding to the values true and false of the primitive type boolean.

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

Explain secondary and main memory.

A

A computer has two forms of memory called secondary memory and main memory. The secondary memory is used to hold files for more or less permanent storage. The main memory is used by the computer when it is running a program.

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

What’s a byte?

A

8 bits.

21
Q

What’s an address in the context of computer memory?

A

The number that identifies a byte is called its address. A data item, such as a number or a letter, can be stored in one of these bytes, and the address of the byte is then used to find the data item when it is needed.

22
Q

What’s a memory location?

A

Most data types have values that require more than one byte of storage. When a data type requires more than one byte of storage, several adjacent bytes are used to hold the data item. In this case, the entire chunk of memory that holds the data item is still called a memory location. The address of the first of the bytes that make up this memory location is used as the address for this larger memory location.

23
Q

What is a reference in computer memory? That is, how are primitive types and classes stored differently in memory? And why?

A

a variable of a class type stores only the memory address of where an object is located. The object named by the variable is stored in some other location in memory, and the variable contains only the memory address of where the object is stored. This memory address is called a reference. A value of a primitive type, such as the type int, always requires the same amount of memory to store one value. There is a maximum value of type int, so values of type int have a limit on their size. However, an object of a class type, such as an object of the class String, might be of any size. The memory location for a variable of type String is of a fixed size, so it cannot store an arbitrarily long string. It can, however, store the address of any string because there is a limit to the size of an address.

24
Q

assignment with variables of a class type

A
When you use the assignment operator with variables of a class type, you are assigning a reference (memory address), so the result of the following is to make variable1 and variable2 two names for the same object:
>variable1 = variable2;
25
Q

How does “==” work with variables of a class type?

A

The test for equality using == with variables of a class type also behaves in what may seem like a peculiar way. The operator == does not check that the objects have the same values for their instance variables. It merely checks for equality of memory addresses, so two objects in two different locations in memory would test as being “not equal” when compared using ==, even if their instance variables contain equivalent data.

26
Q

null

A

The constant null is a special constant that may be assigned to a variable of any class type. It is used to indicate that the variable has no “real value.” If the compiler insists that you initialize a variable of a class type and there is no suitable object with which to initialize it, you can use the value null.

27
Q

anonymous object

A

An expression with a new operator and a constructor creates a new object and returns a reference to the object. If this reference is not assigned to a variable, but instead the expression with new and the constructor is used as an argument to some method, then the object produced is called an anonymous object.

28
Q

static import statement

A
These allow you to reference static methods of a class without using the notation class.someStaticMethod(). For example:
>import static java.lang.Character.*;
>//or: import static java.lang.Character.toUpperCase
>toUpperCase('a') // A
29
Q

static import of constants

A

Similar to static import statements but for constants:
>import static java.lang.Math.*;
>PI; // instead of Math.PI

30
Q

class invariant

A

A statement that is always true for every object of the class is called a class invariant. A class invariant can help to define a class in a consistent and organized way.

31
Q

copy constructor

A

A copy constructor is a constructor with one parameter of the same type as the class. A copy constructor should be designed so the object it creates is intuitively an exact copy of its parameter, but a completely independent copy.

32
Q

clone

A

The Java documentation says to use a method named clone instead of a copy constructor because in certain cases copy constructors don’t really work.

33
Q

leaking accessor methods

A
An accessor method that violates the privacy of a class variable. For example, this is safe:
>public Date getBirthDate() {
>    return new Date(born);
>}
But this is a leaking accessor method:
>public Date getBirthDate() //Unsafe {
>    return born; //Not good 
>}
Since it returns a pointer to a private variable, Born, which the user can then change with Born.setDate() (a method that apparently Date objects have).
34
Q

privacy leak

A

A situation that allows a programmer to circumvent the private modifier before an instance variable.

35
Q

leaking mutator methods

A
Mutator methods that allow for privacy leaks. Say, for example:
>public void setBirthDate(Date newDate) {
>    born = new Date(newDate);
>}
This is safe because the user can't alter born by circumventing the privacy of the variable. If instead we used:
>public void setBirthDate(Date newDate) {
>    born = newDate;
>}
Then we could do the following and circumvent the privacy of born:
>Date dateName = new Date("February", 2, 2002); 
>personObject.setBirthDate(dateName);
>dateName.setYear(1000); // this would affect born in the latter case but not in the former.
36
Q

immutable

A

A class that contains no methods (other than constructors) that change any of the data in an object of the class is called an immutable class, and objects of the class are called immutable objects.

37
Q

mutable

A

A class that contains public mutator methods or other public methods, such as input methods, that can change the data in an object of the class is called a mutable class, and objects of the class are called mutable objects.

38
Q

deep copy

A

A deep copy of an object is a copy that, with one exception, has no references in common with the original. The one exception is that references to immutable objects are allowed to be shared (because immutable objects cannot change in any way and so cannot be changed in any undesirable way).

39
Q

shallow copy

A

Any copy that is not a deep copy is called a shallow copy.

40
Q

package

A

A package is Java’s way of forming a library of classes. You can make a package from a group of classes and then use the package of classes in any other class or program you write without the need to move the classes to the directory (folder) in which you are working.

41
Q

import statement

A

You can use a class from a package in any program or class definition by placing an import statement that names the package and the class from the package at the start of the file containing the program (or class definition). The program (or class definition) need not be in the same directory as the classes in the package.

42
Q

CLASSPATH variable

A

A package name must be a path name for the directory that contains the classes in the package, but the package name uses dots in place of \ or / (whichever your operating system uses). When naming the package, use a relative path name that starts from any directory listed in the value of the CLASSPATH (environment) variable.
EXAMPLES:
utilities.numericstuff
java.util

43
Q

default package

A

All the classes in your current directory (that do not belong to some other package) belong to an unnamed package called the default package.

44
Q

current directory

A

The current directory is not any one specific directory; it is the directory in which you are currently “located.” If you do not know what your current directory is, then it is probably the directory that contains the class you are writing.

45
Q

name clash

A

A name clash is a situation in which two classes have the same name. If different programmers writing different packages used the same name for a class, the ambiguity can be resolved by using the package name.

46
Q

fully qualified class name

A

Names that include the package name.

47
Q

javadoc

A

The principles of encapsulation using information hiding say that you should separate the interface of a class (the instructions on how to use the class) from the implementation (the detailed code that tells the computer how the class does its work). Java does not divide a class definition into two files. Instead, Java has the interface and implementation of a class mixed together into a single file. Java comes with a program named javadoc that automatically extracts the interface from a class definition. If your class definition is correctly commented, a programmer using your class need only look at this API (documentation) produced by javadoc.

48
Q

@ tag

A

The special information about parameters and so forth are preceded by the @ symbol and are called @ tags. Some @ tags that are allowed: @param Parameter_Name Parameter_Description @return Description_Of_Value_Returned @throws Exception_Type Explanation @deprecated @see Package_Name.Class_Name @author Author @version Version_Information

Example:
>/**
>@param otherPerson The person being compared to the calling object.
>@return Returns true if the calling object equals otherPerson.
>*/
>public boolean equals(Person otherPerson)

49
Q

deprecated

A

A deprecated method is one that is being phased out. To allow for backward compatibility, the method still works, but it should not be used in new code. There’s a tag for deprecated methods: @deprecated.