Day 2: Java Variables and Data Types – Learn Java for Beginners
Welcome back to Day 2 of our 100 Days of Java course. Yesterday, we installed Java, set up Eclipse, and ran our first program. Today, we’ll dive into the foundation of every Java program, variables and data types.
By the end of this lesson, you’ll understand how Java stores and manages data, and you’ll be writing your own variable-based programs confidently.
What Are Variables in Java?
Think of a variable as a labeled container where you can store data.
You can put something in it, use it, or even replace it later.
For example:
int age = 25;
String name = "John";
double height = 5.9;
Here’s what’s happening:
int age = 25;
→ stores an integer valueString name = "John";
→ stores text (sequence of characters)double height = 5.9;
→ stores a decimal number
In Java, you must declare the type of data before using a variable, that’s why it’s called a strongly typed language.
Java Data Types Explained
Java has two main categories of data types:
1. Primitive Data Types
These are the basic building blocks:
Type | Size | Example | Description |
---|---|---|---|
byte | 1 byte | byte age = 20; | Small integers |
short | 2 bytes | short salary = 32000; | Medium integers |
int | 4 bytes | int marks = 95; | Common integer type |
long | 8 bytes | long population = 8000000L; | Large integers |
float | 4 bytes | float weight = 65.5f; | Decimal numbers (less precise) |
double | 8 bytes | double price = 199.99; | Decimal numbers (more precise) |
char | 2 bytes | char grade = 'A'; | Single characters |
boolean | 1 bit | boolean isJavaFun = true; | True or false values |
2. Non-Primitive Data Types
These include Strings, Arrays, Classes, and Objects.
Example:
String language = "Java";
Strings are actually objects, not primitive types, but they behave like basic data in most cases.
Example: Using Variables in Java
Let’s write a simple program to use all these types together.
public class VariablesDemo {
public static void main(String[] args) {
int age = 25;
String name = "John";
double height = 5.9;
char grade = 'A';
boolean isJavaFun = true;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Height: " + height);
System.out.println("Grade: " + grade);
System.out.println("Java is fun: " + isJavaFun);
}
}
Output
Name: John
Age: 25
Height: 5.9
Grade: A
Java is fun: true
This is how Java variables and data types help you store and organize data in your programs.
Pro Tip
If you’re unsure which type to use:
- Use
int
for whole numbers. - Use
double
for decimals. - Use
String
for text. - Use
boolean
for true/false values.
Java automatically handles memory efficiently, but choosing the right type keeps your programs fast and clean.
What’s Next?
You’ve mastered Java variables and data types. Next, in Day 3, we’ll explore Java operators, the tools you’ll use to perform calculations and logic.
You can also check out:
- Learn Java Basics (Day 1) – Install JDK and Run Your First Program
- Beginner’s Guide to Web Development
About Us
- Akhil Boddu’s YouTube Channel – Follow my vlogs for personal experiences and tips.
- Code With Me YouTube Channel – Explore tech tutorials and coding advice.
- WeblogTrips LinkedIn – Connect with me on LinkedIn for more insights and discussions.
If the information you are looking for is not available here, please contact us. Additionally, follow us on our social media platforms for updates and more information.