Wednesday, August 17, 2016

Muhammad Akbar

The Concept of Data Types-An Over Views-Best Education Service

data types in java

[Tutorial 6] : 

The concept of Data Types  – What are they and How they work
In this tutorial, I will tell you about the concept of data types.
What are data types , what is the use of data types , how to use data types and how the data types create  difference in your java programs.
okay so before we continue , I want you to consider the program that we used to do mathematical calculations in Java.
public class calculation
public static void main()
                 int x = 20;
                int y = 10;
                       System.out.println(x + y);

So , In this program we have seen that to initialise a variable , we wrote “int x = 20; ” where
x is the name of the variable and 20 is the value given to the variable.
But what is “int” ???
Bewildered ?
int x = 10;
Let me clear it.
int is the type of the data or the value that is stored in the variable i. int is of the Integer data type which represents all types of numerals.
Now the question arises , What are data types ?
What are data types ?
In simple language , We can say that data types are the way to identify the type of data used by the programmer in the program.
Let us take a simple example :- I say to you to add up the numbers 10 and 5 and you gotta to do this with the help of Java. You know that “10” and “5” are number but Java doesn’t. You have to make Java realise that these two are numbers and then only Java can add the numbers. Otherwise it would end up adding the two fragments and give the output as “105”
So , There can be two cases in which Java can add these two tokens.
What are tokens ? Tokens are the smallest building blocks of a program. Each and everything that is used in Java is known as a token.
The two ways will be depending on how we write the program. So , before seeing the ways in which the result will be displayed , let us first see both the programs
in which different types of results will be produced.
Program 1 –
public class datatypes

         public static void main()
            int x = 10;
            int y = 5;
             System.out.print(x + y);
Program 2 –
public class datatypes

public static void main()
                 String i = “10”;
                String j = “5”;
                   System.out.println(i + j);
Did you notice the main difference between both these programs ?
Yes it is the difference in their Data Types.
In program 1 , int data type is used whereas in program 2 string data type is used.
Now let us see the difference between the outputs of these two programs.
Output 1 –  15
Output 2 –  105
Got the basic idea about how data types work ? Let me explain it a bit further.
In the program 1 , the data type used by us was “int” so Java considers the value of the integer as a number and so it adds both the numbers
In the program 2 , the data type used by us was “string” so Java considers the value of the integer a piece of text and so it adds both the pieces and gives the
resulatant as 105.
Important note –  Whenever we add any value to the String data type ,  We put is between double inverted commas.  For example  – String x = “10”;
The above example proves how the change of data types can change the output of the program.
Now , let us carry forward our discussion about data types.
What are the types of “Data types” ?
Well , Data types are also classified into two types. They are :-
  • Primitive data types.
  • Reference data types.
What are primitive data types ?
These are the basic data types provided in Java and they also make up the reference data types. These include the different types of data including text , numbers , decimals , true or false etc. A different data type is associated with each of the above and you will be knowing about them soon.

Kinds of Primitive data types.
The following are the different data types that are available in Java :-
  • Integer Data Types – These are the data types which are in numerical form.  The specific data types that come under it are :-
    • Int – int i = 10;
    • Short – short j = 5; 
    • Byte – byte n = 12;
    • Long – It is used as – long x = 1287413855;
These four data types come under the main category of  data types that is “Integer Data Types” . All of them have the same function and the main difference between them is their size which is described later in this article.
  • Floating data types – These are the data types that contain a decimal place. The  types which come under these are :-
    • Float – It is written in the form of decimal and the suffix “f” is added to it. For example – float i = 1.17f ;
    • Double –  It is also written in the form of decimal data types.  It is used as – double i = 1.11241245;
These also have the same function and the main difference is in their size.
  • Boolean Data Type – It does not have any sub categories. It only contains two values i.e. either true or false. It itself is written as  :- boolean x = true; 
  • Character Data Type – It is used to store only a single character which can be anything. for example :- char m = ‘&’;
NOTE – It add a value  to char data type , we put the value between single quotes i.e ”
To summarise – There are eight data types which are as given below :-
  • INT
  • BYTE
  • LONG
  • CHAR
In the few tables given below , more information about the data types is given below :-
  • Default Values Of Each Datatype is Given Below :- 
Data TypeDefault Value (for fields)
String (or any object)null
  • Size of each data type :- 
Data TypeSIZE
byte8 bits / 1 byte
short16 bits / 2 bytes
int32 bits / 4 bytes
long64 bits / 8 bytes
float32 bits / 4 bytes
double64 bits / 8 bytes
char16 bits / 2 bytes
String (or any object)Not a primitive data type.
boolean8 bits / 1 byte
  • Range of each Data Type :-
byte-128 to 127
short-32,768 to 32,767
int(-2^31) to (2^31 -1)
long(-2^63) to (2^63 -1)
float(- 3.4^38 ) to ( 3.4 ^ 38)
double(- 1.7^308) to (1.7 ^ 308)
char0 to 65535
String (or any object)Is not a primitive data type.
boolean8 bits / 1 byte
So , In this article I have covered almost everything about data types. Now below is a sample program with the output which will make your concepts about data types , very clear.

public class datatype 
public static void main()
            int x = 100;
            byte y = 5;
            short z = 15;
            long phoneno = 9875640;
            float marks = 14.5f;
            double pi = 3.145757875;
             char initial_letter = ‘P’ ;
             boolean statement = true;
                                System.out.println(“The value of x is : ” + x);
                                System.out.println(“The value of y is : ” + y);
                                System.out.println(“The value of z is : ” + z);
                                System.out.println(“My Phone number is : ” + phoneno);
                                System.out.println(“My marks are : ” + marks);
                                System.out.println(“The value of pi is : ” + pi); 
                                System.out.println(“The statement is : ” + statement);
In the above program I have made use of all the datatypes which will make you understand the concept of data types completely.
Now , let us see how this program looks in Java and what will be the output of this program when it will be executed in BlueJ
                      program in blueJ

program in blueJ
The program looks like this in BlueJ

This program gives the output as follows :-
The value of x is : 100
The value of y is : 5
The value of z is : 15
My Phone number is : 9875640
My marks are : 14.5
The value of pi is : 3.145757875
The statement is : true

This is the output of this program.

Muhammad Akbar

About Muhammad Akbar -

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you