Jul 4, 2008

SCJP 1.4 Study Notes- Part-2

0)
Superclass Instance Method Superclass Static Method
Subclass Instance Method Overrides Generates a compile-time error
Subclass Static Method Generates a compile-time error Hides

If static is specified in either superclass or subclass for a method the both the other class should also make it static.
1) No variable can be native, abstract or synchronized. Member variables can be transient, final, static, volatile. Local variables can be final only.
2) For same class names, use the fully qualified name. If not done so, the first path in the classpath will be used.
3) const and goto are keywords but are not in use.
4) Memorize all keywords, null, true and false are not keywords.
5) Local objects and primitive variables must be initialized before first use, not necessarily on the first line.
6) Initialized individual elements of an array may be used with some other elements remaining uninitialized.
7) “John” + b is not a String literal.
8) There can be a maximum of 21 digits in a octal number not including the leading 0 and a maximum of 16 digits in a hexadecimal number not including the leading 0x.
9) An array of superclass can accommodate objects of subclass by auto typecasting. But the reverse is not true. Even if explicit cast is done, java.lang.ClassCastException will be thrown at runtime.
10) Interfaces can’t have variables with modifier other than public static and final.
11) An interface can’t implement another interface
12) An interface can’t extend any class
13) An interface can only extend other interfaces. (super interfaces)
14) An abstract class can implement an interface.
15) An abstract class can extend other classes.
16) An abstract class can have final methods.
17) static final variable can be a member variable and needs special treatment than that without static keyword. Since it is static, the initialization in constructor may not work. It will work if the variable has been initialized before the class was loaded. This initialization can be in a static block or when declaring the variable. Once done that it becomes definitely assigned and hence can’t be reassigned. Moreover a final global variable must be assigned while a final local variable may not be.
18) If not initialized while declaring a non-static final variable is expected to be initialized in all the constructors.
19) But if there is an infinite loop in a constructor, then the initialization of definitely unassigned non-static final variable can be avoided.
20) For an infinite loop, the definitely unassigned non-static final variable will be unassigned for first iteration and will be assigned for every iteration afterwards. Hence compiler flashes error in both cases.
21) But since an if(true) is iterated only once, the initialization can’t be avoided as far as constructor is concerned. Predict the case for if(false). Yes, the compiler flashes error.
22) If definitely unassigned non-static final variable is assigned in both if(cond) and corresponding else then compiler comes to know that the variable will be initialized in any case and hence no error.
23) For final primitives, the value may not be altered once it is initialized. For objects, the data within the object may be modified but the reference variable may not be changed.
24) Hashtable and Hashmap are different in the sense that Hashmap is unsynchronized and permits nulls as keys as well as values.
a. In case the iteration performance is important, keep the capacity/size small because the time for iteration is proportional to capacity + size. However get() and put() have constant time performance.
b. The load factor and initial capacity effect the performance of hashmap. When the number of entries in the hashmap exceed the product of load factor and current capacity, the hashmap is rehashed so as to have twice the number of buckets. A value of 0.75 is for load factor is good tradeoff between time and space cost.
c. The expected number of entries should be taken into account when setting the initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the number of entries divided by the load factor, no rehash will ever occur.
d. The parameterized constructors allow for setting the initial capacity and/or load factor.
e. The hashmap is unsynchronized for structural changes. Collections.synchronizedMap should be used for synchronized access.
f. The iterator returned by hashmap is fail-fast which means that if the map is modified by any means other than the iterator’s modification methods, the iterator throws ConcurrentModificationException. However, the fail fast behaviour is not guaranteed.
g. The put() method returns null if there is no mapping for the key or null was stored as the value for that key otherwise it returns the previous value of the key. Similar is the case with get(). The containsKey() method may be used to distinguish between the cases when there is no key with specified name and when null is stored as value for the key.
h. The implementation class which wants to leave out some operation, simply throws UnsupportedOperationException in that method.

25) switch expects an argument of type int. But due to auto casting short, byte, char also work.
26) strictfp used in front of method or class indicates that floating-point numbers will follow FP-strict rules in all expressions.
27) Even the order of arguments matters when overriding a method.
28) Object reference and local variables are stored on stack. IS a relationship is inheritance and HAS a relationship is composition (One class has an object of other as its member)

No comments:

Post a Comment