Java Identifiers Explained

What exactly is a Java Identifier? So glad you asked because you may see it on a java certification exam or be asked during an interview. Identifiers are the names of a variable, method, class, package, or interface. There are rules in Java that specify how you things can be named. There are some rules for defining valid java identifiers. If the rules are not followed, you get compile-time errors.

Identifier Naming Rules

  • Must be composed of letters, numbers, underscore, and dollar sign.
  • Must begin with a letter, underscore, or dollar sign.
  • Must not begin with a number.
  • Must not contain any white space.
  • There is no limit on naming conventions.

Valid Naming Examples

  • MyInteger
  • myInteger
  • MYINTEGER
  • x
  • _MyInteger
  • _myInteger
  • $MyInteger
  • $myInteger
  • My_Really_Long_Variable_At_Java_Code_depot

Invalid Naming Examples

  • My Integer /*can’t contain white space*/
  • 1myInteger /*can’t start with a number*/
  • MY+INTEGER /*plus sign is not a valid identifier*/
  • _My-Integer /*hyphen is not a valid identifier*/
  • _my’Integer /*apostrophe is not a valid identifier*/
  • $My&Integer /*ampersand is not a valid identifier*/


Add a Comment

Your email address will not be published. Required fields are marked *