Aug 31, 2013

Basic Data Types in Java

Definition of Basic Data Types in Java:

Datatype (data-types) is the name of storage format which store the specific type's and its corresponding range's value.Different data types have been defined by java. These data types are frequently used by programmers. Data types in java are used to specify whether which type of value will be stored in specified variable. A variable is responsible for storing the particular value. But a variable does not store value itself. Variable is just a name for a memory location where a value is to be stored.Java has been defined different types of basic data types. We categorize them broadly into 2 category.

There are defined two types of data types in java-
  • Primitive Data Types
  • Non-Primitive/Reference Data Types

Primitive Data Types:

Primitive means ancient. As its meaning states Primitive Data Types are defined already in the Java API. Programmers can use them as their needs. There are eight types of Primitive Data Type defined in java. Let us take a look of all these eight primitive data types.
All eight primitive data types are as follows-

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean
  • char 

Data Type-byte

  • Byte data type in java is a 8-bit signed two's compliment integer.
  • Range of byte is from -128 to 127 i.e. minimum value is -128 and maximum value is 127.
  • Its default value is 0.
  • Byte data type in java is responsible to save spaces in large arrays, specifically in integer.
  • Byte data type is 4 times smaller than integer.
  • We can store a value in byte data type as- byte a=101, byte b= -40

Data Type-short

  • Short  data type in java is a 16-bit signed two's compliment integer.
  • Range of short is from -32768 to 32767 i.e. minimum value is -32768 and maximum value is 32767.
  • Short data type is also used to save memory.
  • Short data type is two times smaller than integer.
  • Its default value is 0.
  • We can store a value in short data type as- short a=10000, short b= -15000

Data Type-int:

  • Int data type in java is a 32-bit signed two's compliment integer.
  • Range of Int is from  -2147483648 to 2147483647 i.e. minimum value is -2147483648 and maximum value is 2147483647.
  • Int is specifically used as the default data type for integer values.
  • Its default value is 0.
  • We can initialize an int variable as- int a=100,int b= -2000

Data Type-long:

  • Long data type in java used when int data type is unable to store the value i.e. the value which is out of range of int data type can be stored in long data type.
  • Long data type is 64-bit signed integer.
  • Range of long is from  -9223372036854775808 to  9223372036854775807
  • Its default value is 0.
  • We can use a long type variable as- long a=150000L, long b= -250000L

Data Type-float:

  • Float data type in java used to store the fractional values i.e. the values which have decimal point. for ex- 10.25
  • float data type is  32-bit floating point .
  • Its default value is 0.0f.
  • float data type can not be used to store the currency value.
  • We can use float data type as- float a=25.45f

Data Type-double:

  • Double data type in java is also used to store the floating type values but of high range which can not be stored in float data type.
  • Double data type is 64-bit floating point.
  • It can also not be used to precise the value like currency.
  • Its default value is 0.0d.
  • We can use double data type as- double a=152.6

Data Type-boolean:

  • Boolean data type in java is used to store the logical values .
  • Boolean data type represents only one bit of information.
  • Boolean data type can store only two possible values true or false.
  • All relational operator returns the boolean type value.
  • Its default value is false.
  • We can use it as- boolean b=true

Data Type-char:

  • Char data type in java ,a 16-bit Unicode character is a very important data type..
  • Range of char data type lies between 0 to 65535 i.e. from '\uoooo' to '\uffff'.
  • Char data type is used to store any type of character. Some characters are hard to store in that situation we can make use of escape sequences to store that values.
  • We can store a value in char data type as - char a='A'



Reference Data types:

Reference data types are created by users. Reference data type are used to store the reference of the object i.e. reference data type are used to access the objects. Class,Interface,Array,Enum comes under the category of Reference Data type. A reference variable can store the reference of any reference data type and can refer to that class/interface/array/enum's object.
Default value of reference data type is null.
Example of reference data type in java-

Vehicle veh=new Vehicle();

In above example Vehicle is a class i.e. reference data type and veh is a reference variable of type Vehicle which is storing the reference of the Vehicle class's object.



We are providing a list of datatypes in a tabulated form.


Data types in java program



This is the complete description of Basic Data types in java.
Thank you.