Saturday, 13 February 2016

Data types in Java

data-types-in-java

Hello mate, In this post I am going to tell you about the Data types used in Java. In the previous Java tutorials, we have learned about Java variables, objects, classes, methods etc...
Now we are going to learn about the data types used in JAVA.
While coming to the normal way of programming to instruct a machine the knowledge of data types is more important. It’s like knowing alphabets of a language before going to learn a new language. For every programming language, there will be some sort of data types.
Data types are the basic things that need to be known when creating a program for performing some mathematical operations. Data types can be used to specify how data is represented in JAVA. In our previous tutorials “How to get input from the user” and “Addition of two numbers” there was something like “int a,b;”  mentioned that they are integer Data Types. While doing such calculations, it is necessary to mention the type of data. Two types of Data types in Java.
Data-types

 They are,
  1. Primitive Data types
  2. Reference Data types
Primitive Data types:
Primitive Data types are the basic and predefined Data Types in Java. When considering about Data Types Eight primitive Data Types are getting important. They are
 Integer:
  • For representing the whole numbers, integer data type is used. Like integer short, long and byte are also used for representing whole numbers.
  • But they are different in their length.
  • The range of int data type is from -2,147,483,648 to 2,147,483,647.
  • Default integer type value is 0.
  • The size of integer Data Type is 4 bytes.
Example:
int a,b,c;
Here a,b,c are the integer type variables. Naming considerations of these variables are explained in Variables.
Long:
  • If integer data type is not sufficient, long is used to represent large values than the integer.
  • The range of this data type is from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
  • The default value of this Data Type is 0L.
  • The size of long Data Type is 8 bytes.
Example:
long g;
Here g is a long type variable. Long Data Types are rarely used in basic programming.
Short:
  • Short can be used for representing short values (whole numbers).
  • Its range is from -32,768 to 32,767.
  • The default value of this Data Type is 0.
  • The size of short Data Type is 2 bytes.
Example:
short k;
Here k is a short type variable.
 Byte:
  • This is the smallest data type for representing whole numbers.
  • Byte Data Type ranges from -128 to 127.
  • Its default value is 0.
  • The size of this Data Type is one byte.
Example:
byte h,g;
Here h and g are byte type variables.
Float:
  • Float and double are used for representing the decimal point numbers such as 2819.248.
  • These data types are also called as Real types.
  • The default value of Float is 0f.
  • The size of this Data Type is 4 bytes.
Example:
float r;
Here r is the float type variable
Double:
  • The double data type is also same as Float, used for representing decimal point numbers larger than Float.
  • Ranges from to.
  • The default value of the double is 0d.
  • The size of this Data Type is 8 bytes.
Example:
double h;
Here h is a double Type variable.
Boolean:
  • The boolean data type can take Boolean values true and false in the form of 0s and 1s.
  • A Boolean variable can be assigned to a Boolean value or to an expression which returns a Boolean value.
  • Its default value is ‘false’.
Example:
boolean d=false;
(OR)
int a=45;
System.out.println((a>40));//which returns a Boolean value as 1(one)which indicates condition is true.
Character:
  • The character is a data type that allows single character (mostly alphabets but it allows any type of letters) to scan and print.
  • The size of character data type is 2 bytes.
  • Its default value is \u0000”.
Example:
char a;
Here a is the Character type variable.
 Literals:
Variables assigned with a particular value are called as literals, the value of the variable is not getting changed throughout the program. Literals are similar to enumerations which are having the same meaning but declaration of enumerations is difficult.
Example:
byte b=40;
char c=’k’;
 Reference Data Types:
Reference data types allow the user to define the size of the data. Using this data type user can dynamically allocate the size. Reference data types in JAVA are,
 Classes:
Classes are known as the reference data types. Variables having class type data are called as objects. These are used for representing real-world situations. Objects are used for accessing the methods inside the class.
Primitive data types would have fixed size in memory and it can’t be changed. In reference data types there is no fixed size, by the use of new operator user can dynamically allocate memory. This new operator is used in the object creation, array declaration etc.
By using this kind of data type, we can define the real world things such as the properties of vehicles and specifically car etc.
Example:
computer c=new computer();
Here c is the object of class computer and we can mention this that c is the variable of type class computer.
 Arrays:
Arrays are the collection of similar data types. For example, an integer array can hold a fixed amount of integer data type variables.
Example(integer array):
int a[]=new int[10];
or
int a[]={20,40,10,50,70,25,35,55,80,30}
Two important terms in the array are elements and index. Elements are the terms represented by the array. The terms inside the ‘{}’ are called the elements of the array. In other words, values inside the array are called as elements. Another term index which is used to access the elements, normally index starts with 0 and ends with the size of the array-1. In the above example a[10], the first element is stored in 0th place of the index that is a[0], the second element is stored in a[1] .... the last element that is 10th element is stored in a[9]th place.
Arrays are one of the important concepts of JAVA. Here we are mentioning that array is one of the reference data types in JAVA, but the array is the vast concept that needs to be clearly known for a programmer so all the aspects of arrays are fully explained in upcoming concepts.
 Interfaces:
The interface in JAVA can be used for replacing the multiple inheritances in C++. Inheritance is the concept of deriving a subclass from the superclass. The subclass is also called as derived class and super class is also called as the base class. Multiple inheritances mean deriving a base class from more than one derived classes. This concept is implemented by interfaces in java. Interfaces can be discussed deeply in the following chapters.
Also read:

Java Classes Methods and Objects