Your second Java program

In this chapter you will learn:

  1. How to define variables and store value into them
  2. How to define more variables and separate them with comma
  3. How to code block

A Short Program with a variable

A variable is a memory location that may be assigned a value. The value of a variable is changeable.

The following code defines a variable and change its value by assigning a new value to it.


public class Main {
  public static void main(String args[]) {
    int num; // a variable called num
    num = 100;// ww w  .  ja  va 2s.c o  m

    System.out.println("This is num: " + num);
    num = num * 2;

    System.out.print("The value of num * 2 is ");
    System.out.println(num);
  }
}

When you run this program, you will see the following output:

The following snippet declares an integer variable called num. Java requires that variables must be declared before they can be used.


int num; // this declares a variable called num 

Following is the general form of a variable declaration:


type var-name; 

In the program, the line assigns to num the value 100.


num = 100; // this assigns num the value 100 

Define more than one variable with comma

To declare more than one variable of the specified type, you may use a comma-separated list of variable names.


public class Main {
  public static void main(String args[]) {
    int num, num2;
    num = 100; // assigns num the value 100
    num2 = 200;/*  ww  w. ja  va 2  s .  co m*/
    System.out.println("This is num: " + num);
    System.out.println("This is num2: " + num2);

  }
}

When the program is run, the following output is displayed:

Using Blocks of Code

Java can group two or more statements into blocks of code. Code block is enclosing the statements between opening and closing curly braces({}).

For example, a block can be a target for Java's if and for statements. Consider this if statement:


public class Main {
    public static void main(String args[]) {
        int x, y;
        x = 10;//ww w  .j  a  va 2s  . c  om
        y = 20;
        if (x < y) { // begin a block
            x = y;
            y = 0;
            System.out.println("x=" + x);
            System.out.println("y=" + y);
        } // end of block
    }
}

Here is the output of the code above:

A block of code as the target of a for loop.


public class Main {
  public static void main(String args[]) {
    int i, y;/*from  w  w  w  .  j  ava  2 s  .  com*/
    y = 20;
    for (i = 0; i < 10; i++) { // the target of this loop is a block
      System.out.println("This is i: " + i);
      System.out.println("This is y: " + y);
      y = y - 1;

    }
  }
}

The output generated by this program is shown here:

Next chapter...

What you will learn in the next chapter:

  1. Get to know the full list of keywords in Java
  2. How to use legal identifiers
Home »
  Java Tutorial »
    Java Langauge »
      Java Hello World
Your first Java program
Your second Java program
Java Keywords and Identifiers
Java Variable