Two-Minute Drill


Here are some of the key points from this chapter.

Stack and Heap

q  

Local variables (method variables) live on the stack.

q  

Objects and their instance variables live on the heap.

Literals and Primitive Casting (Objective 1.3)

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'.

Scope (Objectives 1.3 and 7.6)

q  

Scope refers to the lifetime of a variable.

q  

There are four basic scopes:

q  

Static variables live basically as long as their class lives.

q  

Instance variables live as long as their object lives.

q  

Local variables live as long as their method is on the stack; however, if their method invokes another method, they are temporarily unavailable.

q  

Block variables (e.g., in a for or an if) live until the block completes.

Basic Assignments (Objectives 1.3 and 7.6)

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  

Make a reference variable named b, of type Button

q  

Create a new Button object

q  

Assign the Button object to the reference variable b

Using a Variable or Array Element That Is Uninitialized and Unassigned (Objectives 1.3 and 7.6)

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.

Passing Variables into Methods (Objective 7.3)

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.

Array Declaration, Construction, and Initialization (Obj. 1.3)

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).

Initialization Blocks (Objectives 1.3 and 7.6)

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.

Using Wrappers (Objective 3.1)

q  

The wrapper classes correlate to the primitive types.

q  

Wrappers have two main functions:

q  

To wrap primitives so that they can be handled like objects

q  

To provide utility methods for primitives (usually conversions)

q  

The three most important method families are

q  

xxxValue() Takes no arguments, returns a primitive

q  

parseXxx() Takes a String, returns a primitive, throws NFE

q  

valueOf() Takes a String, returns a wrapped object, throws NFE

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.

Boxing (Objective 3.1)

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 = =.

Advanced Overloading (Objectives 1.5 and 5.4)

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.

Garbage Collection (Objective 7.4)

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().




SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net