Understanding variables and data types is one of the first and most important steps in learning Java. Variables allow us to store data in memory, while data types tell Java what kind of data can be stored.
In this lesson, we will understand:
-
What are variables in Java?
-
What are data types?
-
How variables are related to memory
-
Variable naming rules
-
Primitive and non-primitive data types
-
All 8 primitive data types
-
Memory size and ranges
-
How to declare and initialize variables
-
Integer, decimal, character, and boolean variables
-
Default types of integer and decimal literals
-
Landfsuffixes in Java
1. What is a Variable in Java?
Before understanding variables, let's take a simple real-world example.
Imagine that you have different things at home:
-
Books
-
Money
-
Clothes
You would normally keep books in a bookshelf, money in a locker, and clothes in an almirah.
Why?
Because different places are intended to store different types of things.
The same concept applies to computer memory.
When Java creates a particular memory location for storing data, that location can be associated with a specific type of data. Java uses variables and data types to manage this information.
For example, suppose we create a memory location where we want to store a number:
int age = 25;
Here:
-
int→ Data Type -
age→ Variable Name -
25→ Value
The variable age represents the memory location where the value 25 is stored.
A variable is therefore a named storage location used to store a value that can be accessed later in a program.
2. What is a Data Type?
A data type tells Java what kind of data a variable is intended to store.
For example:
int age = 25;
Here, int tells Java that age is intended to store an integer value.
Similarly:
boolean isLoggedIn = true;
Here, boolean tells Java that the variable can store a boolean value such as true or false.
The data type helps Java determine what kind of value can be stored in that particular variable.
For example, if a variable is declared as an integer type, Java will not allow arbitrary text to be stored in it.
3. Variables and Data Types Together
A simple way to remember the difference is:
Variable = Where / Name
Data Type = What kind of data
For example:
int marks = 95;
Here:
| Part | Meaning |
|---|---|
int |
Data Type |
marks |
Variable |
= |
Assignment Operator |
95 |
Value |
The variable gives a name to the storage location, while the data type defines the type of data that can be stored there.
4. Variable Naming Rules in Java
Java has certain rules that must be followed while creating variable names.
Rule 1: Variable names cannot start with a number
This is invalid:
int 45rohan = 10;
But this is valid:
int rohan45 = 10;
A variable name can start with a letter, _, or $.
Rule 2: Special characters are not allowed
Java allows $ and _ in identifiers, but other special characters cannot be used.
Valid:
int student_name = 10;
int student$name = 10;
Invalid:
int student-name = 10;
The - character is not allowed in a variable name.
Rule 3: White spaces are not allowed
You cannot use spaces inside a variable name.
Invalid:
int student name = 10;
Instead, you can use:
int student_name = 10;
or preferably:
int studentName = 10;
Rule 4: Reserved Keywords cannot be used
Java has certain reserved keywords that already have a specific meaning.
For example:
public
class
static
void
return
while
You cannot use these keywords as variable names.
For example:
int while = 10;
This is invalid because while is a Java keyword.
Rule 5: Java is Case-Sensitive
Java treats uppercase and lowercase letters as different.
For example:
int age = 20;
int Age = 25;
int AGE = 30;
These are three different variable names.
Similarly:
rohan
Rohan
ROHAN
are considered different identifiers.
Rule 6: Use Meaningful Variable Names
Although you can technically create a variable such as:
int abc = 25;
it doesn't tell another developer what abc represents.
A better approach is:
int studentAge = 25;
Now, simply looking at the variable name tells us that the value represents the student's age.
Meaningful variable names make programs easier to read and maintain.
Rule 7: Prefer camelCase
Java developers commonly use camelCase for variable names.
For example:
studentName
studentAge
totalMarks
phoneNumber
accountBalance
In camelCase:
-
The first word starts with a lowercase letter.
-
Every following word starts with an uppercase letter.
For example:
studentName
instead of:
student_name
Both can be valid identifiers, but camelCase is the commonly preferred naming convention for Java variables.
5. Types of Data Types in Java
Java data types can broadly be divided into two categories:
-
Primitive Data Types
-
Non-Primitive Data Types
Your Java course will cover non-primitive types such as String and arrays separately. In this lesson, our main focus is on primitive data types.
6. Primitive Data Types in Java
Java has 8 primitive data types:
| Data Type | Purpose |
|---|---|
byte |
Small integer values |
short |
Integer values |
int |
Integer values |
long |
Large integer values |
float |
Decimal values |
double |
Decimal values |
char |
Single character |
boolean |
true or false |
The 8 primitive data types covered in the lecture are byte, short, int, long, float, double, char, and boolean.
7. Integer Data Types
Java provides four primitive data types for storing integer values:
-
byte -
short -
int -
long
The main difference between them is their storage size and range.
| Data Type | Size | Range |
|---|---|---|
byte |
1 byte | -128 to 127 |
short |
2 bytes | -32,768 to 32,767 |
int |
4 bytes | -2,147,483,648 to 2,147,483,647 |
long |
8 bytes | Larger integer range |
The lecture explains these sizes and the byte and short ranges directly, and uses the int maximum value 2,147,483,647 in the examples.
Why do we need four integer types?
You might wonder:
If all four store integer values, why do we need four different data types?
The answer is storage capacity.
For example, if you only need to store a person's age:
byte age = 25;
Using a smaller suitable type avoids allocating a larger integer type when it isn't necessary.
The lecture uses age as an example to explain why different integer types exist.
8. byte
The byte data type uses 1 byte, which is equal to 8 bits.
Its range is:
-128 to 127
Example:
byte age = 25;
System.out.println(age);
This is valid because 25 falls within the range of byte.
But:
byte age = 128;
will produce an error because 128 is outside the range of byte.
9. short
The short data type uses 2 bytes, or 16 bits.
Its range is:
-32,768 to 32,767
Example:
short marks = 495;
System.out.println(marks);
The lecture demonstrates this type using a short variable.
10. int
int uses 4 bytes, or 32 bits.
It is commonly used when working with integer values.
Example:
int population = 14823;
System.out.println(population);
The lecture also explains that an integer literal such as:
14823
is treated by Java as an int by default.
11. long
long uses 8 bytes, or 64 bits, and is used when we need to store larger integer values.
For example:
long number = 2147483648L;
Notice the L at the end.
Java normally treats an integer literal as an int. Therefore, when we want to represent a larger integer literal as a long, we can add L:
2147483648L
Both uppercase L and lowercase l can be used, although uppercase L is generally easier to read.
Example:
long number = 2147483648L;
System.out.println(number);
The lecture demonstrates why the L suffix is required when the literal exceeds the int range.
12. Floating-Point Data Types
Java provides two primitive data types for decimal values:
-
float -
double
float
float uses 4 bytes.
Example:
float price = 0.23f;
System.out.println(price);
Notice the f at the end.
A decimal literal such as:
0.23
is treated as a double by default.
Therefore, this produces a type mismatch:
float price = 0.23;
To explicitly represent the value as a float, use:
float price = 0.23f;
The lecture demonstrates this exact issue and explains the use of the f suffix.
13. double
double uses 8 bytes and is another data type for decimal values.
Example:
double price = 0.23;
System.out.println(price);
Here, we don't need to add a suffix because decimal literals are treated as double by default.
The lecture demonstrates this with:
double rst = 0.23;
which can be stored directly in a double variable.
14. char
The char data type is used to store a single character.
A character is written inside single quotes:
char grade = 'A';
You can also store characters such as:
char symbol = '$';
char number = '3';
Notice that '3' is a character, whereas 3 is an integer value.
A char can contain only one character.
For example:
char letter = 'A';
is valid.
But:
char letter = 'ABC';
is not valid because more than one character is being provided.
The lecture demonstrates characters using single quotes and explains that a char stores one character.
15. boolean
The boolean data type is used when we need to represent a logical value.
It can contain only:
true
false
Example:
boolean isLoggedIn = true;
System.out.println(isLoggedIn);
Another example:
boolean isPassed = false;
System.out.println(isPassed);
The lecture demonstrates both true and false values using the boolean data type.
16. Variable Declaration and Initialization
Now let's understand how to create a variable in Java.
The general syntax is:
DataType variableName = value;
For example:
byte age = 25;
There are four parts:
-
Data Type
-
Variable Name
-
Assignment Operator (
=) -
Value
-
Semicolon (
;)
The lecture introduces this same syntax before moving into practical examples.
17. Complete Example
Let's create a simple Java program using a variable.
public class Main {
public static void main(String[] args) {
byte age = 25;
System.out.println(age);
}
}
Here:
byte age = 25;
creates a variable named age of type byte and stores 25 in it.
Then:
System.out.println(age);
prints the value stored inside the variable.
The lecture demonstrates the same declaration-and-print process using byte abc = 45.
18. Examples of All Primitive Data Types
Here is a quick example of the eight primitive data types:
byte age = 25;
short marks = 500;
int population = 14823;
long distance = 2147483648L;
float price = 0.23f;
double percentage = 95.75;
char grade = 'A';
boolean isPassed = true;
These examples show how different types of values can be represented using Java's primitive data types.
19. Important Literal Rules to Remember
There are two important rules from this lesson.
Integer literals
By default, an integer literal is treated as an int.
For example:
int number = 100;
For a larger value that needs to be represented as a long:
long number = 2147483648L;
Decimal literals
By default, a decimal literal is treated as a double.
Therefore:
double price = 0.23;
works directly.
For float, use:
float price = 0.23f;
The lecture specifically demonstrates both of these default-literal behaviors.
20. Quick Revision
Let's quickly revise everything we learned.
Variables
A variable is a named storage location used to store data.
Example:
int age = 25;
Data Types
A data type specifies the kind of data that can be stored in a variable.
Primitive Data Types
Java has 8 primitive data types:
byte
short
int
long
float
double
char
boolean
Integer Types
byte → 1 byte
short → 2 bytes
int → 4 bytes
long → 8 bytes
Decimal Types
float → 4 bytes
double → 8 bytes
Character
char grade = 'A';
Boolean
boolean isPassed = true;
Variable Naming
Remember:
-
Don't start with a number.
-
Don't use spaces.
-
Don't use invalid special characters.
-
Don't use Java reserved keywords.
-
Java is case-sensitive.
-
Prefer meaningful names.
-
Prefer camelCase for variable names.
Conclusion
In this lesson, we learned the fundamentals of Variables and Data Types in Java.
We started by understanding the concept of variables using a real-world storage analogy. Then we learned how data types determine what kind of data can be stored in a variable.
We also explored Java's 8 primitive data types:
byte, short, int, long, float, double, char, and boolean.
Finally, we learned how to declare variables, assign values to them, print their values, and follow Java's variable naming rules.
Understanding variables and data types is essential because almost every Java program will work with data in some form.
In the next lesson, we can move forward to understanding how values can be converted from one data type to another, including concepts such as type conversion and type casting.