0% found this document useful (0 votes)
12 views

2.4 Identifiers and Keyword in Java

Uploaded by

mrnirajbro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

2.4 Identifiers and Keyword in Java

Uploaded by

mrnirajbro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Identifiers in Java

Identifiers in Java are symbolic names used for identification. They can be a class name,
variable name, method name, package name, constant name, and more. However, In Java,
There are some reserved words that can not be used as an identifier.

For every identifier there are some conventions that should be used before declaring them.
Let's understand it with a simple Java program:

1. public class HelloJava {


2. public static void main(String[] args) {
3. System.out.println("Hello JavaTpoint");
4. }
5. }

From the above example, we have the following Java identifiers:

1. HelloJava (Class name)


2. main (main method)
3. String (Predefined Class name)
4. args (String variables)
5. System (Predefined class)
6. out (Variable name)
7. println (method)
Rules for Identifiers in Java
There are some rules and conventions for declaring the identifiers in Java. If the identifiers
are not properly declared, we may get a compile-time error. Following are some rules and
conventions for declaring identifiers:

o A valid identifier must have characters [A-Z] or [a-z] or numbers [0-9], and
underscore(_) or a dollar sign ($). for example, @javatpoint is not a valid identifier
because it contains a special character which is @.
o There should not be any space in an identifier. For example, java tpoint is an invalid
identifier.
o An identifier should not contain a number at the starting. For example, 123javatpoint
is an invalid identifier.
o An identifier should be of length 4-15 letters only. However, there is no limit on its
length. But, it is good to follow the standard conventions.
o We can't use the Java reserved keywords as an identifier such as int, float, double,
char, etc. For example, int double is an invalid identifier in Java.
o An identifier should not be any query language keywords such as SELECT, FROM,
COUNT, DELETE, etc.

Keywords reserved word:-


Java reserved keywords are predefined words, which are reserved for any functionality or
meaning. We can not use these keywords as our identifier names, such as class name or
method name. These keywords are used by the syntax of Java for some functionality. If we use
a reserved word as our variable name, it will throw an error.

In Java, every reserved word has a unique meaning and functionality.

Consider the below syntax:

1. double marks;

in the above statement, double is a reserved word while marks is a valid identifier.

Below is the list of reserved keywords in Java:

abstract continue for protected transient


Assert Default Goto public Try
Boolean Do If Static throws
break double implements strictfp Package
byte else import super Private
case enum Interface Short switch
Catch Extends instanceof return void
Char Final Int synchronized volatile
class finally long throw Date
const float Native This while

Although the const and goto are not part of the Java language; But, they are also considered
keywords.

Example of Valid and Invalid Identifiers


Valid identifiers:

Following are some examples of valid identifiers in Java:

o TestVariable
o testvariable
o a
o i
o Test_Variable
o _testvariable
o $testvariable
o sum_of_array
o TESTVARIABLE
o jtp123
o JavaTpoint
o Javatpoint123

Invalid identifiers:

Below are some examples of invalid identifiers:

o Test Variable ( We can not include a space in an identifier)


o 123javatpoint ( The identifier should not begin with numbers)
o java+tpoint ( The plus (+) symbol cannot be used)
o a-javatpoint ( Hyphen symbol is not allowed)
o java_&_Tpoint ( ampersand symbol is not allowed)

You might also like