Home > Interview Questions > Java Identifiers Explained

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*/


Seymour
Posted by

Seymour

I have been a software developer for twenty years now focusing on writing high quality Java applications. If you are in a programming field, you know that technology is constantly changing. You have to keep current or you will get left behind!

You may also like...

Leave a Reply

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

Copyright © 2018 JavaCodeDepot