How to Program in Java for the Beginner: Java Reserved Words, Identifiers And Naming Conventions

Page content

Reserved words in Java

The Java language uses a number of keywords such as int, char and while. A keyword has a special meaning in the context of a Java program and can be used for that purpose only. For example, int can be used only in those places where we need to specify that the type of some item is integer.

All keywords are written in lowercase letters only. Thus int is a keyword but Int and INT are not. Keywords are reserved, that is, you cannot use them as your identifiers. As such, they are usually called reserved words. A list of Java keywords is given here.

Identifiers in Java

The Java programmer needs to make up names for things such as variables, method and function names and symbolic constants (see next section). A name that he makes up is called a user identifier. There are a few simple rules to follow in naming an identifier:

• it must start with a letter (a-z or A-Z) or underscore (_) or dollar sign ($);

• if other characters are required, they can be any combination of letters, digits (0-9), underscore or dollar sign;

• there is no limit to the length of an identifier.

Examples of valid identifiers:










Examples of invalid identifiers:

2hotToHandle // does not start with a letter or _ or $

Net Pay // contains a space

ALPHA;BETA //contains an invalid character ;

$1,000 //contains an invalid character ,

Important points to note:

Spaces are not allowed in an identifier. If you need one which consists of two or more words, use a combination of uppercase and lowercase letters (as in numThrowsThisTurn) or use the underscore to separate the words (as in num_throws_this_turn). We prefer the uppercase/lowercase combination.

• In general, Java is case-sensitive (an uppercase letter is considered different from the corresponding lowercase letter). Thus r is a different identifier from R. And sum is different from Sum is different from SUM is different from SuM.

• You cannot use a Java reserved word as one of your identifiers.

Some naming conventions

Other than the rules for creating identifiers, Java imposes no restriction on what names to use, or what format (uppercase or lowercase, for instance) to use. However, good programming practice dictates that some common-sense rules should be followed.

An identifier should be meaningful. For example, if it’s a variable, it should reflect the value being stored in the variable; netPay is a much better variable than x for storing someone’s net pay, even though both are valid. If it’s a method/function, it should give some indication of what the method is supposed to do; playGame is a better identifier than plg.

It is a good idea to use upper and lower case combinations to indicate the kind of item named by the identifier. In this series, we use the following conventions:

• A variable is normally written in lowercase, for example, sum. If we need a variable consisting of two or more words, we start the second and subsequent words with an uppercase letter, for example, voteCount or sumOfSeries.

• A symbolic (or named) constant is an identifier which can be used in place of a constant such as 100. Suppose 100 represents the maximum number of items we wish to process in some program. We would probably need to use the number 100 in various places in the program. But suppose we change our mind and want to cater for 500 items. We would have to change all occurrences of 100 to 500. However, we would have to make sure that we do not change an occurrence of 100 used for some purpose other than the maximum number of items (in a calculation like principal*rate/100, say).

To make it easy to change our mind, we can set the identifier MaxItems to 100 and use MaxItems whenever we need to refer to the maximum number of items. If we change our mind, we would just need to set MaxItems to the new value. We will begin a symbolic constant with an uppercase letter. If it consists of more than one word, we will begin each word with uppercase, as in MaxThrowsPerTurn.


Java Programming – A Beginner’s Course

Related programming references

This post is part of the series: Introduction to Java Programming

This is a series which aims to discuss and teach Java programming to the complete beginner. Absolutely no programming background is assumed.

  1. Introduction to Java Programming - An Overview
  2. Java - Data, Variable and Algorithm Explained To A Beginner
  3. Java Example: Algorithm and Program For Area of Square
  4. Java Programming For Beginners - Test, Debug, Document, Maintain
  5. JDK Java Compiler: The Java Development Kit
  6. Java Programming For Beginners - How To Compile And Run Java Programs
  7. Data Types, Constants And Variables
  8. Java Programming For Beginners - Characters and printf
  9. Java Programming For Beginners - Part 9
  10. Java Programming For Beginners - Part 10
  11. Java Programming For Beginners - Part 11
  12. Java Programming For Beginners - Part 12
  13. Java Programming For Beginners - Part 13
  14. Java Programming For Beginners - Part 14
  15. Java Programming For Beginners - Integer Data Types
  16. Java Programming for Beginners - Part 16
  17. Java Integer Arithmetic For Beginners
  18. Java Programming For Beginners - Part 18
  19. Java Programming For Beginners - Part 19
  20. Java double to int and Other Conversions