Friday, May 14, 2010

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 Hello.java.
  • 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 ");
        System.out.println("world");
    }
    

  • 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 {
    RED,
    GREEN,
    BLUE
}

public static void main(String[] args) {
    for (SimpleColor sc : SimpleColor.values()) {
        System.out.println(sc);
    }
}

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; ...;)

Trident/4.0
is the important string to determine the real mode and version of IE8.
Java, JavaScript, HTML, XHTML, AJAX, CSS, etc.