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!

They are in China now, I miss them and my wife.

SCJP Exam Watch 8 - Obfuscate

The exam creators will tell you that they're forced to jam tons of code into little spaces "because of the exam engine." While that's partially true, they ALSO like to obfuscate.

The following code:

Animal a = new Dog();
Dog d = (Dog)a;

Can be replaced with this easy-to-read bit of fun:

Animal a = new Dog();

In this case the compiler needs all of those parentheses, otherwise it thinks it's been handed an incomplete statement.

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

Tuesday, May 18, 2010

SCJP Exam Watch 7 - Object Orientation (3)

Don't be fooled by a method that's overloaded but not overridden by a subclass. It's perfectly legal to do the following:

public class Foo {
    void doStuff() {}

class Bar extends Foo {
    void doStuff(String s) {}

The Bar class has two doStuff() methods: the no-arg version it inherits from Foo (and does not override), and the overloaded doStuff(String s) defined in the Bar class. Code with a refrence to a Foo can invoke only the no-arg version. but code with a reference to a Bar can invoke either of the overloaded versions.

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

SCJP Exam Watch 6 - Object Orientation (2)

Be careful to recognize when a method is overloaded rather than overridden. You might see a method that appears to be violating a rule for overriding, but that is actually a legal overload, as follows:

public class Foo {
    public void doStuff(int y, String s) {}
    public void moreThings(int x) {}

class Bar extends Foo {
    public void doStuff(int y, long s) throws IOException {}

It's tempting to see the IOException as the problem, because the overridden doStuff() method doesn't declare an exception, and IOException is checked by the compiler.

But the doStuff() method is not overridden! Subclass Bar overloads the doStuff() method, by varying the argument list, so the IOException is fine.

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

SCJP Exam Watch 5 - Object Orientation (1)

If a method is overridden but you use a polymorphic (supertype) reference to refer to the subtype object with the overriding method, the compiler assumes you're calling the supertype version of the method.

If the supertype version declares a checked exception, but the overriding subtype method does not, the compiler still thinks you are calling a method that declares an exception.

Let's take a look at an example:

class Animal {
    public void eat() throws Exception {
        // throws an Exception

class Dog extends Animal {
    public void eat() {
        /*no Exceptions*/

    public static void main(String[] args) {
        Animal a = new Dog();
        Dog d = new Dog();;        // OK;        // Compiler error - unreported exception

This code will not compile because of the exception declared on the Animal eat() method. This happens even though, at runtime, the eat() method used would be the Dog version, which does not declare the exception.

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

Friday, May 14, 2010

SCJP Exam Watch 4 - Encapsulation

Look out for code that appears to be asking about the behavior of a method, when the problem is actually a lack of encapsulation. Look at the following example, and see if you can figure out what's going on:

class Foo {
    public int left = 9;
    public int right = 3;

    public void setLeft(int leftNum) {
        left = leftNum;
        right = leftNum/3;

    // lots of complex test code here

Now consider this question: Is the value of right always going to be one-third the value of left? It looks like it will, until you realize that users of the Foo class don't need to use the setLeft() method! Thay can simply go straight to the instance variables and change them to any arbitrary int value.

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

SCJP Exam Watch 3 - Array Declaration

It is never legal to include the size of the array in your declaration. Yes, we know you can do that in some other language, which is why you might see a question or two that include code similar to the following:

int[5] scores;

The preceding code won't compile. Remember, the JVM doesn't allocate space until you actually instantiate the array object. That's when size matters.

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

SCJP Exam Watch 2 - Interface Definition

Look for interface difinitions that define constants, but without explicitly using the required modifiers. For example, the following are all identical:

// Looks non-static and non-final, but isn't!
public int x = 1;
// Looks default, non-static, non-final, but isn't!
int x = 1;
// Does't show public or final
static int x = 1;
// Doesn't show public or static
final int x = 1;
// Doesn't show final
public static int x = 1;
// Doesn't show static
public final int x = 1;
// Doesn't show public 
static final int x = 1;
// What you get implicitly
public static final int x = 1;

Any combination of the required (but implicit) modifiers is legal, as is using no moddifiers at all! On the exam, you can expect to see questions you won't be able to answer correctly unless you know, for example, that an interface variable is final and can never be given a value by implementing (or any other) class.

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

SCJP Exam Watch 1 - Identifier

The objective says you have to know legal identifiers only for variable names, but the rules are the same for ALL Java components. So remember that a legal identifier for a variable is also a legal identifier for a method or a class. However, you need to distinguish between legal identifiers and naming conventions, such as the JavaBeans standards, that indicate how a Java component should be names. In other words, you must be able to recognize that an identifier is legal even if it does't confirm to naming standards.

If the exam question is asking about naming conventions - not just whether an identifier will compile - JavaBeans will be mentioned explicitly.

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

Monday, April 5, 2010

Coding Guidelines - Java Programming Fundamentals

  • Your Java programs should always end with the .java extension.
  • Filenames should match the name of your public class. So for example, if the name of your public class is Hello, you should save it in a file called
  • You should write comments in your code explaining what a certain class does, or what a certain method do.

  • In creating blocks, you can place the opening curly brace in line with the statement, like for example,
  • public static void main(String[] args){
    or you can place the curly brace on the next line, like,
    public static void main(String[] args)
  • You should indent the next statements after the start of a block, for example,
  • public static void main(String[] args){
        System.out.println("Hello ");

  • For names of classes, capitalize the first letter of the class name. For names of methods and variables, the first letter of the word should start with a small letter. For example:
  • ThisIsAnExampleOfClassName thisIsAnExampleOfMethodName
  • In case of multi-word identifiers, use capital letters to indicate the start of the word except the first word. For example, charArray, fileName,ClassName.
  • Avoid using underscores at the start of the identifier such as _read or _write.

  • In defining a long value, a lowercase L is not recommended because it is hard to distinguish from the digit 1.

  • It always good to initialize your variables as you declare them.
  • Use descriptive names for your variables. Like for example, if you want to have a variable that contains a grade for a student, name it as, grade and not just some random letters you choose.
  • Declare one variable per line of code. For example, the variable declarations,
  • double exam = 0;
    double quiz = 10;
    double grade = 0;
    is preferred over the declaration,
    double exam = 0, quiz = 10, grade = 0;

Tuesday, March 30, 2010

Comparing Loop Constrcts

  • Use the while loop to iterate indefinitely through statements and to perform the statements zero or more times.
  • Use the do/while loop to iterate indefinitely through statements and to perform the statements one or more times.
  • Use the for loop to step through statements a predefined number of times.

Thursday, March 18, 2010

Enum in Java

The basic using of Enum should be like following:
public enum SimpleColor {

public static void main(String[] args) {
    for (SimpleColor sc : SimpleColor.values()) {

In addition, you may also using it as following:
public enum ComplexColor {
    RED ("Red", "0XFF0000"),
    GREEN ("Green", "0X00FF00"),
    BLUE ("Blue", "0X0000FF");

    private String title;
    private String rgb;

    ComplexColor(String title, String rgb) {
        this.title = title;
        this.rgb = rgb;

    public String title() {
        return title;

    public String rgb() {
        return rgb;

public static void main(String[] args) {
    for (ComplexColor cc : ComplexColor.values()) {
        System.out.println(cc.title() + " - " + cc.rgb());

so, you can have more information on it.

Thursday, February 25, 2010

Best Practice of Java Array Declaration

To declare a array in Java, you have following options:
  1. dataType[] variableName;
  2. dataType variableName[];
  3. dataType []variableName;
The 1st is clearly related to how Java actually works, and you can read it as "dataType array variableName".

The conclusion is:
  • dataType[] variableName;
  • dataType[][]...[] arrayName;

Wednesday, February 3, 2010

IE8 user agent string

IE8 have 3 kinds of browser mode, but give different user agent string

IE7 mode give you Mozilla/4.0 (compatible; MSIE 7.0; ...;)
IE8 mode give you Mozilla/4.0 (compatible; MSIE 8.0; ...; Trident/4.0; ...;)
IE8 compat view mode give you Mozilla/4.0 (compatible; MSIE 7.0; ...; Trident/4.0; ...;)

is the important string to determine the real mode and version of IE8.

Saturday, January 23, 2010

Java Operators

.         []           ()
++        --           !            ~
*          /           %
+         -
<<        >>           >>>         <<<
<         >            <=           >=
==        !=
&         |

Java Data Types

Primitive Data Types
  • boolean (true, false)
  • char (8 bits)
  • byte (8 bits)
  • short (16 bits)
  • int (32 bits)
  • long (64 bits)
  • float (32 bits)
  • double (64 bits)
Reference Variables vs. Primitive Variables
  • Primitive variables are variables with primitive data types. They store data in the actual memory location of the variable is.
  • Reference variables are variables that stores the address in the memory location. It points to another memory location of the actual data is.

Tuesday, January 19, 2010

My First Java Program

public class Hello {
     * My first Java program
    public static void main(String[] args) {
        //prints the string "Hello world!" on screen
        System.out.println("Hello world!");

Error Types
  • Syntax errors
  • Run-time errors
Java Comments
  • C++ Style Comments
  • C-Style Comments
  • Special Javadoc Comments

About Java

What is Java Technology?
  • A programming language
  • A development environment
  • An application environment
  • A deployment environment
Some Features of Java
  • The Java Virtual Machine (JVM)
  • Garbage Collection
  • Code Security

Sunday, January 17, 2010

My new year's resolution

My new year's resolution is learning Java and using Java in my next project.

Go for it!
Java, JavaScript, HTML, XHTML, AJAX, CSS, etc.