Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, April 16, 2011

Wrapper Classes and Their Constructor Arguments

PrimitiveWrapper Class Constructor Arguments
booleanBooleanboolean or String
charCharacterchar
byteBytebyte or String
shortShortshort or String
intIntegerint or String
longLonglong or String
floatFloatfloat, double, or String
doubleDoubledouble or String

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'

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)

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;
    

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;

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
Java, JavaScript, HTML, XHTML, AJAX, CSS, etc.