Valid java identifier names

Valid java identifier names

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Identifiers in Java with Examples

Java Course - Mastering the Fundamentals

Identifiers in Java are names that are helpful in uniquely recognizing a class, a method, a package name, a constant, or a variable. Some words in Java are reserved and cannot be used as identifiers. Certain rules must be followed in Java while we are defining an identifier, or the compiler will throw an error.

Introduction to Identifiers in Java

In general terms, an identifier is a name given to an entity for identification. Java identifiers are any attribute/property of an entity that is utilized for identification. For example, a person’s name, an employee’s employee number, or an individual’s social security number.

Identifiers in Java are names that distinguish between different Java entities, such as classes, methods, variables, and packages. Identifiers include the names of classes, methods, variables, packages, constants, etc. These identifiers are each specified using a specific syntax and naming scheme.

The syntax would depend on the type of identifier. For example, an integer variable in Java can be declared as below,

The variable name is the name given by the programmer to uniquely identify the int variable. Later the programmer can use that variable in his program. Hence, here the variable name is the identifier. These Identifiers follow a certain naming convention/rules when they are named, which we shall look at in the next section of this article.

Example Java Code Snippet

Identifiers: Below is the list of identifiers that are present in the above sample code.

  • MainClass (Class name)
  • main (Method name)
  • String (Predefined Class name)
  • args (String variable name)
  • var1 (integer variable name)
  • var2 (double variable name)
  • System (Predefined Class name)
  • out (Variable name)
  • println (Method name)

Rules for Identifiers in Java

Below are the rules that need to be followed when defining an identifier in Java. A compile-time error will be produced if any of the following rules are broken.

Rule 1:

Identifiers can only contain alphanumeric characters [a-z] [A-Z] 6 , dollar sign ($) and underscore ( _ ). No other character is allowed. Valid Examples:

Identifier Explanation
NomansLand90 This is valid as it contains only alphanumerics
$Wink1NTomBoy This is valid as characters are alphanumerics, and $

Invalid Examples:

Rule 2:

Identifiers cannot start with a numeric value 4. The starting character should be alphabet [A-Z] [a-z], dollar ($) or underscore ( _ ).

Valid Example:

Invalid Example:

Rule 3:

Identifiers should not contain spaces in their name.

Valid Example:

Invalid Example:

Rule 4:

Java identifiers are case-sensitive.

Eg: ‘Vendetta’ and ‘vendetta’ are considered as two different identifiers.

Rule 5:

The standard convention sets the size of the Java identifiers between 4 – 15, and it is advised to follow that length when defining an identifier. However, there is no upper limit on the length of an identifier.

Rule 6:

Reserve Keywords cannot be used as Identifiers. Java defines a total of 53 reserved keywords that are predefined. In the following part, we’ll examine what a reserved keyword is and a list of predefined terms.

Example: int, float, public, static, etc.

Java Reserved Keywords

Reserved keywords are keywords that are used by java syntax for a particular functionality. Since each of these reserved keywords’ functionality is predefined, these keywords cannot be used for any other purpose.

Java predefines a set of 53 reserved keywords that cannot to used as Identifiers. So these keywords cannot be used as Class names, Method names, Package names, or Variable names. We have some of these keywords in our sample program in the introduction. int, double, public, static, etc., are all examples of reserved keywords.

Example 1: int Var1; → Here, int is a reserved keyword that is used to define an integer variable. The variable name is Var1 , which is an identifier for an int variable.

Example 2: int break; → Here, int is a reserved keyword, and the variable name is break , which is invalid as break itself is a reserved keyword with its predefined functionality in java. Hence this statement will throw an error.

Below is the table of references for all the 53 keywords in Java:

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

Valid Identifiers in Java

Below is an example list of valid Identifiers that can be used in java

Identifier Explanation
Employee alphabets
EMP12 alphanumerics
$Manager1 alphanumerics and $
_AngryMonk404 alphanumerics and _
Student36Pro9 alphanumerics
A alphabet uppercase A
i alphabet lowercase i
$ Symbol $
final_result_value alphabets and _
SevenUp___7 alphanumerics and _

Invalid Identifiers in Java

Below is an example list of Identifiers that are invalid in java.

Identifier Explanation
@Employee contains invalid character @
12EMP Starts with numerics
&Manager1 contains invalid character &
^AngryMonk404 contains invalid character ^
36-StudentPro9 Starts with numerics and contains invalid character –
5 Is a numeric
final result value Contains spaces

Examples

Let’s discuss few examples in detail related to Java identifiers to get a better understandin:

Valid identifier example:

Explanation: In this example, myVariable is a valid identifier for an integer variable. It is assigned the value 1 0 10 1 0 , and then its value is printed, resulting in the output of 1 0 10 1 0 .

Invalid identifier example

Output/Error

Explanation: In this example, the identifier 1 2 3 a b c 123abc 1 2 3 a b c violates the naming rule as it starts with a digit. Identifiers must start with a letter, underscore, or dollar sign, not a digit.

Using the reserved word as an identifier

Output/Error

Explanation: In this example, the identifier class is a reserved word in Java and cannot be used as an identifier. Attempting to use a reserved word as an identifier will cause a compilation error.

Valid constant identifier example

Explanation: In this example, P I PI P I is a valid constant identifier representing the mathematical constant pi. It is declared as final to indicate that its value cannot be changed. The value of P I PI P I is then printed, resulting in the output of 3.14159.

Valid method identifier example

Explanation: In this example, «printMessage» is a valid identifier for a method. It takes a String parameter «message» and prints it. The method is called with the argument «Hello, world!», resulting in the output of Hello, world!.

FAQs

Q1. What are identifiers in Java?

Ans Identifiers in Java are names used to uniquely identify classes, methods, package names, constants, and variables.

Q2. Can reserved words be used as identifiers in Java?

Ans No, reserved words in Java cannot be used as identifiers. Attempting to do so will result in a compilation error.

Q3. What happens if an identifier violates the naming rules in Java?

Ans If an identifier violates the naming rules in Java, the compiler will throw an error, and the code will not compile. It is important to follow the naming rules to avoid compilation issues.

Q4. Why is it important to choose meaningful identifiers in Java?

Ans Choosing meaningful identifiers improves code readability and maintainability. It makes the code easier to understand for developers and helps in avoiding naming conflicts.

Conclusion

Оцените статью