⬅ Previous Topic
Java CommentsNext Topic ⮕
Java Literals Explained - Types & Examples⬅ Previous Topic
Java CommentsNext Topic ⮕
Java Literals Explained - Types & ExamplesIn Java, a variable is a named container used to store data that can be changed during the execution of a program. Think of it as a labeled box where you can keep values—whether that's numbers, text, or more complex objects.
Variables make your program flexible and dynamic. They allow you to write code that can adapt based on inputs, conditions, or changing data. Without variables, programming would be static and manual.
Declaring a variable means telling Java what type of data the variable will hold and giving it a name. The basic syntax is:
datatype variableName;
Example:
int age;
String name;
double price;
Initialization means assigning a value to the declared variable. You can do this at the time of declaration or later in your code.
int age;
age = 25;
String name = "Alice";
Java is a statically-typed language, which means every variable must have a data type. Here are some commonly used types:
Data Type | Description |
---|---|
int | Stores integers (whole numbers), e.g., 1, 100 |
double | Stores decimal numbers, e.g., 3.14 |
char | Stores a single character, e.g., 'A' |
boolean | Stores true or false |
String | Stores sequences of characters, e.g., "Hello" |
Here’s what you need to know to name your variables properly, along with examples for better understanding:
Names can contain letters, digits, underscores, and dollar signs
// Valid variable names
int total_Amount = 100;
double price2 = 49.99;
String user$name = "John";
Names must begin with a letter, not a digit
// Correct
int count1 = 5;
// Incorrect (will cause a compile-time error)
int 1count = 5; // ❌ Invalid: starts with a digit
Names are case-sensitive (value
and Value
are different)
int value = 10;
int Value = 20;
System.out.println(value); // Outputs: 10
System.out.println(Value); // Outputs: 20
Names should not be Java reserved keywords
// Invalid variable names (these are reserved by Java)
int class = 1; // ❌ Error
boolean public = true; // ❌ Error
// Correct alternative names
int classNumber = 1;
boolean isPublic = true;
public class VariableDemo {
public static void main(String[] args) {
int age = 30;
String name = "John";
boolean isAdult = true;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Is Adult? " + isAdult);
}
}
Name: John
Age: 30
Is Adult? true
Here, the program stores a person’s name, age, and adulthood status in variables. When the program runs, it prints each of those values using System.out.println
. Java concatenates strings using the +
operator.
int name = "Alice";
will throw a compile-time error.double
to an int
without casting.totalAmount
, isAvailable
, rather than x
or data
.userAge
, itemPrice
) for variables.class
or public
as variable names.⬅ Previous Topic
Java CommentsNext Topic ⮕
Java Literals Explained - Types & ExamplesYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.