ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How to use Identifiers in Java ?.

Updated on September 17, 2014

Introduction -

In this tutorial, you will learn how to use Identifiers in Java. Identifier in Java is very important concept, which every Java programmer or developer must know. Identifiers as name suggest, it is used to identify something. Identifiers in Java are the names or id's given to class, methods, enums, variables and constants etc. Identifiers help Java programmer a way to give their own custom names to classes, methods and variables etc. Rather than giving language specific names to them and placing comments over them, it is suggested to give names with information so that comments can be replaced from Java source code. Compiler usually complains if names provided to identifiers are not valid based on few rules discussed below. Java legal identifiers must consist of following things :

  1. It may be comprised of Unicode characters.
  2. It may be comprised of Currency symbols such as, ($) etc.
  3. It may be comprised of characters which are connecting in nature such as, underscore ( _ ).

In this tutorial, below you will find a complete video to demonstrate correct usage of identifiers in Java. In Java there are few rules that programmer must follow in order to give collision free identifiers names. Some of the rules or convention are as follows :

Java video tutorial - How to use identifiers in Java.

Above video tutorial provides you a complete walk through, as how to use Identifiers in Java. This video tutorial will discuss Identifiers naming rules in detail. It also provide you how compiler complains if few rules are not followed correctly as per Java standard.

Rules for having legal Identifiers in Java.

  1. Identifiers in Java must start with a connecting character ( _ ), currency symbols ( $ ) or a letter ( a, b, A, B, R, r ) etc.
  2. Identifiers should not start with a number or digit such as, 1, 2, 3, 10, etc.
  3. Identifiers in Java can have connecting characters, currency symbols , letter and numbers after first character. It means that it can have numbers from second character on wards.
  4. Identifiers names can vary without any length in Java. It means that Java does not provide any restriction over length of identifiers name.
  5. Programmers cannot use Java reserved keywords as names of their identifiers. Java reserved keywords have special meaning when they are used in a Java program.
  6. Java identifiers are case sensitive. It means that identifiers such say, 'hubpages' , 'Hubpages' and 'HUBPAGES' are three different and valid identifiers. Java is case sensitive language and will treat them uniquely and differently.

Lets move ahead and discuss each and every Identifier naming rules in detail.

Legal rules to name Identifiers in Java.
Legal rules to name Identifiers in Java.

Rule 1 says that first character of Identifier must contain only Unicode characters, Connecting characters or Currency symbols. It cannot contain numbers or digits as first character.

For example - See below table

Invalid Identifiers
Valid Identifiers
123NewName
newName123
;hello
hello
1$dollar
$dollar
2_two
_two
Legal rules to name Identifiers in Java.
Legal rules to name Identifiers in Java.

Rule 2 says that after first character Java programmer can use digits or numbers in naming an Identifier.

For example - See table below.

Invalid Identifiers
Valid Identifiers
12One
One21
1$hello
$1hello
1_One
_one1
Legal rules to name Identifiers in Java.
Legal rules to name Identifiers in Java.

Rule 3 says that there is no limit to the number of characters a Java programmer can provide to identifiers.

For example - See table below.

Valid Identifiers
helloMyNameIsDinesh
listOfArticlesToWriteOnHubpages
collectionOfElementsToSortInArray
Legal rules to name Identifiers in Java.
Legal rules to name Identifiers in Java.

Rule 4 says that Java legal identifiers cannot be named same as Java Reserved Keywords. At the time of developing Java language certain keywords were Reserved by Java Developers to imply some different meaning when used in a Java program. Java programmer should not use these reserved keywords in program as identifiers. List of Java keywords is as follows :

Table - List of Java Reserved Keywords.

Keywords Names
Keywords Names
abstract
assert
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
enum
extends
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while
Legal rules to name Identifiers in Java.
Legal rules to name Identifiers in Java.

Rule 5 says that Java identifiers are case sensitive. As Java programming language was designed to be highly case sensitive, identifiers are case sensitive in nature. It means that identifiers with same name can be treated differently by Java based on their case sensitivity.

For example - See table below.

Legal Identifier 1
Legal Identifier 2
Legal Identifier 3
name
Name
NAME
hello
Hello
HELLO
hubpages
Hubpages
HUBPAGES
hubberspot
Hubberspot
HUBBERSPOT
Above all identifiers are valid and unique based on their case sensitivity.

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article