Java - Day 2: Data Types,
Variables & Constants
Understanding fundamental building
blocks of Java programs
What are Data Types?
• - Data types define the kind of values a
variable can hold.
• - Java is **strongly typed**, meaning each
variable has a specific type.
• - Two main categories: **Primitive Data
Types** and **Reference Data Types**.
Primitive Data Types
• 1. **Integer Types**: byte, short, int, long
• 2. **Floating-Point Types**: float, double
• 3. **Character Type**: char
• 4. **Boolean Type**: boolean
• Example:
• ```java
• int age = 25;
• double price = 99.99;
• boolean isJavaFun = true;
• char grade = 'A';
• ```
Reference Data Types
• - These store references (memory addresses)
instead of actual values.
• - Includes: **Strings, Arrays, Classes,
Interfaces**.
• - Example:
• ```java
• String name = "John";
• int[] numbers = {1, 2, 3, 4, 5};
• ```
Variables in Java
• - A variable is a container for storing values.
• - **Declaration & Initialization**:
• ```java
• int number; // Declaration
• number = 10; // Initialization
• ```
• - Variable Types: **Local, Instance, Static**.
Constants in Java
• - A constant is a fixed value that cannot be
changed.
• - Declared using the `final` keyword.
• - Example:
• ```java
• final double PI = 3.14159;
• ```
Type Casting in Java
• - **Implicit Casting (Widening Conversion)**: Automatic
conversion from smaller to larger type.
• - **Explicit Casting (Narrowing Conversion)**: Requires
manual conversion.
• - Example:
• ```java
• int num = 10;
• double d = num; // Implicit casting
• double x = 9.7;
• int y = (int) x; // Explicit casting
• ```
Recap & Next Steps
• - Java has **primitive and reference data
types**.
• - Variables store values, while **constants**
hold fixed values.
• - **Type casting** allows conversion between
data types.
• - Next: **Operators & Expressions in Java**.