Chapter 5 – Defining Classes II Flashcards
What are static methods?
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.
What’s a static variable?
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;
What is the default initialization of static variables?
false for booleans, null for classes, and whatever the zero for a given primitive type is.
Do you have to import Math to access its methods?
No.
What do floor and ceil do?
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
Math constants: what does Math have as static constants and how are these different than normal static variables?
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.
What are wrapper classes?
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);
What is the integer class?
The wrapper class for the primitive type int is the predefined class Integer.
What’s boxing?
The process of going from a value of a primitive type to an object of its wrapper class is sometimes called boxing.
What’s unboxing?
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();
What are the other wrapper classes (aside from integer)?
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.
What’s automatic boxing?
Versions of Java past 5.0 have ways of automatically boxing:
>Integer numberOfSamuri = 47;
>Double price = 499.99;
>Character grade = ‘A’;
What’s automatic unboxing?
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 do you use wrapper classes to get largest and smallest values in Java?
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;
What is the parseDouble method?
Converts between a String representation of a double and a double:
>Double.parseDouble(“199.98”); // 199.98
What is the parseInt method?
Converts between a String representation of an int and an int:
>Int.parseInt(“199”); //199
Explain the Character wrapper class methods: toUpperCase, toLowerCase, isUpperCase, isLowerCase, isWhitespace, isLetter, isDigit, isLetterOrDigit.
>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
What methods does Boolean have?
Boolean.TRUE and Boolean.FALSE, which are the Boolean objects corresponding to the values true and false of the primitive type boolean.
Explain secondary and main memory.
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.
What’s a byte?
8 bits.
What’s an address in the context of computer memory?
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.
What’s a memory location?
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.
What is a reference in computer memory? That is, how are primitive types and classes stored differently in memory? And why?
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.
assignment with variables of a class type
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;