Google+

37. 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.

Go through my previous  post#34 Write and execute the Java programs using the TextPad Tool and execute this simple java program explained here and find out whether the correct output is printed as output.




Please comment below to feedback or ask questions.

Integer data types topic will be explained in the next post.




12 comments:

raghavan v said...

Excelent blog.keep it up.
every post excelent.

Arun Motoori said...

@ Raghavan V - Thank You

raghavi said...

class Number
{

public static void main(String args[])
{

int num1,num2;

num1 = 100;
num2 = 200;

System.out.println("This is num1: " + num1);
System.out.println("This is num2: " + num2);

num = num1 + num2;
System.out.print("The value of num1 + num2 is ");
System.out.println(num);

}

}


Please tell me above one is correct?

Arun Motoori said...

@raghavi - Please define the 'num' variable as integer (i.e. instead of num=num1+num2 you have to write int num=num1+num2)

Also you can reduce the last two lines to single print statement -

System.out.print("The sum of num1 and num2 is "+num);




Please find the revised code below -

public class Number {

public static void main(String args[])
{

int num1,num2;

num1 = 100;
num2 = 200;

System.out.println("This is num1: " + num1);
System.out.println("This is num2: " + num2);

int num = num1 + num2;
System.out.print("The value of num1 + num2 is "+num);

}

}

Kalyani said...

Awesome Awesome Awesome blog for any learner..
Simple english and excellent explanation.
All the topics explained in detail with good examples.
Need not to swing around different websites for the concepts of Selenium.
ALL IN ONE BLOG..!! (Selenium-By-Arun)
Great job Arun. Thanks a ton.

nagendrakumar gajavally said...

Hi sir im nagendra i execute Example2 program,but the result shows why?
This is num::100
The value of 100 * 2 is num.

TIJOY MATHEW said...

hi Arun,
Am Just a beginner to JAVA
my doubt is
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);

}

}

My doubt is why in this program why int is not mentioned before the below statement-
num = num * 2;

comment from Raghav you hav mentioned to add int before
num = num1 + num2;

Arun Motoori said...

@Tijoy - In the comment from Raghavi. She was using three variables. num, num1,num2. Out of all the three only num1 and num2 are declared as int. In order to use num variable, we have to declare that as int.

In your program, we didn't use three variables, instead we used only one variable i.e. num. As variables can be declared only once and to be declared before using them, it not required to declare num more than once.

TIJOY MATHEW said...

Ok Understood. Thanks Arun for the immediate Response and Support. Realy Awsome blog!

SHOBHS said...

Hi Arun,

Can we write above code as :

class Example2
{
public static void main(String args[])
{
int num1,num2,num3;
num1=110;
num2=220;
System.out.println("This is num1:" + num1);
System.out.println("This is num2:" + num2);
num3=num1 + num2;
System.out.println("The Value of num1 + num2 is " + num3);
}
}

PratikshaK said...

Hi @SHOBHS - Ans is already mentioned above. you need to define num =num1 +num2 as int num = num1+ num2

Unknown said...

Hi @SHOBS - the code you wrote is correct, once var is define with data type need not to define again.

Blog Index