Thursday 23 February 2017

Java Identifiers




Java Identifiers

Identifiers are user defined names such as the name of classes, variables, constants, methods, interfaces and packages. The word "variable" means changing, because we can change the value inside the program. Each variable has a name by which it is identified in the program. It's a good idea to give your variables meaningful names that are closely related to the values they hold. Variable names can include any alphabetic character or digit and the underscore _. All variable names are case-sensitive. MyVariable is not the same as myVariable. There is no limit to the length of a Java variable name.
Following are the rules we must be aware of regarding the correct formulation of identifier names.

Rules of Identifiers Naming Convention

Syntax is a grammatical rule. Here is the syntax for valid Java identifiers:
• Each identifier must have at least one character.
• The first character must be picked from: alpha, underscore, or dollar sign. The first character cannot be a digit.
• The rest of the characters (besides the first) can be from: alpha, digit, underscore, or dollar sign. In other words, it can be any valid identifier character.
• Dollar Sign and Underscore as First Character in Variable name is allowed but still it’s not good programming Style to use it as First Character.
White space is not permitted.
Special Characters are not allowed.
Digit at start is not allowed.
• Subsequent characters may be letters, digits, dollar signs, or underscore characters.
• Variable name must not be a keyword or reserved word.


Examples of Valid Identifiers

Here are some valid identifiers:
• aaa
• sales_tax
• _circleArea
• box100width
• $directory
• ab1234$$

Although you have great flexibility for identifiers, we're going to make more restrictions for which identifiers we should use.
But first, to look at some invalid identifiers

Examples of Invalid Identifiers

It's easy to make a mistake and use a bad identifier.
• 1ab (ERROR: first character starts with a digit)
• num-oranges (ERROR: dash is not permitted in identifiers)
• num oranges (ERROR: space is not permitted in identifiers)




 Next Topic Shahbaz

0 comments:

Post a Comment

Powered by Blogger.

Stats