Saturday, April 16, 2011

Default Values for Primitives and Refrence Types in Java

Variable TypeDefault Value
Object referencenull (not referencing any object)
byte, short, int, long0
float, double0.0
booleanfalse
char'\u0000'

SCJP Exam Watch 11 - Access instance variable/method from static mathod

One of the mistakes most often made by new Java programmers is attempting to access an instance variable (which means nonstatic variable) from the static main() method (which doesn't know anything about any instances, so it can't access the variable). The following code is an example of illegal access of a nonstatic variable from a static method:

class Foo {
    int x = 3;
    public static void main(String[] args) {
        system.out.println("x is " + x);
    }
}

Understand that this code will never compile, because you can't access a nonstatic (instance) variable from a static method. Just think of the compiler saying, "Hey, I have no idea which Foo object's x variable you're trying to print!". Remember, it's the class running the main() method, not an instance of the class.

Of course, the tricky part for the exam is that the question won't look as obvious as the preceding code. The problem you're being tested for accessing a nonstatic variable from a static method will be buried in code that might appear to be testing something else. For example, the preceding code would be more likely to appear as

class Foo {
    int x = 3;
    float y = 4.3f;
    public static void main(String[] args) {
        for (int z - x; z < ++x; z--, y = y + z) {
            // complicated looping and branching code
        }
    }
}
So, while you're trying to follow the logic, the real issue is that x and y can't be used within main(), because x and y are instance, not static, variables! The same applies for accessing nonstatic methods from a static method. The rule is, a static method of a class can't access a nonstatic (instance) method or variable of its own class.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

Sunday, April 10, 2011

Compiler generated constractor code

class Foo {}
======>
class Foo {
    Foo() {
        super();
    }
}


class Foo {
    Foo() {}
}
======>
class Foo {
    Foo() {
        super();
    }
}


public class Foo {}
======>
public class Foo {
    public Foo() {
        super();
    }
}


class Foo {
    Foo(String s) {}
}
======>
class Foo {
    Foo(String s) {
        super();
    }
}


class Foo {
    Foo(String s) {
        super();
    }
}
======>

Nothing, compiler doesn't need to insert anything.



class Foo {
    void Foo() {}
}
======>
class Foo {
    void Foo() {}
    Foo() {
        super();
    }
}

(void Foo() is a method, not a constructor.)


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

SCJP Exam Watch 10 - Methods return an abstract class or interface

Watch for methods that declare an abstract class or interface return type, and know that any object that passes the IS-A test (in other words, would test true using the instanceof operator) can be returned from that method - for example:

public abstract class Animal {}
public class Bear extends Animal {}
public class Test {
    public Animal go () {
        return new Bear();    // OK, Bear "is-a" Animal
    }
}

This code will compile, the return value is a subtype.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

Saturday, August 28, 2010

SCJP Exam Watch 9 - Class and Interface Declaration

Look for illegal uses of extends and implements.  The following shows examples of legal and illegal class and interface declarations:


class Foo {}                        // OK
class Bar implements Foo {}         // No! Can't implement a class
interface Baz {}                    // OK
interface Fi {}                     // OK
interface Fee implements Baz {}     // No! Interface can't implement an interface
interface Zee implements Foo {}     // No! Interface can't implement a class
interface Zoo extends Foo {}        // No! Interface can't extend a class
interface Boo extends Fi {}         // OK
class Toon extends Foo, Button {}   // No! Class can't extend multiple classes
class Zoom implements Fi, Baz {}    // OK
interface Vroom extends Fi, Baz {}  // OK
class Yow extends Foo implements Fi {}    // OK


Burn these in, and watch for abuses in the questions you get on the exam.  Regardless of what the question appears to be testing, the real problem might be the class or interface declaration.  Before you get caught up in, say, tracing a complex threading flow, check to see if the code will even compile.


Refrence: SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065)

Thursday, August 26, 2010

My sons

My sons picture!
Great!


They are in China now, I miss them and my wife.
Java, JavaScript, HTML, XHTML, AJAX, CSS, etc.