Variables, data types, operators, and type casting are the building blocks of every Java program. Before you can create applications, perform calculations, or process user input, you need to understand how Java stores and manipulates data.
In this article, you'll learn how to declare variables, choose the appropriate data types, use different operators, and convert values from one data type to another using type casting.
Learning Objectives
Understand variables in Java
Declare and initialize variables
Differentiate between primitive and non-primitive data types
Use arithmetic, relational, logical, and assignment operators
Perform implicit and explicit type casting
Write simple Java programs using variables and operators
What are Variables?
A variable is a named memory location used to store data. The value stored in a variable can change while the program is running.
Syntax
dataType variableName = value;
Example
int age = 25;
String name = "John";
double salary = 45000.75;
In the above example:
age stores an integer value.
name stores text.
salary stores a decimal value.
Rules for Naming Variables
When naming variables in Java, follow these rules:
Variable names are case-sensitive.
They must begin with a letter, _, or $.
They cannot start with a number.
Spaces are not allowed.
Java keywords cannot be used as variable names.
Valid Variable Names
studentName
totalMarks
_age
salary2025
Invalid Variable Names
1name
student name
int
Declaring and Initializing Variables
Variables can be declared first and assigned a value later.
int marks;
marks = 90;
Or they can be declared and initialized together.
int marks = 90;
Java Data Types
Java data types are classified into two categories:
Primitive Data Types
Non-Primitive Data Types
Primitive Data Types
Primitive data types store simple values.
Data Type | Description | Example |
byte | Small integer | byte age = 20; |
short | Short integer | short year = 2025; |
int | Integer | int marks = 95; |
long | Large integer | long population = 8000000L; |
float | Decimal (single precision) | float price = 99.5f; |
double | Decimal (double precision) | double salary = 25000.75; |
char | Single character | char grade = 'A'; |
boolean | True or False | boolean passed = true; |
Example Program
public class PrimitiveDataTypes {
public static void main(String[] args) {
int age = 24;
double salary = 35000.50;
char grade = 'A';
boolean passed = true;
System.out.println(age);
System.out.println(salary);
System.out.println(grade);
System.out.println(passed);
}
}
Output
24
35000.5
A
true
Non-Primitive Data Types
Non-primitive data types are objects that can store multiple values or more complex data.
Examples include:
String
Arrays
Classes
Objects
Example:
String course = "Java Programming";
System.out.println(course);
Output
Java Programming
Java Operators
Operators are symbols used to perform operations on variables and values.
Arithmetic Operators
Used for mathematical calculations.
Operator | Description |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (Remainder) |
Example
int a = 20;
int b = 10;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
Output
30
10
200
2
0
Assignment Operators
Assignment operators assign values to variables.
int x = 10;
x += 5;
System.out.println(x);
x -= 2;
System.out.println(x);
Output
15
13
Relational Operators
Relational operators compare two values.
Operator | Meaning |
== | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Example
int a = 10;
int b = 20;
System.out.println(a < b);
System.out.println(a == b);
Output
true
false
Logical Operators
Logical operators combine multiple conditions.
Operator | Meaning |
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
Example
int age = 22;
System.out.println(age > 18 && age < 30);
Output
true
Increment and Decrement Operators
Increase or decrease a variable by one.
int number = 10;
number++;
System.out.println(number);
number--;
System.out.println(number);
Output
11
10
Ternary Operator
The ternary operator is a shorthand for an if-else statement.
Syntax
condition ? value1 : value2;
Example
int marks = 80;
String result = (marks >= 50) ? "Pass" : "Fail";
System.out.println(result);
Output
Pass
Understanding Type Casting
Type casting is the process of converting one data type into another.
Java supports two types of type casting:
Implicit Type Casting (Widening)
Explicit Type Casting (Narrowing)
Implicit Type Casting (Widening)
This conversion happens automatically when converting a smaller data type to a larger data type.
Example
int number = 100;
double value = number;
System.out.println(value);
Output
100.0
Explicit Type Casting (Narrowing)
This conversion is performed manually when converting a larger data type into a smaller data type.
Example
double price = 99.99;
int amount = (int) price;
System.out.println(amount);
Output
99
Notice that the decimal portion is removed during explicit casting.
Complete Example Program
public class JavaBasics {
public static void main(String[] args) {
String student = "David";
int marks = 85;
double percentage = 85.75;
System.out.println("Student: " + student);
System.out.println("Marks: " + marks);
System.out.println("Percentage: " + percentage);
int total = 50 + 40;
System.out.println("Total = " + total);
double average = total;
System.out.println("Average = " + average);
}
}
Output
Student: David
Marks: 85
Percentage: 85.75
Total = 90
Average = 90.0
Common Mistakes
When working with variables and data types, avoid these common errors:
Using a variable before it has been initialized.
Assigning a decimal value to an int without type casting.
Using Java keywords as variable names.
Forgetting to use f with float values.
Confusing = (assignment) with == (comparison).
Practice Exercise
Create a Java program that:
Declares variables for:
Student Name
Age
Marks
Grade
Displays all values using System.out.println().
Calculates the total of two subject marks.
Converts an integer value into a double using implicit type casting.
Converts a double into an integer using explicit type casting.
Still need help?
Contact us