Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

Java

Learn step by step



Data Types

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.

Data types in Java are classified into two types:

1. Primitive— which include Integer, Character, Boolean, and Floating Point.
2. Non-primitive— which include Classes, Interfaces, and Arrays.

1. Primitive Data Types

1. Integer
Integer types can hold whole numbers such as 123 and −96. The size of the values that can be stored depends on the integer type that we choose

  • There are four types of integer data variables.
  • They can store both negative and positive values.
Data TypeValueMemory
byte-128 -- +127occupy 1 byte (8 bits) in memory
short-32768 -- 32767occupy 2 bytes (16 bits) in memory
int-2147483648 -- 2147483647occupy 4 bytes (32 bits) in memory
long-9223372036854775808 -- 9223372036854775807occupy 8 bytes (64 bits) in memory

The range of values is calculated as −(2n−1) to (2n−1)−1; where n is the number of bits required. For example, the byte data type requires 1 byte = 8 bits. Therefore, the range of values that can be stored in the byte data type is −(28−1) to (28−1)−1
= −27 to (27) -1
= −128 to 127

2. Floating Point
Floating point data types are used to represent numbers with a fractional part. Single precision floating point numbers occupy 4 bytes and Double precision floating point numbers occupy 8 bytes. There are two subtypes:

TypeSizeRange of values that can be stored
Float4 bytes3.4e−038 to 3.4e+038
Double8 bytes1.7e−308 to 1.7e+038

3. Character
It stores character constants in the memory. It assumes a size of 2 bytes, but basically it can hold only a single character because char stores unicode character sets. It has a minimum value of ‘u0000′ (or 0) and a maximum value of ‘uffff’ (or 65,535, inclusive).

4. Boolean
The boolean data type has only two possible values: true and false . Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

2. Non-Primitive Data Types

  • Objects (Classes & Interfaces) and Arrays are the reference or non-primitive data types in Java.
  • They are so called because they are handled by "reference" i.e. variables of their type store the address of the object or array in a variable.
  • The reference type can hold three kinds of values-

1. Class Type:
Objects (Classes & Interfaces) and Arrays are the reference or non-primitive data types in Java. They are so called because they are handled by "reference" i.e. variables of their type store the address of the object or array in a variable.

The reference type can hold three kinds of values-

  • Points to an object/class instance
  • Whenever a variable is created, a reference to an object is also created using the name of a class for its type i.e. that variable can contain either null or a reference to an object of that class. It is not allowed to contain any other kinds of values. Such type is called reference types in Java.

EXAMPLE:

class Example
{
...
...
}
main()
{
Example example1;
Example example2;
}

2. Interface Type:

  • Points to an object, which is implementing corresponding interface.
  • When a class declaration implements an interface, that class inherits all of the variables and methods declared in that interface. So the implementations for all of the methods declared in the interface must be provided by that class. For example, Java provides an interface called ActionListener whose method named actionPerformed() is used to handle the different kind of event . Java also provides a class called Thread that implements Runnable interface.

EXAMPLE :

Runnable r;
r = new Thread();

3. Array Type:

  • Points to an array instance.
  • An array is a special kind of object that contains values called elements. The java array enables the user to store the values of the same type in contiguous memory allocations. The elements in an array are identified by an integer index which initially starts from 0 and ends with one less than number of elements available in the array. All elements of an array must contain the same type of value i.e. if an array is a type of integer then all the elements must be of integer type. It is a reference data type because the class named as Array implicitly extends java.lang.Object.

EXAMPLE :

DataType [] example1, example2, .......examplen;
DataType [] example = new DataType [ArraySize];

DataType [] example = {item 1, item 2,...item n};


Related Videos