Second Simple Java Program

First lets understand the concept of Variables followed by writing the second simple Java program.


Variables

Variable is a named memory locations that can be assigned a value by your program.

Example of a variable in Java Programming Language:

int num = 5;

Here num is a variable name to which the value 5 is assigned. And also this variable name is declared as int to store integer values like 5.

Second Simple Java Program

    /*
         Here is another short example.
         Call this file 'Example2.java'.
   */

    class Example2
    {

           public static void main(String args[])
           {

                int num;   // This statement declares the variable called num as integer variable.
           
                num = 100; // This statement assigns the value 100 to the integer variable num.
           
                System.out.println("This is num: " + num);  //This prints the current holding value of num variable 
 
                num = num * 2;  // This statement multiplies the value assigned to the num integer variable by 2. i.e. num = 100 * 2. So after multiplications the resultant value will be stored in num (i.e. num will be holding 200 after Java executing this statement)
           
                System.out.print("The value of num * 2 is ");
                System.out.println(num);  

            }

    }

Output of this program

This is num: 100  
The value of num*2 is 200

Observe that though we have three print statements, we got the output for only two. This is because the second print statement is using print( ) method instead of println( ) method to print the output. Hence at the output console the third print statement will get printed in the same line of the second print statements output. In order to have the all the statements in this program to print the outputs in the separate lines we have to use println( ) method instead of print( ) statement in the second print statement.