For every programming language, the data type is very important. Let’s discuss what is the data types in java. The data type specifies the type of value that can store in a variable. Variable is just a memory location to store values. Based on the data type of a variable, the operating system (OS) allocates memory and decides what can be stored in memory.
Youtube video available in the Hindi language
Two types of Data types in Java
1. Primitive data types in java
2. Non-Primitive/Object/reference data types in java
1. Primitive Data type
The primitive data type in Java is the most basic data type. These are building blocks of data manipulation and predefined by the language.
There are 8 types of primitive data types:
- boolean data type
- byte data type
- char data type
- short data type
- int data type
- long data type
- float data type
- double data type
- boolean:
- The boolean data type is only two possible values: true and false.
- The boolean data type can represent only one bit (1 bit) of information.
- It is used to track true/false conditions.
- The default value of boolean is false.
- Example: boolean isUserExist = true; Here “boolean” is data type, “isUserExist” is the name of a variable and it can store only true/false.
class ExampleOfBoolean { public static void main(String[] args) { boolean isUserExist = true; if(isUserExist) { System.out.println("Value of boolean variable :"+ isUserExist); } } }
Output : Value of boolean variable :true
public class ExampleOfBoolean { boolean hasValueExist; public static void main(String args[]) { // Data type is boolean // Value can be true or false only // Represent in only one bit (1 bit) // Default value will be false boolean hasValue = false; // boolean hasData = 1; if(hasValue) { System.out.println("boolean value is true"); } else { System.out.println("boolean value is false"); } int number = 101; boolean isNumberGreaterThanHundred = number > 100; if(isNumberGreaterThanHundred) { System.out.println("Number is greater than 100"); } ExampleOfBoolean object = new ExampleOfBoolean(); System.out.println("Default value: "+ object.hasValueExist); } }
Output: boolean value is false
Number is greater than 100
Default value: false
- byte:
- byte data can hold the whole number between -128 to 127.
- It is an 8-bit signed two’s complement integer.
- Its minimum value is -128 and maximum value is 127.
- It is used to save memory in large arrays because it is four-time smaller than int data type
- The default value of the byte is 0.
class ExampleOfByte { public static void main(String[] args) { byte a = 127; System.out.println("Value of byte variable :"+ a); // It will increment one in "a" again assigning the new value a++; System.out.println("Value of byte variable :"+ a); // It will increment one in "a" again assigning the new value a++; System.out.println("Value of byte variable :"+ a); } }
Output: Value of byte variable: 127
Value of byte variable: -128
Value of byte variable: -127
As you see in the example byte can hold values from -128 to 127. When the compiler encounters the value of a = 128 which overflows from the range then it was gone in the negative range.
public class ExampleOfByte { byte defaultValue; public static void main(String[] args) { // Data type is byte // Value can be integer only // Can hold number between -128 to 127 in 1 byte // Default value will be 0 // Four time smaller than int byte byteData = 120; System.out.println("Value of byte variable :"+ byteData); byteData = 127; // It will increment one in "byteData" again assigning the new value byteData++; System.out.println("Value of byte variable :"+ byteData); // It will increment one in "byteData" again assigning the new value byteData++; System.out.println("Value of byte variable :"+ byteData); byteData = -1; // It will increment one in "byteData" again assigning the new value byteData++; System.out.println("Value of byte variable :"+ byteData); byteData = 0; // It will increment one in "byteData" again assigning the new value byteData++; System.out.println("Value of byte variable :"+ byteData); byte number = (byte)130; System.out.println("Number: "+ number); ExampleOfByte object = new ExampleOfByte(); System.out.println("Default value: "+ object.defaultValue); } }
Output: Value of byte variable :120
Value of byte variable :-128
Value of byte variable :-127
Value of byte variable :0
Value of byte variable :1
Number: -126
Default value: 0
- short:
- short data can hold the whole number between -32,768 to 32,767.
- It is a 16-bit signed two’s complement integer.
- Its minimum value is -32,768 and maximum value is 32,767.
- It is also used to save memory in large arrays because it is two times smaller than int data type
- The default value of the byte is 0.
class ExampleOfShort { public static void main(String[] args) { short a = 127; System.out.println("Value of short variable :"+ a); } }
Output: Value of short variable :127
public class ExampleOfShort { short defaultValue; public static void main(String[] args) { // Data type is short // Value can be integer only // Can hold number between -32,768 to 32,767 in 2 byte. // Default value will be 0 // 2 times smaller than int short shortData = 32766; System.out.println("Value of short variable : "+ shortData); shortData = 32767; // It will increment one in "shortData" again assigning the new value shortData++; System.out.println("Value of short variable :"+ shortData); // It will increment one in "shortData" again assigning the new value shortData++; System.out.println("Value of short variable :"+ shortData); shortData = -1; // It will increment one in "shortData" again assigning the new value shortData++; System.out.println("Value of short variable :"+ shortData); shortData = 0; // It will increment one in "shortData" again assigning the new value shortData++; System.out.println("Value of short variable :"+ shortData); short number = (short) 32770; System.out.println("Number: "+ number); ExampleOfShort object = new ExampleOfShort(); System.out.println("Default value: "+ object.defaultValue); } }
Output: Value of short variable : 32766
Value of short variable :-32768
Value of short variable :-32767
Value of short variable :0
Value of short variable :1
Number: -32766
Default value: 0
- int:
- int data can hold whole number between – 2,147,483,648 to 2,147,483,647.
- It is a 32-bit signed two’s complement integer.
- Its minimum value is – 2,147,483,648 and maximum value is 2,147,483,647.
- It is used to store an integer value.
- Default value of byte is 0.
class ExampleOfInt { public static void main(String[] args) { int a = 127; System.out.println("Value of int variable :"+ a); } }
Output: Value of int variable :127
public class ExampleOfInt { int defaultValue; public static void main(String[] args) { // Data type is int // Value can be integer only // Can hold number between – 2,147,483,648 to 2,147,483,647 in 4 byte. // Default value will be 0 int intData = 2147483645; System.out.println("Value of int variable : "+ intData); intData = 2147483647; // It will increment one in "intData" again assigning the new value intData++; System.out.println("Value of int variable :"+ intData); // It will increment one in "intData" again assigning the new value intData++; System.out.println("Value of int variable :"+ intData); intData = -1; // It will increment one in "intData" again assigning the new value intData++; System.out.println("Value of int variable :"+ intData); intData = 0; // It will increment one in "intData" again assigning the new value intData++; System.out.println("Value of int variable :"+ intData); int number = (int)2147483650l; System.out.println("Number: "+ number); ExampleOfInt object = new ExampleOfInt(); System.out.println("Default value: "+ object.defaultValue); } }
Output: Value of int variable : 2147483645
Value of int variable :-2147483648
Value of int variable :-2147483647
Value of int variable :0
Value of int variable :1
Number: -2147483646
Default value: 0
- long:
- int data can hold whole number between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
- It is a 64-bit signed two’s complement integer.
- Its minimum value is -9,223,372,036,854,775,808 and maximum value is 9,223,372,036,854,775,807
- It is used when you need a range of values more than those provided by int.
- The default value of the byte is 0L.
class ExampleOfLong { public static void main(String[] args) { long a = 123456789; System.out.println("Value of long variable :"+ a); } }
Output: Value of int variable :123456789
public class ExampleOfLong { long defaultValue; public static void main(String[] args) { // Data type is long // Value can be integer only // Can hold number between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 in 8 bytes // Default value will be 0 long longData = 9223372036854775805L; System.out.println("Value of long variable : "+ longData); longData = 9223372036854775807L; // It will increment one in "longData" again assigning the new value longData++; System.out.println("Value of long variable :"+ longData); // It will increment one in "longData" again assigning the new value longData++; System.out.println("Value of long variable :"+ longData); longData = -1; // It will increment one in "longData" again assigning the new value longData++; System.out.println("Value of long variable :"+ longData); longData = 0; // It will increment one in "longData" again assigning the new value longData++; System.out.println("Value of long variable :"+ longData); ExampleOfLong object = new ExampleOfLong(); System.out.println("Default value: "+ object.defaultValue); } }
Output: Value of long variable : 9223372036854775805
Value of long variable :-9223372036854775808
Value of long variable :-9223372036854775807
Value of long variable :0
Value of long variable :1
Default value: 0
- float:
- It is a single-precision 32-bit IEEE 754 floating-point.
- It is used to save memory in large arrays for floating numbers.
- The default value of the byte is 0.0f.
- Enough for holding 6 to 7 decimal digits
- Example: float a = 1.3f
class ExampleOfFloat { public static void main(String[] args) { float a = 12.123456f; System.out.println("Value of float variable :"+ a); } }
Output: Value of float variable :12.123456
public class ExampleOfFloat { float defaultValue; public static void main(String[] args) { // Data type is float // Value can be decimal and integer // Enough for holding 6 to 7 decimal digits // Save memory in large arrays for floating numbers. // Default value will be 0.0 float floatData = 1.123456f; System.out.println("Value of long variable : "+ floatData); floatData = 1.1234567f; floatData = 1.123456789f; System.out.println("Value of float variable :"+ floatData); // It will increment one in "floatData" again assigning the new value floatData++; System.out.println("Value of float variable :"+ floatData); floatData = 0f; System.out.println("Value of float variable :"+ floatData); floatData = (float) 1.123456; System.out.println("Value of float variable :"+ floatData); ExampleOfFloat object = new ExampleOfFloat(); System.out.println("Default value: "+ object.defaultValue); } }
Output: Value of long variable : 1.123456
Value of float variable :1.1234568
Value of float variable :2.123457
Value of float variable :0.0
Value of float variable :1.123456
Default value: 0.0
- double:
- It is a single-precision 64-bit IEEE 754 floating-point.
- It is used to save memory in large arrays for floating numbers.
- The default value of byte is 0.0d.
- Enough for holding 15 decimal digits
- This data type is generally the default choice. If you declared any variable with decimal, then it is by default double. You must specify its type.
- For example: float a = 1.2;
NOTE: It will show an error message – “Type mismatch: cannot convert from double to float”. You can declare a float variable as like: Float a = 1.2f;
class ExampleOfDouble { public static void main(String[] args) { double a = 12.123456788888888; System.out.println("Value of double variable :"+ a); } }
Output: Value of float variable :12.123456788888888
public class ExampleOfDouble { double defaultValue; public static void main(String[] args) { // Data type is double // Value can be decimal and integer // Enough for holding 15 or 16 decimal digits // Save memory in large arrays for floating numbers. // Default value will be 0.0 double doubleData = 1.1234567891234567; System.out.println("Value of double variable : " + doubleData); doubleData = 1.123456789123456789; System.out.println("Value of double variable :" + doubleData); // It will increment one in "doubleData" again assigning the new value doubleData++; System.out.println("Value of double variable :" + doubleData); doubleData = 0; System.out.println("Value of double variable :" + doubleData); ExampleOfDouble object = new ExampleOfDouble(); System.out.println("Default value: " + object.defaultValue); } }
Output: Value of double variable : 1.1234567891234566
Value of double variable :1.1234567891234568
Value of double variable :2.123456789123457
Value of double variable :0.0
Default value: 0.0
- char:
- This data type is used to store characters.
- the char data type is a single 16-bit Unicode character
- Minimum value is ‘\u0000’ (or 0)
- Maximum value is ‘\uffff’ (or 65,535 inclusive)
- Example: char ABC = ‘A’;
NOTE: Character should be enclosed in single quotes.
class ExampleOfChar { public static void main(String[] args) { char abc = 'a'; System.out.println("Value of char variable : "+ abc); } }
Value of char variable : a
public class ExampleOfChar { char defaultValue; public static void main(String[] args) { // Data type is char hold 2 bytes // Value can be anything but should be in single quote only // Default value will be '\u0000' char num = '1'; System.out.println("Value of char variable : "+ num); char charData = 65; System.out.println("Value of char variable : "+ charData); // It will increment one in "charData" again assigning the new value charData++; System.out.println("Value of char variable :"+ charData); charData = 'C'; System.out.println("Value of char variable :"+ charData); // It will increment one in "charData" again assigning the new value charData++; System.out.println("Value of char variable :"+ charData); charData = '0'; System.out.println("Value of char variable :"+ charData); charData = 'H'; System.out.println("Value of char variable :"+ charData); ExampleOfChar object = new ExampleOfChar(); System.out.println("Default value: "+ object.defaultValue); } }
Output: Value of char variable : 1
Value of char variable : A
Value of char variable :B
Value of char variable :C
Value of char variable 😀
Value of char variable :0
Value of char variable :H
Default value:
NOTE: You can calculate the range of any data type by using the formula:
-2(n-1) to (2(n-1)-1)
Where n is number of bits of primitive data type.
Example of byte data type :
-2(8-1) to (2(8-1)-1) = -128 to 127
2. Non-Primitive data type
- The programmer creates the non-primitive data type in Java. They are also known as the “reference variables” or “object variables”.
- Non-primitive data type variables are created using defined constructors of the classes.
- String, Array, Map, etc. are examples of reference variables.
class ExampleOfString { public static void main(String[] args) { String name = new String(“Ravi”); System.out.println("Value of Ravi variable :"+ name); } }
Output: Value of String variable: Ravi
Data Type | Default Value | Default Size |
boolean | false | 1 bit |
byte | 0 | 1 byte |
char | ‘\u0000’ | 2 bytes |
short | 0 | 2 bytes |
int | 0 | 4 bytes |
long | 0L | 8 bytes |
float | 0.0f | 4 bytes |
double | 0.0d | 8 bytes |
1. Quiz, Read the below code and do answer.
Click on anyone to know the answer.