10/03/2008

Overload

Rules of overload
A type, the number, the order of each argument can be changed.
The access level can be less strict, not stricter.
Access keywords can be changed.
New exceptions can be added.

examples)
void myfunc() {}
void myfunc(int a){}
void myfunc(int a, int b) {}
void myfunc(double a, double b) {}
String myfunc(double a, int b) {return null;}
private void myfunc(File file) throws FileNotFoundException {}

Inheritance and Polymorphism

Sub classes can extend the super class using the keyword extends.

Basic Structure
class Animal {
    ...
}

class Tiger extends Animal {
    ...
}


is-a relation

Animal obj = new Tiger(); --> Tiger is-a Animal

example)

class Animal {
    String getValue() { return "Animal";}
}

class Tiger extends Animal {
    String getValue() { return "Tiger";}
    void func() { System.out.println("Tiger");}
}


Animal obj = new Tiger();

Polymorphism
obj.value cannot reach Anmal's getValue method because the getValue method is overridden in the Tiger class.

Override
In the above example there are two same-name methods. the getValue method in the sub class overrides the getValue method in the super class.

obj is an Animal type object, thus obj cannot access any method in the Tiger class except those overridden methods.
If you want to reach out to those methods in the sub class from the obj, Animal type obj can be casted to Tiger type.
example
(Tiger)obj.func();

10/02/2008

for / while loop

for (declaration; conditions; procedures) {
......
}

declaration --> declare variables varid only in the for block
conditions --> the roup conditions
procedure --> these are done after the body procedure

___

int[] x = {1,2,3,4,5};
for (int i = 0, n = x.length; i < n; i++) {
    System.out.println(x[i]);
}

___

Nested loop

int[][] x = new int[2][];
x[0] = {1,2,3};
x[1] = {11,12,13};
for (int i = 0, n = x.length; i < n; i++) {
    for (int j = 0, m = x[i].length; j < m; j++) {
        System.out.println(x[i][j]);
    }
}


break --> go out of the loop right now
continue --> go to the next iteration right now
return --> go out of the method, returning the value or "nothing".

switch

switch is also used to define conditions.
int value or value that can be casted to int should be used, which are char, short and byte.
Usually break is necessary in each case block.
The default block can be placed first, second or anywhere.

switch (value) {
case 1:
    System.out.println("One");
    break;
case 2:
    System.out.println("Two");
    break;
case 3:
    System.out.println("Three");
    break;
default:
    System.out.println("Others");
}

If / elseif / else

These are for setting conditions in program.

if (condition) {
 ...

} else if (another condition) {
 ...

} else {

...
}

  • The condition statement should return boolean.
___

example

Class

The structure of Java Class

example)

package learn.java.sample;
import java.util.*;

public class SampleClass {

    String str = "Hello";    // String variable
    int x = 100;    // primitive

    /* main method */
    public static void main(String[] args) {
        SampleClass sc = new SampleClass();
        sc.showMsg();
        System.out.println(getValue());
    }
    
    /* method */
    void showMsg() {
        System.out.println(str);
    }

    /* method */
    int getValue() {
        return x;
    }
}
-----

How to make a class (Basic)
  • Declare the name of the package
  • Import necessary packages
  • Define the name of the class
  • Declare instance variables (and initialize them if you want)]
  • Write main method 
What is main method?
main mehtod is the only method where the program thead starts.


10/01/2008

Modifiers

Modifiers are used for classes, methods and variables.

Those are access modifiers for members:

  1. public --> accessible from everywhere
  2. protected --> accessible in the same package and from sub classes in other packages.
  3. (default) -->  accessible in the same package.
  4. private --> accessible in the same class.

Method local variables

If the variables are in the method, then they are called loacal variables.

Basically, local variables are thread safe.

Local variables should always be initialized.

Instance variable / class variable

Variables, which is not inside a method, in a class is called instance variables.
With the keyword static, it is called a class variable, which is not dnamic.

example

Class Sample {

int x = 1;   // instance variable
static int y = 2;  // class variable

public static void main(String[] args) {

Samepl sample = new Sample();

System.out.println(sample.x);
System.out.println(Sample.y);

}
}

How to refer to static variables:
Class name dot variable name

How to refer to dynamic variables:
Object dot variable name

2-dimensional arrays

How to declare 2-dimensional arrays:

int[][] array = new int[3][];

The number of the first dimensional elements must be initialized.
every value of the elements is null.

How to initialize:
array[0] = {1,2,3,4,5};
array[1] = {5,4,3,2,1};
...

or

array[0][0] = 1;

There are a couple of ways.

Arrays

How to declare arrays:

int array = new int[5];
(the length of the array is 5)

How to initialize an array:
There are a couple of ways to initialize an array.

int array = {1,2,3,4,5}; 
 or
int array = new int[5]
array[0] = 1;
...
array[4] = 5;

How to refer to elememts of the array:

array[0]--> the frist element 
array[4] --> the last element of this array


Variables

Primitives

byte (8bits)
short (16bits)
int (32bits)
long (64bits)
float (32bits)
double (64 bits)

A defult value of primitive is 0.

You can put a suffix to double and float, like 1.5d or 1.5f.

boolean / char

boolean --> true or false
char --> character (16bits), defaut \u0000

String

String is an immutable object; actually String is a Java class.

There are a couple of ways to create a String instance.

String str = "Hello world";
String str = new String ("Hello world");

exapmple

String s1 = "Hello";
String s2 = s1;

then s2 and s1 refer to the same "Hello" in the pool.

"Hello" is a String object and s1 refers to the object.

In other words, variables (except primitives) are objects, each of which referes to an instance.