Here are some of the key points from this chapter.
q | Local variables (method variables) live on the stack. |
q | Objects and their instance variables live on the heap. |
q | Integer literals can be decimal, octal (e.g. 013), or hexadecimal (e.g. 0x3d). |
q | Literals for longs end in L or 1. |
q | Float literals end in F or f, double literals end in a digit or D or d. |
q | The boolean literals are true and false. |
q | Literals for chars are a single character inside single quotes: 'd'. |
q | Scope refers to the lifetime of a variable. | ||||||||
q | There are four basic scopes:
|
q | Literal integers are implicitly ints. | ||||||
q | Integer expressions always result in an int-sized result, never smaller. | ||||||
q | Floating-point numbers are implicitly doubles (64 bits). | ||||||
q | Narrowing a primitive truncates the high order bits. | ||||||
q | Compound assignments (e.g. +=), perform an automatic cast. | ||||||
q | A reference variable holds the bits that are used to refer to an object. | ||||||
q | Reference variables can refer to subclasses of the declared type but not to superclasses. | ||||||
q | When creating a new object, e.g., Button b = new Button();, three things happen:
|
q | When an array of objects is instantiated, objects within the array are not instantiated automatically, but all the references get the default value of null. |
q | When an array of primitives is instantiated, elements get default values. |
q | Instance variables are always initialized with a default value. |
q | Local/aufomatic/method variables are never given a default value. If you attempt to use one before initializing it, you'll get a compiler error. |
q | Methods can take primitives and/or object references as arguments. |
q | Method arguments are always copies. |
q | Method arguments are never actual objects (they can be references to objects). |
q | A primitive argument is an unattached copy of the original primitive. |
q | A reference argument is another copy of a reference to the original object. |
q | Shadowing occurs when two variables with different scopes share the same name. This leads to hard-to-find bugs, and hard-to-answer exam questions. |
q | Arrays can hold primitives or objects, but the array itself is always an object. |
q | When you declare an array, the brackets can be left or right of the name. |
q | It is never legal to include the size of an array in the declaration. |
q | You must include the size of an array when you construct it (using new) unless you are creating an anonymous array. |
q | Elements in an array of objects are not automatically created, although primitive array elements are given default values. |
q | You'll get a NullPointerException if you try to use an array element in an object array, if that element does not refer to a real object. |
q | Arrays are indexed beginning with zero. |
q | An ArraylndexOutOfBoundsException occurs if you use a bad index value. |
q | Arrays have a length variable whose value is the number of array elements. |
q | The last index you can access is always one less than the length of the array. |
q | Multidimensional arrays are just arrays of arrays. |
q | The dimensions in a multidimensional array can have different lengths. |
q | An array of primitives can accept any value that can be promoted implicitly to the array's declared type;. e.g., a byte variable can go in an int array. |
q | An array of objects can hold any object that passes the IS-A (or instanceof) test for the declared type of the array. For example, if Horse extends Animal, then a Horse object can go into an Animal array. |
q | If you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the reference you're assigning it to. |
q | You can assign an array of one type to a previously declared array reference of one of its supertypes. For example, a Honda array can be assigned to an array declared as type Car (assuming Honda extends Car). |
q | Static initialization blocks run once, when the class is first loaded. |
q | Instance initialization blocks run every time a new instance is created. They run after all super-constructors and before the constructor's code has run. |
q | If multiple init blocks exist in a class, they follow the rules stated above, AND they run in the order in which they appear in the source file. |
q | The wrapper classes correlate to the primitive types. | ||||||
q | Wrappers have two main functions:
| ||||||
q | The three most important method families are
| ||||||
q | Wrapper constructors can take a String or a primitive, except for Character, which can only take a char. | ||||||
q | Radix refers to bases (typically) other than 10; octal is radix = 8, hex = 16. |
q | As of Java 5, boxing allows you to convert primitives to wrappers or to convert wrappers to primitives automatically. |
q | Using = = with wrappers is tricky; wrappers with the same small values (typically lower than 127), will be = =, larger values will not be = =. |
q | Primitive widening uses the "smallest" method argument possible. |
q | Used individually, boxing and var-args are compatible with overloading. |
q | You CANNOT widen from one wrapper type to another. (IS-A fails.) |
q | You CANNOT widen and then box. (An int can't become a Long.) |
q | You can box and then widen. (An int can become an Object, via an Integer.) |
q | You can combine var-args with either widening or boxing. |
q | In Java, garbage collection (GC) provides automated memory management. |
q | The purpose of GC is to delete objects that can't be reached. |
q | Only the JVM decides when to run the GC, you can only suggest it. |
q | You can't know the GC algorithm for sure. |
q | Objects must be considered eligible before they can be garbage collected. |
q | An object is eligible when no live thread can reach it. |
q | To reach an object, you must have a live, reachable reference to that object. |
q | Java applications can run out of memory. |
q | Islands of objects can be GCed, even though they refer to each other. |
q | Request garbage collection with System.gc(); (recommended). |
q | Class Object has a finalize() method. |
q | The finalize() method is guaranteed to run once and only once before the garbage collector deletes an object. |
q | The garbage collector makes no guarantees, finalize() may never run. |
q | You can uneligibilize an object for GC from within finalize(). |