Data types are the means for the tasks related to identifying and assessing the type of data. Java is rich in data types which allows the programmer to select the appropriate type needed to build variables of an application.
Why is the data type important?
- Every variable in Java has a data type which tells the compiler what type of variable it as and what type of data it is going to store.
- Data type specifies the size and type of values.
- Information is stored in computer memory with different data types.
- Whenever a variable is declared, it becomes necessary to define a data type that what will be the type of data that variable can hold.
Data Types in Java
- Primary Data Type
Java supports eight primitive data types: byte, short, int, long, float, double, char and boolean.
These eight data types are further classified into four groups:- Integer,
- Relational Numbers(Floating point)
- Characters
- Boolean(Conditional).
- Non-Primitive Data Types
Classes, Interface, Arrays, etc.
This chapter is all about basic primitive data types in Java.
Integer Types
Integer is the whole number without any fractional point. It can hold whole numbers such as 196, -52, 4036, etc. Java supports four different types of integers, these are:
Type | Contains | Default | Size | Range |
---|---|---|---|---|
byte | Signed integer | 0 | 8 bit or 1 byte | -27 to 27-1 or -128 to 127 |
short | Signed integer | 0 | 16 bit or 2 bytes | -215 to 215-1 or -32,768 to 32767 |
int | Signed integer | 0 | 32 bit or 4 bytes | -231 to 231-1 or -2147,483,648 to 2147,483,647 |
long | Signed integer | 0 | 64 bit or 8 bytes | -263 to 263-1 or -9223,372,036,854,755,808 to 9223,372,036,854,755,807 |
Rational Numbers
It is used to hold whole numbers containing fractional part such as 36.74, or -23.95 (which are known as floating point constants). There are two types of floating point storage in java. These are:
Type | Contains | Default | Size | Range |
---|---|---|---|---|
float | IEEE 754 floating point single-precision | 0.0f | 32 bit or 4 bytes | ±1.4E-45 to ±3.40282347E+38F |
double | IEEE 754 floating point double-precision | 0.0 | 64 bit or 8 bytes | ±439E-324 to ±1.7976931348623157E+308 |
Characters
It is used to store character constants in memory. Java provides a character data type called char whose type consumes a size of two bytes but can hold only a single character.
Type | Contains | Default | Size | Range |
---|---|---|---|---|
char | Unicode character unsigned | \u0000 | 16 bits or 2 bytes | 0 to 216-1 or \u0000 to \uFFFF |
Conditional
Boolean type is used to test a particular condition during program execution. Boolean variables can take either true or false and is denoted by the keyword boolean and usually consumes one byte of storage.
Type | Contains | Default | Size | Range |
---|---|---|---|---|
boolean | true or false | false | 1 bit | true or false |
Example of Data Types and Variable Declarations in Java
Example:
class DataTypes{
public static void main(String args[]){
byte byteVar = 5;
short shortVar = 20;
int intVar = 30;
long longVar = 60;
float floatVar = 20;
double doubleVar = 20.123;
boolean booleanVar = true;
char charVar ='W';
System.out.println("Value Of byte Variable is " + byteVar);
System.out.println("Value Of short Variable is " + shortVar);
System.out.println("Value Of int Variable is " + intVar);
System.out.println("Value Of long Variable is " + longVar);
System.out.println("Value Of float Variable is " + floatVar);
System.out.println("Value Of double Variable is " + doubleVar);
System.out.println("Value Of boolean Variable is " + booleanVar);
System.out.println("Value Of char Variable is " + charVar);
}
}
Program Output:
Value Of byte Variable is 5 Value Of short Variable is 20 Value Of int Variable is 30 Value Of long Variable is 60 Value Of float Variable is 20.0 Value Of double Variable is 20.123 Value Of boolean Variable is true Value Of char Variable is W