Getting familiar with Java: Adding numbers

The first and the foremost question that comes into mind is that why did Java became famous. It become famous because it is a platform independent language. Most of other languages such as C or C++ once the language written inside the environment then it depends upon the corresponding operating system such as Windows to run its compiler from it system instances. Java does not needs so and in fact for advance java users the program can be run from it dos command too.

So C or C++ codes can not be compiled with some other operating system, so it is dependable with operating system. Where as, Java is platform independent as it has its own java virtual machines and the compiler runs with JVM, and it is not dependable with different operating systems.

 

package day1.examples;



public class MyFirstClass {



                public static void main(String[] args) {

                                // TODO Auto-generated method stub

           System.out.println("Hello World");

           // My first statement

                }



}

 

 

In this tutorial we will be learning in depth about how to add numbers to Java program. In the previous example suppose we will like to add two numbers. Then, we will have to declare those two numbers.

 

Here, we can write this as:

int x= 10;

int y= 20;

Integer datatypes can hold any whole number:

Here, we have declared inside Java these two numbers let us for example here we can call this as int for integer x and its value as 10 and integer y and its value as 20 and at the end of each statement the semicolon marks the end of statement in both these cases. Here, x and y are examples of variables. Variables can hold some data or values. Int stands for integer and it is called as data types and it can hold any number. Always remember that integer can hold whole numbers. Whole numbers are simply the numbers beginning from o,1,2,3 and so on. But do remember that numbers like 1/1 or 1.1 are not whole numbers.

Now, ours new Java code after adding integer x and y is as follows.

There are various examples of data types and integer is one of them. You can ask yourself if there is any instances in which I wish to hold character data types then how to do this. Then questions arises what is about such numbers as well as that of character data types that to be included in Java programming.  Here we can add another integer such as:

int c = x+y;

or to be precise we can write as

int result = x+y;

So ours new code will be like as below:

 

package day1.examples;



public class MyFirstClass {



                public static void main(String[] args) {

                                // TODO Auto-generated method stub

           System.out.println("Hello World");

           // My first statement

          

           int x = 10;

           int y = 20;

           int result = x + y;

           System.out.println(result);

                }



}

 

Here, in order to print out the result of integer we have to write down system.out.println. There is a short cut to write it out. In the eclipse just write syso and then press the control button and then hit space button and then after some seconds the system.out.println will be visible and then press enter to write that on eclipse and then within bracket put the integer type and here it is result and then save that code on Eclipse.  In the previous post I have told you how to run the java application. In the coding space any where right click on it and then run as and then click on Java application to see the console window where you will find the two println functions such as that of previous one that is of hello world and the later one that of integer which is the result of two integer that is 30.

As we have provide value to integer x as 10 and integer y as 20 and then the third one is also integer as the it is about result of these two that is integer result will be equal to that of x plus y that is 30.

 

From this java example so far we come to understand that it is println functions that provides the outout and we learn about how to write down the short cut of println as well as we learn about data types of integer and the limit of integer as it only can contain the whole numbers. Suppose in the console out put, you instead of just 30 as the result as in the previous case you want to write down result = 30 and for this you need to concatenation so that this will be done easily. For this we have to modify the system.out println code and this will be like this.

 

System.out.println(“result= “+result);

 

Here, we have used the concotation methods, where the result = is with inverted comma and this means it will be seen in the console out put and then we write down +result which is the symbol of providing the integer result output.  Save this code and then within code window right click at anywhere and then click on run as and then java application and this will lead to show in the console window instead of only the integer as result this will show

Result = 30

When we try to analyse this code we could see that we have written system.out println code where the within bracket and then inside of it within inverted comma we have result = and this is visible in console out put window and then we have +  result which means the addition of int x and int y to be added and the result will be there at the integer type. As we know in order to show as it is in print or out put functions we need to have inverted comma and then we include inverted comma into “result =” and then we need to put the result in whole number that is with integer. In this way through the methods of concatenation we could find that the integer number is now on show. Try this out find out what is the main function and it is important to remember these methods so that ultimately while writing these codes it will be easier for us to understand the series of events that follows.

This entry was posted in Java on by .

Modified: 26th Oct 2017

About Mohan Manohar Mekap

Mohan Manohar is a blogger from India who founded Ittech back in 2007. He is passionate about all things tech and knows the Internet and computers like the back of his hand.