Java Data Types
Data type in java defines the type of data that variables can store. Following are two main categories of data types in java.
Primitive Data Types
Primitive means something that occurs in early stage of development. Similarly, primitive data types are those which are provided by Java and are not objects. Below are the examples of primitive data types
Data Type | Default Value | Size in memory | Range |
byte | 0 | 1 byte | -128 to 127 |
short | 0 | 2 byte | -32768 to 32767 |
int | 0 | 4 byte | -2,147,483,648 to 2,147,483,647 |
long | 0L | 8 byte | -9,223,372,036,854,775,808 to -9,223,372,036,854,775,807 |
float | 0.0f | 4 byte | 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative) |
double | 0.0d | 8 byte | 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative) |
char | ‘\u0000’ | 2 byte | ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535) |
boolean | false | 1 bit | true/false |
Non Primitive Data Types
Non primitive data types are defined using classes, interfaces and enums. Such as String is a data type, List, or any custom class.
byte :
A byte data type is a 8 bit singed two’s complement integer. The range of integers that can be stored in it is -128 to 127. Any number out of this range cannot be assigned to byte type variable. It takes only 1 byte and therefore very effective in memory saving in situations where memory is crucial.
byte b = 126;
byte b1 = 327; // x not allowed
short :
It is 16 bits two’s complement integer. Its range varies from -32,768 t0 32,767. Any number between this range can be assigned to short type variable.
short s = 3265;
short s1 = 32768; // x not allowed
int :
It is 32 bits two’s complement integer. Its range varies from – 2,147,483,648 i.e. (-2^31) to 2,147,483,647 i.e. (2^31 – 1). It consumes 4 bytes in memory
int num = 1243214;
int num1 = 21474836476; // x not allowed
long :
It is a 64-bit two’s complement integer. Its range varies from -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1). It consumes 8 bytes in memory. You would need to use this data type if you want to store a value which is beyond the range of int.
long num = 1354356789;
Question : Now the question arises if we have a number which is beyond the range of long data type as well, then how will we store that? Answer : We have a class called BigInteger. We can store any integer using object of this class.
float :
It is a single-precision 32-bit IEEE 754 floating point number (A number with decimal values). Its default value is 0.0F. A floating-point literal is of type float if it is suffixed with an ASCII letter F or f. Otherwise, it is considered double by default. A double can optionally be suffixed with D or d.
float decimalVal = 122.76f;
double :
It is a double-precision 64-bit IEEE 754 floating point number. Its default value is 0.0d. A double number can be suffixed with D or d, but that is not mandatory. It we don’t specify it, it is still considered double by default.
double d = 13412.3;
double d1 = 24234.54d;
char :
It is a single 16-bit Unicode character. It can store 1 character at a time. Its value ranges from ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535). Java uses Unicode system to recognize different characters.
char c = 'A';
char c1 = 'M';
char c2 = 'f';
char c3 = 23; // we can also assign unicode integer values which represents a character
char c4 = '?';
boolean :
boolean stores either true or false. It takes only 1 bit in memory.
boolean result = true;
boolean isAlive = false;
boolean itSuccess = true;
Code Example to show all primitive data types
package com.sks;
public class PrimitiveDataTypesExample {
byte b = 120;
short s = 12030;
int num = 2798765;
long longNum = 132412341L;
float floatNum = 13412.0976f;
double doubleNum = 23414.987;
char c = 'v';
boolean result = true;
public static void main(String[] args) {
PrimitiveDataTypesExample obj = new PrimitiveDataTypesExample();
System.out.println("byte b = " + obj.b);
System.out.println("short s = " + obj.s);
System.out.println("int num = " + obj.num);
System.out.println("long longNum = " + obj.longNum);
System.out.println("float floatNum = " + obj.floatNum);
System.out.println("double doubleNum = " + obj.doubleNum);
System.out.println("char c = " + obj.c);
System.out.println("boolean result = " + obj.result);
}
}
Output :
byte b = 120
short s = 12030
int num = 2798765
long longNum = 132412341
float floatNum = 13412.098
double doubleNum = 23414.987
char c = v
boolean result = true