📘 Java Notes – Introduction to Data Types and Variables
Description:
This document explains Java's fundamental data types and how variables are declared and
used. It serves as class notes for beginners learning about memory storage, literals, type
casting, and best practices for naming variables.
🧠 What are Data Types?
In Java, data types specify the size and type of values that can be stored in a variable. Java
supports two main categories:
Primitive Data Types
Non-Primitive Data Types
🔢 Primitive Data Types
Java has 8 built-in (primitive) data types:
Data Type Size Description
byte 1 byte Whole number from -128 to 127
short 2 bytes Whole number from -32,768 to 32,767
int 4 bytes Whole number, default type
long 8 bytes Very large whole number
float 4 bytes Fractional numbers (e.g. 3.14)
double 8 bytes Precise decimal numbers
char 2 bytes A single character ('A')
boolean 1 bit true or false
📌 Example Code: Declaring Variables
java
CopyEdit
int age = 25;
float height = 5.9f;
char grade = 'A';
boolean isPassed = true;
🔄 Type Casting in Java
Type casting is converting one data type into another.
Widening Casting (automatic): int → float → double
Narrowing Casting (manual): double → float → int
java
CopyEdit
int num = 10;
double result = (double) num; // Explicit casting
📛 Rules for Naming Variables
1. Must begin with a letter or underscore (_)
2. Cannot use reserved keywords (like int, class, etc.)
3. Case-sensitive (Age and age are different)
💡 Practice Task
Write a Java program that declares one variable for each primitive data type and prints all of
them.
3
4
5
6
7
8
9
10
Educational Use:
Builds foundational understanding of loops in Java — key for tasks that require repetition or
iteration.