In programming, we often need to store data for use throughout the program. Variables allow us to store and manipulate this data. Without variables, handling dynamic or large amounts of data would be inefficient and cumbersome.
What is it?
In Java, a variable is a container that holds data that can be changed during the execution of a program. Each variable has a specific data type, which determines what kind of data the variable can store (e.g., integers, floating-point numbers, strings).
Types of Variables in Java:
- Local Variables:
Declared inside methods, constructors, or blocks. They are only accessible within the scope in which they are defined.
2. Instance Variables (Non-static fields):
Declared inside a class but outside methods. They are unique to each instance of the class (i.e., each object created from the class).
3. Static Variables (Class variables):
Declared using the static
keyword. They are shared among all instances of the class.
How are variables declared?
A variable declaration in Java includes the data type, followed by the variable name. Optionally, you can initialize the variable during declaration.
Syntax:
dataType variableName = value;
Example of variable declaration:
int age = 25; // Integer variable double salary = 50000.50; // Floating-point variable String name = "John"; // String variable boolean isActive = true; // Boolean variable
Types of Data for Variables:
- Primitive Types:
int
: For integers (e.g.,int age = 25;
).double
: For floating-point numbers (e.g.,double salary = 50000.50;
).boolean
: For true/false values (e.g.,boolean isActive = true;
).char
: For single characters (e.g.,char grade = 'A';
).- Reference Types:
- These are objects created from classes (e.g.,
String
, custom classes). - Example:
java
String message = "Hello, World!";
User user = new User(); // Custom object
Example of Variable Declaration:
Local Variable Example:
public class Example { public void printAge() { int age = 30; // Local variable System.out.println("Age: " + age); } }
Instance Variable Example:
public class Person { String name; // Instance variable int age; // Instance variable }
Static Variable Example:
public class Person { static String species = "Human"; // Static variable }
Rules for Declaring Variables:
- Variable names must start with a letter, underscore (
_
), or dollar sign ($
). - Example:
int age;
,double _salary;
,String $name;
- Variable names cannot start with a digit.
- Example:
int 2age;
// Invalid - Variable names are case-sensitive.
- Example:
int age;
andint Age;
are different variables. - Variables must be declared before they are used.
- Example:
java
int age;
age = 30; // Valid
Summary:
- Variables are containers for storing data values.
- They are declared with a data type, followed by a variable name.
- Java has different types of variables: local, instance, and static.
- Java supports primitive types (
int
,double
, etc.) and reference types (objects).
In conclusion, variables in Java are essential for storing and manipulating data. They are declared by specifying their type and name, and they allow developers to work with various forms of data efficiently.