Identifier Choices – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #JAVA#3798    siteicon   siteicon  

Identifier Choices

Making Identifier name choices.

Learning Objectives

  • Using different types of valid identifier names

Source Code

TC++ #3798

Source Code

Run Output

Average age of animals I chose = 78
This is about 30 % of max known 255

Code Understanding

class IdentifierExamples
Class names usually follow title case where each word in name has first letter in capital
{

public static void main(String[] args)
Here public, static and void are keywords but main,String and args are identifiers. Since String is a class name so it begins with a capital letter.
{
final int MAX=255;
Here MAX is an identifier. All capital identifier names are often given to final values which can not be changed while execution.

int ageSum=0;
The word ageSum is a two word identifier which follows the camelCase rule, which means first word in all lowercase and second and more words with first alphabet in Upper case.

int lion=12;
The identifier lion  is most common type identifier with all alphabets in lower case.

int rehsus_monkey=25;
Here the identifier rehsus_monkey follows the traditional c style of naming where multiple words are separated by an underscore character.

int bigSnake$=25;
The identifier bigSnake$ has a special character $ in the name which is allowed in java other than the underscore character. This make java naming a little different from many other languages.

int tortoise250=250;
This identifier tortoise250 has numerals also a part of naming, which is perfectly valid. The only thing is that a name can not begin with numerals.

ageSum=lion+rehsus_monkey+bigSnake$+tortoise250;
System.out.println(“Average age of animals I chose = “+ageSum/4);
System.out.println(“This is about “+((ageSum/4*100)/MAX)+” % of max known 255″);
All above lines of codes are simply implementation of previous declarations to make this program do some useful function. The approach of its implementation is not a part of this explanation.

}
}

Notes

  • Identifiers can not begin with any numerals from 0 to 9 but they can definitely begin with _ or $.
  • Some compiler reserve the use of _ and __ beginning for special usage, so programmers are recommended to avoid such use in their code.
  • Java does not put any restriction on identifier length, but programmers are definitely advised to use meaningful names with limited length so that typo errors can be avoided.

Common Errors

  • Some commonly mis-spelled identifiers could of the form –
    Snake 10  //Identifier can not contain spaces
    12Lion     //Identifier can not begin with numerals
    Tortoise(200) //Identifier can not have any other special character other than $ and _

 


Suggested Filename(s): IdentifierExamples.java



Share

sunmitra| Created: 10-Mar-2018 | Updated: 11-Mar-2018|






Back