Data Types
In Java programming, data types define the type of data that a variable can store and also specify the size and value range of that data. Each data type is predefined, which makes Java a statically typed and strongly typed language. This means every variable must be declared with a specific data type before it is used.
Data types help the compiler understand what kind of value a variable holds, such as a number, character, or logical value (true/false).
Types Β (Java data types are mainly divided into two categories:)
1. Primitive Data Types
Primitive data types are the basic data types used to store simple values directly in memory.
- boolean β true or false values
- char β single character (e.g., 'A')
- byte β small integer values
- short β short integer values
- int β integer numbers
- long β large integer numbers
- float β decimal numbers
- double β large decimal numbers
2. Non-Primitive Data Types
Non-primitive data types are used to store complex data and multiple values. These are also called reference types.
- String β sequence of characters (text)
- Array β collection of similar data elements
- Class β blueprint for objects
- Interface β used to achieve abstraction
Example
public class DataTypesExample {
Β Β public static void main(String[] args) {
Β Β Β Β String name = "Hello";
Β Β Β Β int number = 100;
Β Β Β Β float price = 10.5f;
Β Β Β Β char grade = 'A';
Β Β Β Β boolean isJavaEasy = true;
Β Β Β Β System.out.println("String: " + name);
Β Β Β Β System.out.println("int: " + number);
Β Β Β Β System.out.println("float: " + price);
Β Β Β Β System.out.println("char: " + grade);
Β Β Β Β System.out.println("boolean: " + isJavaEasy);
Β Β }
}