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.