synchronized code

September 24, 2006 by Krishnamoorthy Sethuraman

synchronization possible on objects only and not on primitives

int x;

synchronized(x) will be compiler error. No autoboxing here…

Assign String to String Buffer

September 24, 2006 by Krishnamoorthy Sethuraman

StringBuffer sb = “hello”;

this won’t compile. StringBuffer have to be explicity created by new operator.

Ambiguous Match

September 24, 2006 by Krishnamoorthy Sethuraman

When class implements two or more interface(or extends classes) containing same member varaible type(say, int i). Referencing this variable in the new class will cause ambiguous match for compiler as long as that variable is not redefined in the new class. Redefining the variable in new class will resolve the issue.

Method of private inner class : Accessibility

September 24, 2006 by Krishnamoorthy Sethuraman

Not possible to access methods of private inner class as the following code will give compile error.

class MyOuter{
private class MyInner{
public float f(){return 1.2f;}
}
public MyInner getMyInner(){
return new MyInner();
}

}

public class Test13{
public static void main(String... args){
MyOuter o = new MyOuter();
float f = new MyOuter().getMyInner().f();
}
}

Format string conversion

September 24, 2006 by Krishnamoorthy Sethuraman

For %b(boolean) : All data types can be passed

For %c(char) : byte, char, short, int

For %d(int) : byte, short, int, long (No char)

For %f(float) : only float and double

For %s(string) : everything

=======================================

public class Test12{
public static void main(String… args){
boolean b = false;
byte by = 2;
char c = ‘A’;
short s = 7;
int i = 35565;
long l = 2345676436323746l;
float f = 234.567f;
double d = 234.678;
//System.out.printf(“%d\t”, b);
System.out.printf(“%d\t”, by);
//System.out.printf(“%d\t”, c);
System.out.printf(“%d\t”, s);
System.out.printf(“%d\t”, i);
System.out.printf(“%d\t”, l);
//System.out.printf(“%d\t”, f);
//System.out.printf(“%d\t”, d);
System.out.println();
System.out.printf(“%b\t”, b);
System.out.printf(“%b\t”, by);
System.out.printf(“%b\t”, c);
System.out.printf(“%b\t”, s);
System.out.printf(“%b\t”, i);
System.out.printf(“%b\t”, l);
System.out.printf(“%b\t”, f);
System.out.printf(“%b\t”, d);
System.out.println();
System.out.printf(“%s\t”, b);
System.out.printf(“%s\t”, by);
System.out.printf(“%s\t”, c);
System.out.printf(“%s\t”, s);
System.out.printf(“%s\t”, i);
System.out.printf(“%s\t”, l);
System.out.printf(“%s\t”, f);
System.out.printf(“%s\t”, d);
System.out.println();
//System.out.printf(“%f\t”, b);
//System.out.printf(“%f\t”, by);
//System.out.printf(“%f\t”, c);
//System.out.printf(“%f\t”, s);
//System.out.printf(“%f\t”, i);
//System.out.printf(“%f\t”, l);
System.out.printf(“%f\t”, f);
System.out.printf(“%f\t”, d);
System.out.println();
//System.out.printf(“%c\t”, b);
System.out.printf(“%c\t”, by);
System.out.printf(“%c\t”, c);
System.out.printf(“%c\t”, s);
System.out.printf(“%c\t”, i);
//System.out.printf(“%c\t”, l);
//System.out.printf(“%c\t”, f);
//System.out.printf(“%c\t”, d);
}
}

========================

Output:

2    7    35565    2345676436323746
false    true    true    true    true    true    true    true
false    2    A    7    35565    2345676436323746    234.567    234.678
234.567001    234.678000
    A        ?

OO Encapsulation

September 23, 2006 by Krishnamoorthy Sethuraman

Advantages of encapsulation:

  1. Reusability of code
  2. Code Clarity

Some more SCJP5 tips

