Every Variable in Java has a data type. Data types specify the size and types of values that can be stored.
In Java, a data type defines the type of data that a variable can hold. It determines the size and type of values that can be stored in memory.
Java provides a variety of data types, which can be broadly categorized into primitive and non-primitive data types.
Primitive Data Types
Java defines eight primitive types of data: byte, short, int, long, float, double, char, and boolean.
Integer Types
Java supports four types of Integer.
1. byte
Width: 1 byte
Range: Stores small integer values from -128 to 127.
Example:
public class DataTypeExample {
public static void main(String[] args) {
byte smallNumber = 110;
System.out.println("Byte Value: " + smallNumber);
}
}
2. short
Width: 2 byte
Range: Stores medium-range integer values from -32,768 to 32,767.
Example:
public class DataTypeExample {
public static void main(String[] args) {
short mediumNumber = 32000;
System.out.println("Short Value: " + mediumNumber);
}
}
3. int
Width: 4 byte
Range: Stores large integer values from -2,147,483,648 to 2,147,483,647.
Example:
public class DataTypeExample {
public static void main(String[] args) {
int largeNumber = 1000000;
System.out.println("Int Value: " + largeNumber);
}
}
4. long
Width: 6 byte
Range: Stores very large integer values. Must be suffixed with L (e.g., 10000000000L).
Example
public class DataTypeExample {
public static void main(String[] args) {
long veryLargeNumber = 10000000000L;
System.out.println("Long Value: " + veryLargeNumber);
}
}
Floating-Point Types
This group includes float and double, which represent numbers with fractional precision.
1. float
Width: 4 byte
Range: Used for decimal numbers with single precision. Must be suffixed with f (e.g., 10.5f).
Example:
public class DataTypeExample {
public static void main(String[] args) {
float decimalValue = 5.75f;
System.out.println("Float Value: " + decimalValue);
}
}
2. double
Width: 8 byte
Range: Used for decimal numbers with double precision (default for floating-point literals).
Example:
public class DataTypeExample {
public static void main(String[] args) {
double largeDecimalValue = 20.99;
System.out.println("Double Value: " + largeDecimalValue);
}
}
Character Type
1. char: Stores a single 16-bit Unicode character.
Example:
public class DataTypeExample {
public static void main(String[] args) {
char letter = 'A';
System.out.println("Character Value: " + letter);
}
}
Boolean Type
1. boolean: Stores only true or false.
Example:
public class DataTypeExample {
public static void main(String[] args) {
boolean isJavaFun = true;
System.out.println("Boolean Value: " + isJavaFun);
}
}
2. Non-Primitive Data Types
a. String: Stores a sequence of characters.
Example:
public class DataTypeExample {
public static void main(String[] args) {
String message = "Hello, Java!";
System.out.println("String Value: " + message);
}
}
b. Array: Stores a collection of values of the same type.
Example:
public class DataTypeExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40};
System.out.println("Array Value at Index 2: " + numbers[2]);
}
}
Java DataType – Interview Questions
Q 1: What is a data type in Java?
Q 2: How many types of data types are there in Java?
Q 3: Name primitive data types in Java.
Q 4: What is the default data type for decimal values?
Q 5: What is the size of int data type?
Java Data Type – Objective Questions (MCQs)
Q1. Which of the following is a primitive data type in Java?
Q2. What is the size of a long data type in Java?
Q3. What is the default value of a char data type in Java?
Q4. Which of the following data types can store decimal values in Java?
Q5. Which of the following is the correct range for the byte data type in Java?