π¦ Java Variables β Declare, Initialize & Use Variables in Java
π§² Introduction β What Are Variables in Java?
Every Java program revolves around data β and variables are how we store and manipulate that data. Think of a variable as a container with a label, used to hold different kinds of information like numbers, text, or boolean values.
Whether you’re calculating tax or storing user input, understanding Java variables is a fundamental step.
By the end of this guide, youβll be able to:
- β Declare and initialize variables
- β Understand Java’s variable types and naming rules
- β Use variables in expressions and output
- β Follow best practices for clean, readable code
π What Is a Variable in Java?
A variable in Java is a named memory location that stores a value.
Syntax:
type variableName = value;
β Example:
int age = 25;
β Explanation:
int
: data type (integer)age
: variable name25
: value assigned to the variable
π Types of Variables in Java
Java supports several types of variables, depending on where and how they are declared.
Type | Scope | Defined Inside | Example |
---|---|---|---|
Local | Method | Method or block | int count = 10; |
Instance | Object | Class (outside any method, without static ) | String name; |
Static | Class | Class with static keyword | static int totalUsers; |
π‘ Tip: Always initialize local variables before using them.
π·οΈ Declaring and Initializing Variables
πΉ Declare First, Assign Later:
int score;
score = 100;
πΉ Declare and Assign Together:
String city = "Mumbai";
πΉ Multiple Variables of Same Type:
int x = 10, y = 20, z = 30;
β οΈ Warning: Avoid using undeclared or uninitialized variables β it causes a compile-time error.
π‘ Variable Naming Rules
β Allowed:
- Letters (
aβz
,AβZ
) - Digits (
0β9
) - Underscore (
_
) and Dollar sign ($
)
π« Not Allowed:
- Names starting with a digit
- Reserved keywords (
class
,int
, etc.)
β Examples:
Valid Names | Invalid Names |
---|---|
userName | 1user (starts with digit) |
total_amount | int (Java keyword) |
MAX_SIZE | @value (invalid symbol) |
π‘ Best Practice: Use camelCase for variables (firstName
, totalAmount
).
π Example: Using Variables in Java
public class UserProfile {
public static void main(String[] args) {
String name = "Alice";
int age = 30;
double height = 5.7;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Height: " + height + " feet");
}
}
π¨οΈ Output:
Name: Alice
Age: 30
Height: 5.7 feet
β Explanation:
- Different data types are used (
String
,int
,double
) - All values are printed using
System.out.println()
π§ͺ Java Variable Data Types (Quick Overview)
Type | Description | Example Value |
---|---|---|
int | Integer numbers | 100 , -45 |
double | Decimal numbers | 3.14 , -9.8 |
char | Single character | 'A' , '9' |
boolean | True/false value | true , false |
String | Sequence of characters | "Hello" |
π Youβll explore these in detail in the Java Data Types section.
π§ Summary β Java Variables Essentials
Java variables are the foundation of storing and manipulating data. Youβve learned:
- How to declare and initialize variables
- Types of variables: local, instance, static
- Naming rules and best practices
- How to use variables in Java programs.
βFAQs β Java Variables
β What is a variable in Java?
A variable is a container that holds a value β like a label on a storage box in memory.
β Can a Java variable name start with a number?
No. Variable names cannot begin with digits, but digits can be used after the first character.
β What happens if I donβt initialize a variable?
- Local variables must be initialized before use β or youβll get a compile error.
- Instance variables get default values (
0
,false
,null
, etc.).
β Whatβs the difference between String
and char
?
char
: stores a single character ('A'
)String
: stores a sequence of characters ("Apple"
)
Share Now :