September 22, 2006 by Krishnamoorthy Sethuraman
  1. enum declaration outside class cannot contain static, final or any access modifiers, although enum type is implicity static and final. However enum declaration inside class can be declared as static(although implicitly static) but explicit declaration as final will be compiler error. Explicit declaration of static or final for enums outside class is also compiler error.
  2. Two special cases where primitive == doesn’t match the object equals method. NaN and +0.0f with -0.0f. For first case, primitive == is false(so equals will return true) and for second case primitive == is true(so equals will return false).
  3. final instance variables need to get initialized before the constructor ends.(else compiler error).
  4. Protected member inherited to subclass can be accessed in the subclass by a member of declared type as subclass but not by a member of declared type as superclass. Inherited protected member will not be accessible by other classes in the same package of subclass(unless they also extend the subclass).
  5. ArrayStoreException : Storing a object in a array of another object type. E.g., Object[] a = new String[5]; a[0] = new Integer(1); here storing a integer in a array object(whose runtime type is String array causes Heap Pollution).
  6. Compile type determines which overloaded method to be invoked. Run time type determine which overridden method to be invoked.
  7. Constructor is never inherited and hence no overriding. static methods are inherited but can not be overridden.
  8. result of expression is integer. so, byte b = 7+3; will be compile error. Needs explicit cast. However for compound assignment, explicit cast is not required. Thus, byte b = 127; b += 3; will run fine and will give output as -126.
  9. local variable initialization before use. Even null check on a local variable cannot be done before the local variable is initially set as null. Thus, Date d; if(d == null) will cause variable not initialized error. Initialization within if condition or for loop which the compiler is not certain will be executed and then accessing the variable later down the line will also cause variable not initialized error.
  10. When initializing multi-dimension array, the second size can be omitted. However, before assigning the elements into the second dimension array, it has to be initialized with the size of the second dimension. i.e., each of the first dimension array should be assigned a new array object with size specified in the new constructor.
  11. auto-unboxing may result in NPE. e.g., passing wrapper objects to method which expects a primitive and that the wrapper object passed is a instance member variable not initialized.
  12. overloading between methods is fine between one which takes a primitive and other which takes the wrapper of primitive.
  13. In switch statement, case labels should be compile time constants(final variable should be initialized in same line when declared). Duplicate case label will be a compile time error. case label constants should be within the range of the switch argument otherwise compile error.

Oracle Collections

September 15, 2006 by Krishnamoorthy Sethuraman

== and equals

September 11, 2006 by Krishnamoorthy Sethuraman

Object equals checks both objects are of same type and their contents are equal. In case of Float/Double:

two instances of class Double, d1 and d2, the value of d1.equals(d2) is true if and only if

d1.doubleValue() == d2.doubleValue()

Same holds good in case of Float also.

Exceptions:

  1. If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.
  2. If d1 represents +0.0 while d2 represents -0.0, or vice versa, the equal test has the value false, even though +0.0==-0.0 has the value true.

Array and ArrayList

September 11, 2006 by Krishnamoorthy Sethuraman

In a arrayList of object, any types of object(mixed) can be put. However in an array of objects, once initialized to a particular object type, other type of objects cannot be put.

Thus,

ArrayList a = new ArrayList();
a.add(“krishna”);
a.add(5);

is allowed and will run fine.

However,

Object[] obj = new String[5];
obj[0] = new Integer(1);

will throw ArrayStoreException(is a RunTimeException).

With generics, arraylist is also not allowed to store Integer if it is declared as ArrayList of String. Thus, with generics,
ArrayList<String> a = new ArrayList<String>();
a.add(“krishna”);
a.add(5);  — will be compiler error(unlike array which is at rutime).

But with generics also, one more difference is array  allows polymorphic assignment., i.e.,

Number[] n = new Integer[5];

However, in case of generics, polymorphic assignment holds good only for the base

type and not for the generic type. i.e.,

List<Integer> l = new ArrayList<Integer>;  — for base type hold good

ArrayList<Number> n = new ArrayList<Integer>; — not allowed for generic type