Data types in Java are of different sizes and values that can be stored in the variable that is made as per convenience and circumstances to cover up all test cases. Java has two categories in which data types are segregated
- Primitive Data Type: such as boolean, char, int, short, byte, long, float, and double
- Non-Primitive Data Type or Object Data type: such as String, Array, etc.
Primitive Data Type
Primitive data are only single values and have no special capabilities. There are 8 primitive data types. They are depicted below in tabular format below as follows:
Type | Description | Default | Size | Example Literals | Range of values |
---|---|---|---|---|---|
boolean | true or false | false | 8 bits | true, false | true, false |
byte | twos-complement integer | 0 | 8 bits | (none) | -128 to 127 |
char | Unicode character | \u0000 | 16 bits | ‘a’, ‘\u0041’, ‘\101’, ‘\\’, ‘\’, ‘\n’, ‘β’ | characters representation of ASCII values 0 to 255 |
short | twos-complement integer | 0 | 16 bits | (none) | -32,768 to 32,767 |
int | twos-complement intger | 0 | 32 bits | -2,-1,0,1,2 | -2,147,483,648 to 2,147,483,647 |
long | twos-complement integer | 0 | 64 bits | -2L,-1L,0L,1L,2L | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | IEEE 754 floating point | 0.0 | 32 bits | 1.23e100f , -1.23e-100f , .3f ,3.14F | upto 7 decimal digits |
double | IEEE 754 floating point | 0.0 | 64 bits | 1.23456e300d , -123456e-300d , 1e1d | upto 16 decimal digits |
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | // Class class Wrap { // Main driver method public static void main(String args[]) { // Creating and initializing custom character char a = 'G'; // Integer data type is generally // used for numeric values int i = 89; // use byte and short // if memory is a constraint byte b = 4; // this will give error as number is // larger than byte range // byte b1 = 7888888955; short s = 56; // this will give error as number is // larger than short range // short s1 = 87878787878; // by default fraction value // is double in java double d = 4.355453532; // for float use 'f' as suffix as standard float f = 4.7333434f; // need to hold big range of numbers then we need // this data type long l = 12121; System.out.println("char: " + a); System.out.println("integer: " + i); System.out.println("byte: " + b); System.out.println("short: " + s); System.out.println("float: " + f); System.out.println("double: " + d); System.out.println("long: " + l); } } |
Output:
1 2 3 4 5 6 7 | char: G integer: 89 byte: 4 short: 56 float: 4.7333436 double: 4.355453532 long: 12121 |
Non-Primitive Data Type or Reference Data Types
Non-primitive data types are called reference types because they refer to objects.
The main difference between primitive and non-primitive data types are:
- Primitive types are predefined (already defined) in Java. Non-primitive types are created by the programmer and is not defined by Java (except for
String
). - Non-primitive types can be used to call methods to perform certain operations, while primitive types cannot.
- A primitive type has always a value, while non-primitive types can be
null
. - A primitive type starts with a lowercase letter, while non-primitive types starts with an uppercase letter.
Examples of non-primitive types are Strings, Arrays, Classes, Interface, etc.