Introduction to Java | Programming Basics
Java Variables
Variables in Java are used to store data that can change during the execution of a program. A variable in Java is a container that holds a value and has a specific data type associated with it. The data type determines the kind of value the variable can store.
Types of Variables in Java
There are several types of variables in Java:
- Local Variables: Declared inside a method or block and can only be used within that method/block.
- Instance Variables: Declared inside a class but outside of any method. These are associated with instances of a class.
- Class Variables (Static Variables): Declared with the
static
keyword. These variables are shared across all instances of a class.
Declaring and Initializing Variables
A variable is declared with a type, followed by its name. Optionally, a value can be assigned to the variable during declaration or afterward. Here's how you declare and initialize a variable in Java:
Code Example
public class VariableExample {
public static void main(String[] args) {
// Declaring variables
int age = 25; // Integer variable
double salary = 45000.50; // Double variable
char grade = 'A'; // Character variable
boolean isStudent = true; // Boolean variable
// Outputting variable values
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Grade: " + grade);
System.out.println("Is Student: " + isStudent);
}
}
Output
Salary: 45000.5
Grade: A
Is Student: true
Variable Naming Rules
In Java, there are certain rules for naming variables:
- The name must start with a letter (a-z, A-Z), underscore (_), or a dollar sign ($).
- Subsequent characters can include letters, numbers (0-9), underscores, and dollar signs.
- Java is case-sensitive, meaning
myVariable
andMyVariable
are different. - Variable names cannot be reserved keywords (e.g.,
int
,class
,public
).
Example of Variable Naming
Code Example
public class VariableNamingExample {
public static void main(String[] args) {
int myAge = 30; // Correct variable name
double salaryAmount = 60000.75; // Correct variable name
// int 2ndAge = 28; // Invalid, variable cannot start with a number
// String class = "Math"; // Invalid, 'class' is a reserved keyword
}
}
Data Type Conversion (Type Casting)
Sometimes, you may need to convert one data type into another. This is called type casting in Java. There are two types of type casting:
- Implicit Casting (Widening Conversion): When a smaller type is automatically converted to a larger type.
- Explicit Casting (Narrowing Conversion): When a larger type is manually converted to a smaller type.
Example of Type Casting
Code Example
public class TypeCastingExample {
public static void main(String[] args) {
// Implicit casting (widening)
int num = 100;
double d = num; // int to double (automatic)
// Explicit casting (narrowing)
double pi = 3.14;
int intPi = (int) pi; // double to int (manual)
System.out.println("Double: " + d);
System.out.println("Integer: " + intPi);
}
}
Output
Integer: 3
Practice Exercises
- Write a program to swap the values of two variables without using a third variable.
- Create a program to calculate the area of a circle, where the radius is a variable.
- Write a program to convert a temperature from Celsius to Fahrenheit using variables.
💡 Pro Tip
Always give your variables meaningful names so that your code is easier to understand and maintain.