Java Data Types

In Java, every variable must have a specified data type. This determines the kind of values the variable can hold.

Example:

int age = 25;               // Integer (whole number)
float height = 5.75f;       // Floating-point number
char initial = 'A';         // Character
boolean isStudent = true;   // Boolean
String greeting = "Hello";  // String




Types of Data Types

Data types in Java are divided into two main categories:

  1. Primitive Data Types
  2. Non-Primitive Data Types




Primitive Data Types

Primitive data types specify the type of a variable and the kind of values it can hold. Java has eight primitive data types:

Data TypeDescription
byteStores whole numbers from -128 to 127
shortStores whole numbers from -32,768 to 32,767
intStores whole numbers from -2,147,483,648 to 2,147,483,647
longStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
floatStores fractional numbers, sufficient for storing 6 to 7 decimal digits
doubleStores fractional numbers, sufficient for storing 15 to 16 decimal digits
booleanStores true or false values
charStores a single character/letter or ASCII values




Non-Primitive Data Types

Non-primitive data types include classes, arrays, and interfaces. For now, we’ll focus on one common non-primitive type: String.

Example:

String city = "Manila";




Examples of Using Data Types

In this example, we declare different types of variables and print their values using the println() method.

int temperature = 28;          // Integer
double pi = 3.14159;           // Double
char grade = 'B';              // Character
boolean isJavaFun = true;      // Boolean
String country = "Philippines"; // String

System.out.println("Temperature: " + temperature);
System.out.println("Pi: " + pi);
System.out.println("Grade: " + grade);
System.out.println("Is Java fun? " + isJavaFun);
System.out.println("Country: " + country);

Scroll to Top