Learn Java Data Types
In Java, data types are used to define the type of data that variables can hold. The data type of a variable specifies the size & the type of values that can be stored in that variable. There are 2 types of data types in Java Programming language: primitive data types and reference data types.
Primitive Data Types:
Java has eight primitive data types, which are predefined in the language. They are:
- byte: It is a 1-byte (8-bit) signed integer data type, with a range from -128 to 127.
- short: It is a 2-byte (16-bit) signed integer data type with a range from -32,768 to 32,767.
- int: It is a 4-byte (32-bit) signed integer data type with a range from -2,147,483,648 to 2,147,483,647.
- long: It is an 8-byte (64-bit) signed integer data type with a range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
- float: It is a 4-byte (32-bit) floating-point data type, which can represent decimal values with 6-7 decimal digits of precision.
- double: It is an 8-byte (64-bit) floating-point data type, which can represent decimal values with 15-16 decimal digits of precision.
- boolean: It is a 1-bit data type that can have only two possible values: true or false.
- char: It is a 2-byte (16-bit) Unicode character data type that can represent any character in the Unicode character set.
Reference Data Types:
Reference data types in Java are objects that are created using class definitions. They are called reference types because they refer to objects. Some examples of reference data types include:
- String: It is a sequence of characters that can be used to represent text in Java.
- Arrays: It is a collection of similar data types that can be stored in a single variable.
- Classes: It is a user-defined data type that can be used to define objects in Java.
In Java, variables must be declared with a data type before they can be used in a program. This helps the Java compiler to determine the types of value that a variable can hold and to allocate the necessary memory space for that variable. Choosing the appropriate data type is important, as it affects the performance and memory usage of a program.
For Example:
int meraNumber = 4; // Integer (whole number)
float meraFloatNum = 99.99f; // Floating point number
char meraLetter = 'H'; // Character
boolean meraBoolean = true; // Boolean
String meraText = "Write Anything"; // String
Code Example:
public class first {
public static void main(String[] args) {
// Integer (whole number)
int meraNumber = 4;
System.out.println("My int number is: " + meraNumber);
// Floating point number
float meraFloatNum = 99.99f;
System.out.println("My float number is: " + meraFloatNum);
// Character
char meraLetter = 'H';
System.out.println("Mera Character number is: " + meraLetter);
// Boolean
boolean meraBoolean = true;
System.out.println("Mera Boolean number is: " + meraBoolean);
// String
String meraText = "Write Anything";
System.out.println("Mera String number is: " + meraText);
}
}
PS C:\Users\Priyanka\Documents\java> java first
My int number is: 4
My float number is: 99.99
Mera Character number is: H
Mera Boolean number is: true
Mera String number is: Write Anything
PS C:\Users\Priyanka\Documents\java>
It’s important to choose the appropriate data type for your variables based on the kind of data you need to store, as this can affect the performance and memory usage of your program.