What is a Java Identifier?

An Explanation For What "Identifier" Means in Java Programming

Picture of a woman working on her laptop at her desk
© 2A Images

A Java identifier is a name given to a package, class, interface, method, or variable. It allows a programmer to refer to the item from other places in the program.

To make the most out of the identifiers you choose, make them meaningful and follow the standard Java naming conventions.

Examples of Java Identifiers

If you have variables that hold the name, height, and weight of a person, then choose identifiers that make their purpose obvious:

 String name = "Homer Jay Simpson";
 int weight = 300;
 double height = 6;
 
 System.out.printf("My name is %s, my height is %.0f foot and my weight is %d pounds. D'oh!%n", name, height, weight); 

This to Remember About Java Identifiers

Since there are some strict syntax, or grammatical rules when it comes to Java identifiers (don't worry, they aren't hard to understand), make sure you're aware of these do's and don't:

  • Reserved words like class, continue, void, else, and if cannot be used. Check that link for even more reserved words to avoid
  • "Java letters" is the term given to the acceptable letters that can be used for an identifier. This includes not only regular alphabet letters but also symbols, which just includes, without exception, the underscore (_) and dollar sign ($)
  • "Java digits" include the numbers 0-9
  • An identifier can begin with a letter, dollar sign, or underscore, but not a digit. However, it's important to realize that digits can be used so long as they exist after the first character, likee8xmple
  • Java letters and digits can be anything from the Unicode character set, which means characters in Chinese, Japanese, and other languages, can be used
  • Spaces are not acceptable, so an underscore can be used instead
  • The length does not matter, so you can have a really long identifier if you choose to
  • A compile-time error will occur if the identifier uses the same spelling as a keyword, the null literal, or boolean literal
  • Since the list of SQL keywords may, at some point in the future, include other SQL words (and identifiers can't be spelled the same as a keyword), it's usually not recommended that you use an SQL keyword as an identifier
  • It's recommended to use identifiers that are related to their values so they're easier to remember
  • Variables are case-sensitive, which means myvalue does not mean the same as MyValue

Note: If you're in a hurry, just take away the fact that an identifier is one or more characters that come from the pool of numbers, letters, the underscore, and the dollar sign, and that the first character must never be a number.

Following the rules above, these identifiers would be considered legal:

  • _variablename
  • _3variable
  • $testvariable
  • VariableTest
  • variabletest
  • this_is_a_variable_name_that_is_long_but_still_valid_because_of_the_underscores
  • max_value

Here are some examples of identifiers that are not valid because they disobey the rules mentioned above:

  • 8example(this starts off with a digit)
  • exa+ple (the plus sign isn't allowed
  • variable test (spaces are not valid)
  • this_long_variable_name_is_not_valid_because_of_this-hyphen (while the underscores are acceptable like in the example from above, even the one hyphen in this identifier renders it invalid)
    Format
    mla apa chicago
    Your Citation
    Leahy, Paul. "What is a Java Identifier?" ThoughtCo, Jan. 16, 2017, thoughtco.com/identifier-2034136. Leahy, Paul. (2017, January 16). What is a Java Identifier? Retrieved from https://www.thoughtco.com/identifier-2034136 Leahy, Paul. "What is a Java Identifier?" ThoughtCo. https://www.thoughtco.com/identifier-2034136 (accessed November 23, 2017).