DMCA.com Protection Status Java Programming Lesson 2 | Variables & Data Types Explained - ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer

Header Ads

Java Programming Lesson 2 | Variables & Data Types Explained

LESSON - 2 VERIABLES AND DATA TYPES





Understanding variables and data types is the foundation of Java programming. Without learning how to store, update, and use data, you cannot build real-world applications.
In this lesson, we will break down everything simply—perfect for beginners.


⭐ What Are Variables in Java?

A variable is like a container in memory where we store values.

✔ Example

int age = 20; String name = "John";

Here:

  • int and String are data types
  • age and name are variables
  • 20 and "John" are values

✔ Rules for Naming Variables

  • Must start with a letter, $, or _
  • Cannot start with a number
  • No spaces
  • Case-sensitive (age and Age are different)

✔ Good vs Bad Examples

✔ Good:

studentName, totalMarks, user_age

✘ Bad:

1age, user age, total-marks


⭐ Why Do We Use Variables?

Variables help programmers:

  • Store data
  • Process and manipulate information
  • Make programs dynamic and interactive
  • Reuse values anytime

Imagine creating a calculator, game, or website backend—variables make everything possible.


Java Data Types Explained

Java has two main categories of data types:


1. Primitive Data Types (Built-in Types)

Java provides 8 primitive types.

Type          SizeExampleMeaning
byte        1 byte          byte a = 10;      Small integers
short        2 bytes          short s = 200;      Medium integers
int        4 bytes          int age = 21;      Default integer type
long        8 bytes          long views = 100000L;      Large integers
float        4 bytes          float price = 10.5f;      Decimal numbers
double        8 bytes          double salary = 50000.75;    High precision decimals
char        2 bytes          char grade = 'A';       Single characters
boolean        1 bit          boolean isOnline = true;       True/false values

📝 Example Program Using All Primitive Types

public class DataTypesExample { public static void main(String[] args) { byte age = 25; short year = 2025; int population = 150000; long stars = 9999999999L; float rating = 4.5f; double pi = 3.14159; char grade = 'A'; boolean isJavaFun = true; System.out.println("Age: " + age); System.out.println("Year: " + year); System.out.println("Population: " + population); System.out.println("Stars: " + stars); System.out.println("Rating: " + rating); System.out.println("Pi Value: " + pi); System.out.println("Grade: " + grade); System.out.println("Java Fun? " + isJavaFun); } }

2. Non-Primitive (Reference) Data Types

These include:

  • String
  • Arrays
  • Classes
  • Objects

✔ Example of Non-Primitive Type
String name = "Alice";

Differences from primitive types:

  • Created by the programmer
  • Can store multiple values
  • Have methods and behaviors


⭐ String – Most Important Non-Primitive Type

String city = "Dhaka"; System.out.println(city.toUpperCase());

Output:

DHAKA

How to Choose the Right Data Type

Use Case                                Best Data Type
Counting, age, number                      int
Large number                      long
Decimal number                  float / double
A single letter                      char
Yes/No value                    boolean
Text or words                     String

Complete Beginner Example

public class VariablesExample { public static void main(String[] args) { int age = 21; float height = 5.7f; char grade = 'A'; boolean isStudent = true; String name = "Jitu Hasan"; System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Height: " + height); System.out.println("Grade: " + grade); System.out.println("Is Student: " + isStudent); } }

✅ Output:

Name: Jitu Hasan Age: 21 Height: 5.7 Grade: A Is Student: true

Summary of Lesson 2

✔ Variables store information
✔ Java has 8 primitive data types
✔ Non-primitive types include String, Arrays, Objects
✔ Strings are most commonly used
✔ Naming variables correctly is important


✅ Naming Rules (Important Interview Question)

✔ Allowed: letters, numbers, _, $
✔ Must NOT start with a number
✔ Case-sensitive (age ≠ Age)
✔ No spaces

✅ Good:
studentName, totalMarks, age2

❌ Bad:

2amount, student name, class

✅ Mini Exercise (Do it Now)

Write a program using variables to print:

📌 Your Name
📌 Your Age
📌 Your Country
📌 Your Favorite Programming Language

Example Output:

My Name: _____ Age: _____ Country: _____ Favorite Language: _____

👉 Reply here with your code & output, I will check and improve it ✅


🎯 Quick Quiz

1️⃣ Which is a non-primitive data type?
A) int
B) String
C) boolean

2️⃣ Which is correct?
A) float x = 5.5;
B) float x = 5.5f;

Reply like: 1 → ?, 2 → ?


✅ Lesson-2 Completed!

Tell me when you're ready for:

Lesson-3: Operators (Arithmetic, Logical, Relational)

Waiting for your answers + exercise 👇😊


NEXT PARTLesson-3: Operators

Frequently Asked Questions - (FAQs)

What is a variable in Java?

A variable in Java is a reserved memory location used to store data. Each variable has a name, a type, and a value that can change during program execution.

What are data types in Java?

Data types specify the kind of data a variable can store. Java supports primitive data types (int, double, char, boolean, etc.) and non-primitive types (String, arrays, classes).

What is the difference between primitive and non-primitive data types?

Primitive types store simple values directly in memory, while non-primitive types store references to objects. Primitive types are fast and memory-efficient, whereas non-primitive types provide more flexibility.

How do you declare a variable in Java?

You declare a variable using the format: dataType variableName = value;. For example: int age = 20;.

Can a variable value be changed in Java?

Yes. Variables declared without the final keyword can have their values updated anytime during runtime.

What is type casting in Java?

Type casting converts one data type into another, such as converting double to int. Java supports implicit (automatic) and explicit (manual) casting.

What is a String in Java?

A String is a non-primitive data type used to store sequences of characters. It is treated as an object and belongs to the Java String class.

No comments

Thank You For Visit My Website.
I Will Contact As Soon As Possible.

Theme images by 5ugarless. Powered by Blogger.