-------------------------------------- Java Sag is under construction
Please inform us if you find a non good advertisement : info.enim dot gmail.com

Best Web Hosting

mercredi 9 mai 2012

How do I use the this keyword in Java?

The program below demonstrate how to use the switch statement. The switch statement can work with thebyteshortint and char primitive data types and the corresponding wrappers of these data type such asByteShortInteger and Character. It also work with work with enumerated types, refer to the following example: How do I use enum in switch statement?.
The switch block or the body can contains one or more case or default labels. The switch statement evaluates its expression and evaluate the appropriate case.
You'll also notice that after each case labels we have a break statement. This break statement causes the program execution to continue outside the switch block. Without using a break the case will fall-through to another case or default label.
package org.kodejava.example.lang;

public class SwitchDemo {
    public static void main(String[] args) {
        System.out.println("The Planets");
        System.out.println("===================================");
        System.out.println("1. Mercury");
        System.out.println("2. Venus");
        System.out.println("3. The Earth");
        System.out.println("4. Mars");
        System.out.println("5. Jupiter");
        System.out.println("6. Saturn");
        System.out.println("7. Uranus");
        System.out.println("8. Neptune");
        System.out.println("");
        System.out.print("Please choose your favorite destination: ");

        int destionation = 0;
        try {
            destionation = Integer.valueOf(System.console().readLine());
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        System.out.print("Welcome to ");
        switch (destionation) {
            case 1:
                System.out.println("Mercury"); break;
            case 2:
                System.out.println("Venus"); break;
            case 3:
                System.out.println("The Earth"); break;
            case 4:
                System.out.println("Mars"); break;
            case 5:
                System.out.println("Jupiter"); break;
            case 6:
                System.out.println("Saturn"); break;
            case 7:
                System.out.println("Uranus"); break;
            case 8:
                System.out.println("Neptune"); break;
            default:
                System.out.println("Invalid Destination");
        }
    }
}

When you run the program you'll have to following on the screen:

The Planets
===================================
1. Mercury
2. Venus
3. The Earth
4. Mars
5. Jupiter
6. Saturn
7. Uranus
8. Neptune

Please choose your favorite destination: 3
Welcome to The Earth

How do I define a constant variable?

To define a constant in Java, use final modifier which combined with static modifier. The final modifier indicates that the value of this field cannot change.

If you change the value of the constant, you need to recompile the class to get the current value. Other feature in Java that provide similar functionality is enumeration (a list of named constants). You can simply create an enumeration by using the enum keyword.
package org.kodejava.example.fundametal;

public class ConstantDemo {
    public static void main(String[] args) {
        int sunday = DayConstant.SUNDAY;
        System.out.println("Sunday= " + sunday);

        String dozen = MeasureConstant.DOZEN;
        System.out.println("Dozen: " + dozen);
    }

}

class DayConstant {
    public final static int SUNDAY = 0;
    public final static int MONDAY = 1;
    public final static int TUESDAY = 2;
    public final static int WEDNESDAY = 3;
    public final static int THURSDAY = 4;
    public final static int FRIDAY = 5;
    public final static int SATURDAY = 6;
}

class MeasureConstant {
    final static String UNIT = "unit";
    final static String DOZEN = "dozen";
}

How do I use the return keyword in Java?

The return keyword is used to return from a method when its execution is complete. When a return statement is reached in a method, the program returns to the code that invoked it.

A method can return a value or reference type or does not return a value. If a method does not return a value, the method must be declared void and it doesn't need to contain a return statement.

If a method declare to return a value, then it must use the return statement within the body of method. The data type of the return value must match the method's declared return type.
package org.kodejava.example.fundametal;

public class ReturnDemo {

    public static void main(String[] args) {
        int z = ReturnDemo.calculate(2, 3);
        System.out.println("z = " + z);

        Dog dog = new Dog("Spaniel", "Doggie");
        System.out.println(dog.getDog());
    }

    public static int calculate(int x, int y) {
        //
        // return an int type value
        //
        return x + y;
    }

    public void print(){
        System.out.println("void method");

        //
        // it does not need to contain a return statement, but it
        // may do so
        //
        return;
    }

    public String getString(){
        return "return String type value";

        //
        // try to execute a statement after return a value will
        // cause a compile-time error.
        //
        String error = "error";
    }
}

class Dog {
    private String breed;
    private String name;

    Dog(String breed, String name) {
        this.breed = breed;
        this.name = name;
    }

    public Dog getDog() {
        //
        // return Dog type
        //
        return this;
    }

    public String toString(){
        return "breed: " + breed.concat("name: " + name);
    }
}

Best Web Hosting