answersLogoWhite

0

Some basic conventions are:
1. you cannot have keywords as names
2. All identifiers have to start with an alphabet
3. Except '_' and '$' no other special character is allowed in names
4. When there are multiple words in an identifier we usually capitalize the first alphabets of each word. For example if an identifier represents name of employee then we name it as nameOfEmployee. The first word would remain normal with no capitalization

etc...

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
BlakeBlake
As your older brother, I've been where you are—maybe not exactly, but close enough.
Chat with Blake
ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
More answers

All variable names must begin with a letter of the alphabet, an underscore ( _ ), or a dollar sign ($). The rest of the characters may be any of those previously mentioned plus the digits 0-9.

The convention is to always use a letter of the alphabet. The dollar sign and the underscore are discouraged.

User Avatar

Wiki User

15y ago
User Avatar

You can name your variable anything you like, using most symbols. A few symbols are excluded, like a hyphen (which would be confused with a minus), and the first symbol for the variable name can't be a digit. However, try not to use a lot of weird symbols; just using letters, digits, and the underscore is probably safest.

There are also certain recommended conventions for naming variables. The compiler won't complain if you don't follow them, but they certainly help to make your program more readable. Mainly:

  • Write the variable name in lowercase. Examples: age, person.
  • If the variable name contains several words, start every word, except the first, with uppercase. Examples: dateOfBirth, anualSalary.
  • Give your variables meaningful names. In the ideal case, just looking at the variable name should tell a programmer what it is about, without reading lots of comments in the program.
  • Constants are written all in uppercase. To separate words, use the underscore (low hyphen). Example: DAYS_PER_WEEK (which would be defined as 7).
  • Names for methods follow the same conventions as variables.
  • Names for classes start with an uppercase letter. Example: String, Calendar. Once again, if the name contains several words, start each word with an uppercase letter. Example: ComplexNumber.

Search for the variable naming conventions from Sun, for more details on the recommended naming conventions.

User Avatar

Wiki User

15y ago
User Avatar

Add your answer:

Earn +20 pts
Q: What are the rules for naming variables in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp