Variables, Primitive Data Types (int, double, boolean, char)
In Java, variables store data that can be used and modified during program execution. Every variable must have a type that defines the kind of data it can hold. Primitive data types are the simplest forms of data in Java. The most common include int (whole numbers), double (decimal numbers), boolean (true or false values), and char (single characters). Understanding these types is essential because they form the building blocks for more complex data structures. In this lesson, we will learn how to declare, initialize, and use these variables in a Java program with examples.
Example: Using Java Primitive Data Types
public class PrimitiveTypesExample {
public static void main(String[] args) {
// Integer type
int age = 25;
// Double type
double price = 99.99;
// Boolean type
boolean isJavaFun = true;
// Char type
char grade = 'A';
// Output the values
System.out.println("Age: " + age);
System.out.println("Price: " + price);
System.out.println("Is Java Fun? " + isJavaFun);
System.out.println("Grade: " + grade);
}
}