Friday, 31 May 2013

Data Types in JAVA

Before going to discuss with data types of java. We need to discuss where data types comes into java.

Java is OOP Language.
Here we have question like What is OOP? and What it provides?
OOP provides design rules and design principles.

What design rules or principles that oop follows:
 Divide & Conquer

It divides the system into units, where each unit is implemented individually and these are integrated to form final system.

Now replace the system with Object, then system is termed as oop system.

What is oop system?
It is an implementation and integration of object.

How do we implement Object?
To do this we have to understand what is object.

An Object is small unit of oop system that consisting of data and behavior to perform task.

Object is created using class.

Class is designed model or blueprint or template to create objects.
Class describes data and behavior that object should hold. So class put it to do action to create objects.

Who represent the data in an object?
Variable and constant

What variable consist of?
Name and type.  Where as "Name" required to identify the variable and "type" represents the type of data.
Variable declaration.

When we declare variable then memory will be allowcate memory. JVM provides different types of memory locations for different types of variables. So variable is used to acquire memory to store data.

Syntax to declare variable is Datatype variableName;

So "data types" comes into java.

DataType: Data type specifies the size and type of values that can be stored in an identifier. The Java language is rich in its data types. Different data types allow you to select the type appropriate to the needs of the application.

There are two data types available in Java:
  1. Primitive Data Types
  2. Reference/Object Data Types
Primitive data types are predefined by the language and named by a key word.There are eight primitive data types supported by Java. Let us now look into detail about the eight primitive data types.

byte: Byte data type is a 8-bit signed two's complement integer.
Range: -128 (-2^7) to 127 (inclusive)(2^7 -1)
Example : byte a = 100 , byte b = -50;

short: Short data type is a 16-bit signed two's complement integer.
Range: -32,768 (-2^15) to 32,767(inclusive) (2^15 -1)
Example : short s= 10000 , short r = -20000;

int: Int data type is a 32-bit signed two's complement integer.
Range: - 2,147,483,648.(-2^31) to 2,147,483,647(inclusive).(2^31 -1)
Example : int a = 100000, int b = -200000;

long: Long data type is a 64-bit signed two's complement integer.
Range:  -9,223,372,036,854,775,808.(-2^63) to 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
Example : int a = 100000L, int b = -200000L;

float: Float data type is a single-precision 32-bit IEEE 754 floating point.
Example : float f1 = 234.5f;

double: double data type is a double-precision 64-bit IEEE 754 floating point.
Example : double d1 = 123.4;

boolean: boolean data type represents one bit of information.
Example : boolean one = true;

char: char data type is a single 16-bit Unicode character.
Example : char letterA ='A';

Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.

Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int.

Int is generally used as the default data type for integral values unless there is a concern about memory.

Long type is used when a wider range than int is needed.

Float and Long data types are never used for precise values such as currency.

boolean data type is used for simple flags that track true/false conditions.

Char data type is used to store any character.

Reference Data Types:

Reference variables are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed. For example, Employee, Puppy etc.

Class objects, and various type of array variables come under reference data type.

Default value of any reference variable is null.

A reference variable can be used to refer to any object of the declared type or any compatible type.

Example : Class reference = new Class("JAVA");

No comments:

Post a Comment