Stack and Heap:
- Local variables (method variables) live in the stack.
- Objects and their instance variables live on the heap.
- Literals and primitive casting
- Integer literals can be decimal, octal (e.g. 013) or hexadecimal (e.g. 0x3d).
- Literals for long and in L or l.
- Float literals and in F or f, double literals end in a digit Dor d.
- The Boolean literals are tree and false
- Literals for chars are a single character in side single quotes: ‘d’.
Scope:
- Scope refer to life time of a variable.
- There are four basic scopes.
- Static variables live basically as their class lives.
- Instance variables liv as long as their object lives.
- Local variables live as long as their method is on the stach, however, if their method invokes another method, they are temporarily unavailable.
- Stack variables (e.g. in for or an if) live until the block completes.
Basic Assignments:
- Literal integers are logically ints.
- Integer expressions always result in a int-sized result, never smaller.
- Floating point numbers are implicitly double (64 bits).
- Narrowing a primitive truncates the high order bits
- Compound assignments e.g.(+=) perform atomic cast.
- A reference variable holds the bits that are used to refer to an object.
- Reference variable can refer to sub class of the declared type but not super class.
- When creating a new objet, e.g. Button b = new Button() the three things happen:
- Make a reference variable b with type Button.
- Create a new button object
- Assign the Button object to the reference variable b using a variable or array element that is uninitialized an unassigned.
- 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.
- When an array of primitives is instantiated, elements gets default values.
- Instance variables are always initialized with default value.
- Local/ automatic/ method variables are never give a default value. If you attempt to use one before initializing it, you will get a compiler error.
Passing variables into methods:
- Methods can take primitives and/or object reference as arguments.
- Method arguments are always copies.
- Method arguments are never actual objects(they can be reference to objects)
- A primitive argument Is an unattached copy of the original object.
- A reference argument is another copy of a reference to the original object.
- Shadowing occurs when two variables with different scopes share same name, this leads to hard-to-find bugs, and hard-to answer exam questions.
Arrays declaration Construction and Initialization:
- Arrays can hold primitives or objects, but the arrays itself objects.
- When you declare an array the brackets can left or right of the name.
- It’s never legal to include the size of the array in the declaration.
- You must include the size of the array when you construct it (using now) unless you are creating an anonymous array.
- Elements in an array of objects are not automatically created, although primitive array elements are given default values.
- You will 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 Arrays are indexed beginning with zero.
- An ArrayIdexOutofBoundsexception occurs of you use a bad index value.
- Arrays have a length variable whose value is the number of array elements.
- The last index you can access is always one less than the length of the array.
- Multi-dimensional arrays are just arrays of arrays.
- The dimensions in a multidimensional array can have different lengths.
- An array of primitives can accept any value that can be promoted implicitly to the array declare type; e.g. a byte variable can go in an int array.
- An array of objects can hold any object that passes IS-A( or instance of) test for the declared type of the array. For example if Horse extends Animal, then a Horse object can go inti an Animal array.
- If you assign an array to a previously declared array reference, the array youa re ssigning must be the same dimension as the reference you are assigning to
- You can assign an array of one type to previously declared reference of ine of its subtypes, for example a Honda array can be assigned to an array declared a stype Car(asumming Honda extends Car).
Initialization Blocks:
- Static initializer blocks run once, when class is first loaded.
- Instance initialization blocks run every time a new instance ois created. They run after all super- constructors and before the constructor’s code has run.
- If multiple init blocks exist in a class, they a=follow the rules startd above and thery run int ehe order in which they appear int eh source file.
Using Wrappers:
- The wrapper class correlates to the primitive types.
- Wrappers have two main functions
- With wrappers primitives so that they can be handled like objects
- To provide utility methods for primitives (usually conversions)
- The three most important method families are
- xxxValue()takes no arguments, returns a primitive.
- parseXXX() takes a String returns a primitive, throws NPE.
- valueOf() takes a String, returns a warped object, throws NPE wrapper constructors can take a String or a primitive, except or Character which can only take a char.
- Radix refers to bases (typically) other than 10, octal is radix=8, hex= 16.
Boxing:
- As of Java 5, boxing allows you convert primitives to wrappers to convert wraps to primitives automatically.
- Using == with wrappers created through boxing is tricky, those with the same small values
(typically lower than 127) will be == larger values will not be ==.
Advanced Overloading:
- Primitive widening uses the “smallest” method argument possible.
- Used individually, being and var-args are compatible with overloading.
- You CANNOT widen form one wrapper type to another ( IS-A fails).
- You can box then widen. (An int can become an Object, via an Integer).
- You can combine var-args with either widening or boxing.
Garbage collection:
- In java, garbage collection (GC) provides automated memory management.
- The purpose of GC is two delete objects that can’t be reached.
- Only the JVM decides when to run the GC, you can only suggest it.
- You can’t know the GC algorithm for sure.
- Objects must be considered eligible before they can be garbage collected.
- An object eligible when no live thread can reach it.
- To reach an object, you must have an live, reachable reference of that object
- Java applications can run out of memory.
- Island of objects can be GC’ed, even though they refer to each other.
- Request garbage collection with System.gc () or Runtime.getRuntime().gc().
- Class object has a finalize () method.
- The finalize method is guaranteed to run once and only once before the garbage collector deletes an object.
- The garbage collector makes no guarantee finalize () method never run.
- You can initialize an object for GC from within the finalize ().