Sunday 29 May 2016

Java Data Types

In this blog post,we will learn about data types supported by java language.
For performing any operations in a program we need data .In java program data comes from variables.Variables are nothing but reserved memory location to store values.Each value have certain type known as Data Types in java.

There are two data types available(supported) by java:
1-Primitive Data Types
2-Object /Reference Data Types


Primitive Data Types:
There are eight primitive data types supported by Java.Primitive data types are predefined by the language and also named by a keyword.Let us now look into more details one by one.

1-byte:

Byte data type is an 8-bit signed two's complement integer.The default value is 0
The Maximum value is 127 (2^7 -1) and Minimum value is -128 (-2^7).Byte data types mostly used when getting data from Input Stream.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.

Example: byte _ab = 78 , byte abc = -40

2-short:
Short data type is a 16-bit signed two's complement integer.The Default value for short also is 0.

The Minimum value is -32,768 (-2^15) and Maximum value is 32,767 (inclusive) (2^15 -1)

Example: short _sh1 = 10000, short _sh2 = -15000

3-int:
Int data type is a 32-bit signed two's complement integer.The default value also 0.
The Minimum value is - 2,147,483,648.(-2^31) and Maximum value is 2,147,483,647(inclusive).(2^31 -1)
Example: int a = 100000, int b = -200000


4-long:
Long data type is a 64-bit signed two's complement integer.The default value is 0L.
The Minimum value is -9,223,372,036,854,775,808.(-2^63) and Maximum value is 9,223,372,036,854,775,807 (2^63 -1).

Example: long _ln1 = 100600L, long _ln2 = -100000L

5-float:
Float data type is a single-precision 32-bit floating point.The default value is 0.0f. Float is mainly used to save memory in large arrays of floating point numbers.

Example: float f1 = 434.67f


5-double:
double data type is a double-precision 64-bit floating point.The default value is 0.0d.
Example: double _d1 = 173.4

6-boolean:
boolean data type represents one bit of information either true or false.The default value is false.
This data type is used for to check conditions.

Example: boolean _b1 = true


5-char:
char data type is a single 16-bit Unicode character.The char data type is used to store any character.
The Minimum value is '\u0000' (or 0) and Maximum value is '\uffff' (or 65,535 inclusive).

Example: char _ch='D'

2-The second and most important data types in java and any other programming language is reference data type.

Reference Data Types:

A reference type is a data type that’s based on a class rather than on one of the primitive types that are built in to the Java language.In the other way,When we create an object from class ,JVM allocates the amount of memory to store that object.If you assign the object to the variable the variable actually assigned the reference to the object.The reference is the address of the memory location where objects are stored. For example- Employee, Animal etc.

objects and various type of array variables come under the reference data type.The default value of any reference data type is null.

Example:
Employee emp1=null;
Employee emp1 = new Employee("Ramesh");

No comments:

Post a Comment