A data type of a variable in java determines the values it may contain, how much memory is required to store this data, plus the operations that may be performed on it. By creating variables of different data types, we can store integers, floating point numbers, boolean values etc.
Java programming language supports two types of data types primitive and Non-primitive data types. A primitive data type is predefined by the language and is named by a reserved keyword. In this tutorial, we will learn about primitive data types. There are eight primitive data types supported by Java programming language:
Data types are fundamental building blocks in any programming language, defining the nature of variables and the operations that can be performed on them. Java, being a statically-typed language, requires explicit declaration of data types for variables. In this tutorial, we will delve into the various data types in Java, covering primitive data types, reference data types, and how to effectively use them in your programs.
byte
- Keyword byte is used to declare variables.
- The byte data type is an 8-bit signed two's complement integer.
- The range of byte data type if from -128 to 127(inclusive).
- We can use variables of byte data types instead of integer data type to save memory. We should use byte data types only when we know the range of values we are dealing with will always be in the range of byte data type.
byte count = 100;
short
- Keyword short is used to declare variables.
- The short data type is a 16-bit signed two's complement integer.
- The range of short data type is from -32,768 to 32,767 (inclusive).
- We can use variables of short data types instead of integer data type to save memory.
- As the range of short data type is more than byte, we can use it to store values beyond the range of byte variables.
short i = -15000, j = 12000;
int
- Keyword int is used to declare integer variables.
- The int data type is a 32-bit signed two's complement integer.
- The range of int data type is from - 2,147,483,648 (-2^31) to 2,147,483,647(2^31 -1)(inclusive).
- We can use variables of int data types if we are not concerned about memory or when you are dealing with number whose values are beyond range of short data types.
int score = 5236521;
long
- Keyword long is used to declare variables.
- The long data type is a 64-bit two's complement integer.
- The range of long data type is from -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive).
- We can use variables of type long when we are dealing with number larger than the range of int data types.
long amount = 645374587L;
float
- Keyword float is used to declare variables.
- The float data type is a single-precision 32-bit IEEE 754 floating point.
- float data type takes less memory than double but float variables cannot store very precise values like double.
float length = 345.6f;
double
- Keyword double is used to declare variables.
- The double data type is a double-precision 64-bit IEEE 754 floating point.
- Double data type is default for decimal numbers.
- double data types takes more memory than float but are more precise than float.
double area = 543.76;
boolean
- Keyword boolean is used to declare variables.
- The boolean data type has only two possible values: true and false.
- We use this variable as a flag to store binary information.
- It stores 1 bit of information but it's actual size is not defined properly.
boolean isComplete = true;
char
- Keyword char is used to declare variables.
- The char data type is a single 16-bit Unicode character.
- The range of char data type is from '\u0000' (0) to '\uffff'(65,535)(inclusive).
- We use char data type to store one character.
char c = 'A';
Default Values of Data Types
It is not compulsory to initialize a variable while declaration. If we don't initialize a variable the compiles assigns a default value as per the data type of the variable. However, it is a good programming practice to initialize a variable during declaration. Here are the default values of various data types:
Data Type | Default Value |
---|---|
byte | 0 |
short | 0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
char | '\u0000' |
boolean | false |
Here is the Java program to demonstrate the declaration, initialization and printing of primitive data types.
public class DataTypes { public static void main(String args[]) { byte b = 100; System.out.println("Byte : " + b); short s = 50; System.out.println("Short : " + s); int i = 5236521; System.out.println("Int : " + i); long l = 645374587L; System.out.println("Long : " + l); float f = 345.6f; System.out.println("Float : " + f); double d = 543.76; System.out.println("Double : " + d); boolean bl = true; System.out.println("Boolean : " + bl); char c = 'A'; System.out.println("Char : " + c); } }Output
Byte : 100 Short : 50 Int : 5236521 Long : 645374587 Float : 345.6 Double : 543.76 Boolean : true Char : A
Implicit and Explicit Type Casting
Implicit Type Casting
Implicit type casting occurs when a smaller data type is converted to a larger data type. Java automatically performs this conversion without the need for explicit instructions.
int myInt = 50; long myLong = myInt; // Implicit casting from int to long
Explicit Type Casting
Explicit type casting is required when converting a larger data type to a smaller one. This conversion may result in data loss, and it requires manual intervention.
double myDouble = 3.14; int myInt = (int) myDouble; // Explicit casting from double to int
Conclusion: Mastering Data Types in Java
Understanding data types is fundamental to effective Java programming. Whether working with primitive data types for simple values or reference data types for complex structures, choosing the right data type is essential for efficient memory usage and program correctness.
As you continue your Java journey, experiment with different data types, practice type casting, and explore the nuances of each type in various scenarios. Mastering data types will empower you to write robust, efficient, and maintainable code in Java, contributing to your growth as a proficient programmer.