The Wayback Machine - https://web.archive.org/web/20250306132405/https://www.geeksforgeeks.org/java-identifiers/
Open In App

Java Identifiers

Last Updated : 20 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

An identifier in Java is the name given to Variables, Classes, Methods, Packages, Interfaces, etc. These are the unique names and every Java Variables must be identified with unique names.

Example:

public class Test
{
public static void main(String[] args)
{
int a = 20;
}
}

In the above Java code, we have 5 identifiers as follows: 

  • Test: Class Name
  • main: Method Name
  • String: Predefined Class Name
  • args: Variable Name
  • a: Variable Name

Rules For Naming Java Identifiers

There are certain rules for defining a valid Java identifier. These rules must be followed, otherwise, we get a compile-time error. These rules are also valid for other languages like C, and C++. 

  • The only allowed characters for identifiers are all alphanumeric characters([A-Z],[a-z],[0-9]), ‘$‘(dollar sign) and ‘_‘ (underscore). For example “geek@” is not a valid Java identifier as it contains a ‘@’ a special character.
  • Identifiers should not start with digits([0-9]). For example “123geeks” is not a valid Java identifier.
  • Java identifiers are case-sensitive.
  • There is no limit on the length of the identifier but it is advisable to use an optimum length of 4 – 15 letters only.
  • Reserved Words can’t be used as an identifier. For example “int while = 20;” is an invalid statement as a while is a reserved word. There are 53 reserved words in Java.

Examples of Valid Identifiers:

MyVariable
MYVARIABLE
myvariable
x
i
x1
i1
_myvariable
$myvariable
sum_of_array
geeks123

Examples of Invalid Identifiers:

My Variable                     // contains a space
123geeks // Begins with a digit
a+c // plus sign is not an alphanumeric character
variable-2 // hyphen is not an alphanumeric character
sum_&_difference // ampersand is not an alphanumeric character

Reserved Words in Java

Any programming language reserves some words to represent functionalities defined by that language. These words are called reserved words. They can be briefly categorized into two parts: keywords(50) and literals(3). Keywords define functionalities and literals define value. Identifiers are used by symbol tables in various analyzing phases(like lexical, syntax, and semantic) of a compiler architecture. 

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

Note: The keywords const and goto are reserved, even though they are not currently used. In place of const, the final keyword is used. Some keywords like strictfp are included in later versions of Java.


Kickstart your Java journey with our online course on Java Programming, covering everything from basics to advanced concepts. Complete real-world coding challenges and gain hands-on experience. Join the Three 90 Challenge—finish 90% in 90 days for a 90% refund. Start mastering Java today!


Next Article
Article Tags :
Practice Tags :

Similar Reads

Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
Different Ways to Convert java.util.Date to java.time.LocalDate in Java
Prior to Java 8 for handling time and dates we had Date, Calendar, TimeStamp (java.util) but there were several performance issues as well as some methods and classes were deprecated, so Java 8 introduced some new concepts Date and Time APIs' (also known as Joda time APIs') present in java.time package. Java 7: Date, Calendar, TimeStamp present ins
3 min read
Java AWT vs Java Swing vs Java FX
Java's UI frameworks include Java AWT, Java Swing, and JavaFX. This plays a very important role in creating the user experience of Java applications. These frameworks provide a range of tools and components for creating graphical user interfaces (GUIs) that are not only functional but also visually appealing. As a Java developer, selecting the righ
11 min read
How to Convert java.sql.Date to java.util.Date in Java?
If we have the Date object of the SQL package, then we can easily convert it into an util Date object. We need to pass the getTime() method while creating the util Date object. java.util.Date utilDate = new java.util.Date(sqlDate.getTime()); It will give us util Date object. getTime() method Syntax: public long getTime() Parameters: The function do
1 min read
How to Convert java.util.Date to java.sql.Date in Java?
Date class is present in both java.util package and java.sql package. Though the name of the class is the same for both packages, their utilities are different. Date class of java.util package is required when data is required in a java application to do any computation or for other various things, while Date class of java.sql package is used whene
3 min read
Java.io.StreamTokenizer Class in Java | Set 1
Java.io.StreamTokenizer class parses input stream into "tokens".It allows to read one token at a time. Stream Tokenizer can recognize numbers, quoted strings, and various comment styles. Declaration : public class StreamTokenizer extends Object Constructor : StreamTokenizer(Reader arg) : Creates a tokenizer that parses the given character stream. M
9 min read
Java.net.HttpURLConnection Class in Java
HttpURLConnection class is an abstract class directly extending from URLConnection class. It includes all the functionality of its parent class with additional HTTP-specific features. HttpsURLConnection is another class that is used for the more secured HTTPS protocol. It is one of the popular choices among Java developers for interacting with web
5 min read
Java.io.InputStream Class in Java
Java InputStream class is the superclass of all the io classes i.e. representing an input stream of bytes. It represents an input stream of bytes. Applications that are defining a subclass of the Java InputStream class must provide a method, that returns the next byte of input. A reset() method is invoked which re-positions the stream to the recent
3 min read
Java.io.LineNumberInputStream Class in Java
java.io.LineNumberInputStream class is simply an extension of input stream providing a extra facility to keep the record of current line number. Line is a sequence of bytes ending with : '\r' i.e. a carriage return character or a newline character : '\n', or a linefeed character following the carriage return character. Declaration : public class
11 min read
Java.io.StringWriter class in Java
Java StringWriter class creates a string from the characters of the String Buffer stream. Methods of the StringWriter class in Java can also be called after closing the Stream as this will raise no IO Exception. Declaration in Java StringWriter Classpublic class StringWriter extends WriterConstructors in StringWriter Class in Java Constructors use
6 min read