Sunday 30 June 2013

DATA TYPES IN JAVA




DEFINITION :

Data type in any language is the type of data that is used to represent the values such as the whole numbers(Integer types) , number with decimal points(floating point), characters etc.
Data type in java language is one of the most fundamental element.
There are two types of data in Java, these are :
1. Primitive type
2. Reference type


PRIMITIVE TYPE :

The primitive type is simple type which represent only single value - not complex objects. The reason for this is the performance. Since if we make simple type as the object than it must contain some property and some behaviour which will result in degraded performance.
They are used for reffering only a single value or we can say that they are not like the actual objects(which has a property and behaviour).


Four types of Primitive data type are :
1. INTEGERS :  byte, short, int, and long - data types in Integers group.
2. FLOATING-POINT NUMBERS : float and double - data types in Floating point group.
3. CHARACTERS : char - data type in the characters group.
4. BOOLEAN : boolean - data type in the boolean group.


Now in java all the data types have a strictly defined range to make it portable. For example, an int is always 32 bits, regardless of the particular platform. However the size can be changed later by Java Runtime Environment which depends upon the processor of your computer.

INTEGERS :
There are four integer types that java supports :
1. byte : 
It is the smallest integer type.
width : 8 bits, range : -128 to +127.
Use : The variables of type "byte" are especially useful when you're working with a stream of data from a network or file.
declaration : declared with byte keyword.
e.g,
byte b;

2. short :
It is probably the least used java type.
width : 16 bits, range : -32,768 to +32,767
Use : Used mostly in 16 bit computers or we can say that 16 bit microprocessors.
declaration : declared with short keyword.
e.g,
short s;

3. int :
It is the most commonly used integer data type.
width : 32 bits, range : -2,147,483,648 to +2.147.483.647
Use : Mostly used in control loops and to index arrays.
declaration : declared with int keyword.
e.g,
int a;

4. long :
It is largest integer type.
width : 64 bits, range : -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
Use : It is useful when big whole numbers are needed like in the calculation of large distances. for e.g, in measuring the distance of earth from the sun.
declaration : declared with long keyword.
e.g,
long g;

Note : All of the above integer types are signed, positive and negative values. Java does not support unsigned, positive only integers.

FLOATING POINT TYPES :
The floating point numbers are also known as the real numbers and are used when evaluating expressions that require fractional precision.
There are two kinds of floating point types :
1. float :
It specifies a "single precision" value or we can say it specifies  the small decimal value as compared with the double.
width : 32 bits, range : 1.4e-045 to 3.4e+038
Use : Mostly used in representing dollers and cents.
declaration : declared with the help of float keyword.
e.g,
float doller;

2. double :
It specifies a "double precision" value. It is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations.
width : 64 bits, range : -4.9e-324 to 1.8e+308
Use : All transcendental math functions, such as sin(), cos(), and sqrt(), return double values.
declaration : declared with the use of double keyword.
e.g,
double pi = 3.1416;

CHARACTERS :
The data type used to represent characters is char.
char :
It represents characters ASCII value.
width : 16 bits, range : 0 to 65,536
The width of char data type is 16 bits because it is represented by "Unicode" which is the combination of all the characters of different languages in the world, such as Latin, Greek, Arabic, English etc.
Also there are no negative chars.
Use : used to represent characters.
declaration : declared with the use of char keyword.
e.g,
char a = 'Y';

BOOLEANS :
The data type used to represent booleans is boolean.
It can have only one of the two possible values, true or false.
The default value of boolean is false.
Use : Mostly used in the conditional expressions that govern the control statements such as if and for.
e.g,
boolean b = true;

Note : It should not be assumed as a string which is different that boolean.


REFERENCE TYPES :

As the name, reference data types are used to refer to a particular object.
for e.g,
My_Class reference = new My_Class();
Here, reference is the reference variable which is used to refer to a My_Class object.

The reference variables are created in the heap. They can be of Array type or any class type.


Java is a strongly typed language because in java every variable has a type, every expression has a type and every type is strictly defined.

9 comments :

  1. Great Explanation.
    Byte and short datatype cannot be a literal.

    Prathap
    java training in chennai

    ReplyDelete
  2. Thank you,

    For more simple tutorial about java data type have a look at this site. codenread.com

    ReplyDelete
  3. You can clear many of your doubts regarding data types in Core Java through Merit Campus, visit: http://java.meritcampus.com/core-java-topics/data-types-in-java, http://java.meritcampus.com/core-java-topics/floating-point-data-types-in-java

    Not only data types, we also have each and every topic in Core Java with example for each. You can read lot of sessions and can write many practice tests in Merit Campus Java website. visit: http://java.meritcampus.com/core-java-topics/ to know more.

    ReplyDelete
  4. I have read your blog and i got a very useful and knowledgeable information from your blog.You have done a great job . If anyone want Java Online Training, Please visit our page

    Core Java Online Training

    ReplyDelete
  5. Excellent post. You have post an very useful information.This is my first time i visit here. I found so many interesting stuff in your blog. keep updating. Java Training in Chennai | Selenium Training in Chennai

    ReplyDelete
  6. Thanks of sharing this post…Python is the fastest growing language that helps to get your dream job in a developing area. It says every fundamental in a programming, so if you want to become an expertise in python get some training





    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery

    ReplyDelete
  7. Thanks Author for Sharing this Valuable information about Data Types in Java here. Can we called Custom Data type to Primitive Data types?

    ReplyDelete