Variables Naming Rules
Swipe to show menu
Now that you understand what variables are, the next important step is learning how to name them properly. Good naming makes your code easier to read, easier to debug, and easier to maintain in the future.
No Spaces Allowed
Variable names cannot contain spaces — a variable must always be written as a single continuous word.
Invalid:
int user age = 25;
To fix this, we combine words using camelCase:
int userAge = 25;
In camelCase the first word starts with a lowercase letter, and every next word starts with a capital letter:
firstName
playerScore
accountBalance
Cannot Start With a Number
Variable names cannot begin with a number.
Invalid:
int 1score = 100;
Valid:
int score1 = 100;
Numbers are allowed inside or at the end of a name, just not at the beginning.
No Special Symbols
Variable names cannot contain special symbols like -, @, #, or %.
Invalid:
int user-age = 25;
Java interprets - as a minus operator. Instead:
int userAge = 25;
Allowed characters in variable names:
- letters;
- numbers;
- underscores
_; - dollar signs
$(rarely used in practice).
Java Is Case-Sensitive
Java treats uppercase and lowercase letters as completely different:
int age = 25;
int Age = 30;
These are two separate variables. So age, Age, and AGE are all different in Java.
Use Meaningful Names
A meaningful variable name clearly describes what the value represents. Compare these two:
int a = 100;
int playerScore = 100;
Even though both work the same, the second is much better because it immediately tells us what the value represents. A good rule of thumb: if you need to explain what a variable means, the name is probably not good enough.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat