Thursday, April 1, 2010

My preparation notes for SCJP 6

These are the notes I made while preparing for SCJP exam. Not very structured, but these were the key points I wanted to cram right before the exam.
Google Docs link, PDF link
Enjoy!

Java keywords : abstract, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, extends, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while, assert, enum.

Identifiers can start with letter, currency characters ($,€, etc.), _. Can contain number, cannot start with number.

Beans 
Getter : getPropertyName, for booleans isPropertyName.
Listener : addMyListener, removeMyListener

Interfaces 
All methods are forcefully abstract and public.
All variables are public static and final, i.e. you can only declare constants in an interface, no instance variables.
Methods cannot be static, strictfp, final or native (because they are abstract). public modifies is required, because it can also be default package access.

Protected members and methods have package-level access as well as parent-child access. Default members and methods have only package access.

Argument: The things you specify between the parenthesis when you are invoking a method.
Parameters: The things in method's signature that indicate what the method must receive when it is invoked.

strictfp - in methods causes floating point to adhere to IEEE 754 standard, no matter the underlying platform.

Varargs : must be last parameter and there can be only on var-arg in a method.
Examples:
Legal
void doStuff(int... x); // 0 to many ints as parameter
void doStuff(char c, int... x):
void doStuff(Animal... a);
Illegal
void doStuff(int x...);
void doStuff(int... x, char... y);
void doStuff(String... s, byte b);

Constructors cannot be static, final or abstract.

Data types:
byte - 8 bits
short - 16 bits
int - 32 bits
long - 64 bits
float - 32 bits
double - 64 bits
char - 16 bits
size of boolean is VM dependent.
sign bit is leftmost bit.

Instance variables cannot be marked static, because then they become class variables.

Local variables can only have final as modifier. They have to be initialized before attempted to be used. Otherwise there will be a compile error because local variables are not initialized to their default values, unlike instance variables.

public class A{
    int in;
    public A(int in) {
        in = in; // no effect
        this.in = in; // will do what we assumed
    }
}

If you mark an instance variable transient, you tell JVM to skip it when you serialize the object.

Volatile modifier tells JVM that a thread accessing it must reconcile its own private copy of the variable with the master copy in memory.

Static methods and variables.
Things you can make static:
- Methods
- Variables
- A class nested within another class, but not withing a method.
- Initialization blocks.
Things you cannot make static:
- Constructors
- Classes (unless they are nested)
- Interfaces
- Method local inner classes.
- Inner class methods and instance variables.
- Local variables

You cannot declare enums in a method.

Java history (what appeared in which version) :
enums - java 5
generics - java 5
var-args - java 5
function to return subtype of return type - java 5
using Boolean in if(Boolean) - java 5
switch evaluate enum - java 6
assertions - java 4

final and volatile modifiers don't go together.

Java objects that pass more than one IS-A test are polymorphic, so all classes except Object are polymorphic.

On runtime only instance methods are dynamically invoked, not variables or static methods. 

Overloading method can return subtype since java 5.

Object casting can look up multiple inheritance tree levels.

Casting interfaces always works until class is not final

Coupling
If A knows about the way B was implemented, its tight coupling, which should be avoided. The looser coupling is, the better it is.
Cohesion is a degree of how a class has a single well-focused purpose. The higher the cohesion, the better it is.

Literals 
Octal literals : int nine = 011; // zero in front!
Hex : 0xDeadCaFe
Long: long a = 55L; // L or l at the end
Floating point literals are double by default, so if you want them to be float, add F/f to the end.
Literal integer is always int by default.
Calculation of int-or-small expressions is always int

Primitive casting:
float -> int - explicit
int -> double - implicit
lond -> double - implicit
double -> int - explicit, all digits after dot are lost.

long l = 130L;
byte b = (byte) l;
System.out.println(b); //prints -126

byte = 3; b+=7;
compound assignment operations cast automatically.

Array elements are always initialized to their default values.

Multi-dimensional arrays = multi-objects.

primitive arrays cannot be reassigned to different type, object arrays can:
int a[] = new char[2]; // NO
Object objs[] = new String[4]; // Ok

Wrappers are IMMUTABLE.

Except char, all wrappers have 2 constructors.
Integer i1 = new Integer(42);
Integer i2 = new Integer("42");
For Boolean, "true" in all cases is true, else false.

Methods of wrappers:
primitive xxxValue() - convert wrapper to primitive
primitive parseXxx(String) - convert String to primitive
Wrapper valueOf(String) - convert String to Wrapper

Following wrapper object pass == test:
Boolean
Byte
Character from \u0000 to \u007f (127).
Short and Integer from -128 to 127

Argument matching while overloading:
Widening beats boxing
Widening beats var-args
Boxing beats var-args
Wrappers don't widen!
You can't widen and box
Yo can unbox and widen.
These are made in a way that old code would not be behaving differently (widening is older concept, so it beats boxing and var-args which are newer)

Signature of finalize method : protected void finalize() throws Throwable {}

instanceof operation on null is always false.

switch can evaluate : short, int, char, byte, enum
cannot evaluate : long, float, double, Objects
case has to be compile-time constant, so :
final int a = 1; // Ok for using in case
final int b;
b = 1; // NOT Ok for using in case
if enum is used, you need only the name of constant, not the name of enum. So MY_ENUM_INSTANCE instead of MyEnum.MY_ENUM_INSTANCE.

byte b = 2;
switch (b) {
case 23:
case 128: // compile error, compiler knows that 128 is not a byte.
}

If a directory is missing, file creation will throw exception

You can't delete a directory if it is not empty. You can rename it though.

renameTo takes File as an argument.

File.list() returns array of Strings.

Console.readLine("%s", "input:"); // just syntax memo

Console.readPassword returns char array.

Object o;
os.writeObject(o);
o = /*cast necessary here if not Object*/ is.readObject();

java.io.NotSerializableException is thrown at runtime if object is not serializable.

serialization method signatures:
private void writeObject(ObjectOutputStream os) {
    os.defaultWriteObject();
}
private void readObject(ObjectInputStream is) {
    is.defaultReadObject();
}

regular expressions:
\d digit
\s space
\w letter, digit, _

subset, submap, tailmap, etc. by default INCLUDE. And all of these methods are creating BACKED collections!

Collections.reverserOrder(/*optionally Comparator here*/) returns reverse Comparator.

wait, notify, notifyAll have to be invoked only from synchronized context, where you have lock for an object.

Garbage collection: while counting how many objects get garbage collected, pay attention to what is inside classes.

Array casting - exception, not a compile error.

System.out.println() on enum gives its reference, not name.

class A{
    {
        index = 4; // this works, no compile error
    }
    int index;
}

equals for wrappers return true only if types are equal too.

PriorityQueue :
add() and offer() do the same.
peek() returns first value
pool() returns and removes.

float calculations result in float.
TreeSet - watch for objects inserted there to be Comparable!

For anonymous inner classes, ";" must come right after }, otherwise compile error will be in the same line as }

In order for main() method to use inner class it must be static, because main is also static! If inner class is not static, you need to make an instance of outer class. <-- watch for this in exam.

javac has no -D option.

No comments:

Post a Comment