Java_Day2_DataTypes_Variables
Java_Day2_DataTypes_Variables
• 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**.