In This Article, You Will Learn about Java Identifiers.
Java Identifiers – Before moving ahead, let’s know a bit about Java Declare Multiple Variables.
Table of Contents
Java Identifiers
Not in Java but in all programming languages, variables are defined with a different name, and these distinct names are called Identifiers
It can be a short name like A, B, or C and a long name like firstName, or secondName.
Here is a list showing rules to define variable names in Java –
- Names can contain letters, digits, underscores, and dollar signs
- Names must begin with a letter
- Names should start with a lowercase letter and it cannot contain whitespace
- Names can also begin with $ and _ (underscore)
- Names are case sensitive (“firstVariable” and “firstvariable” are different variables)
- Reserved words (like Java keywords, such as int, String, integer, char, and boolean) cannot be used as names
Note: It is said that a long name should be used as a variable name for better clarification of code.
Long Name Variable.
Example: Define a long name variable.
public class Main {
public static void main(String[] args) {
// long name
int marksPerSubject = 80;
System.out.println(marksPerSubject);
}
}
Short Name Variable.
Example: Define a short name variable.
public class Main {
public static void main(String[] args) {
// short name, does not fit good always
int m = 80;
System.out.println(m);
}
}
Both Long & Short Name Variable.
Example: Define both short and long name variable.
public class Main {
public static void main(String[] args) {
// long name
int marksPerSubject = 80;
// short name, does not fit good always
int m = 80;
System.out.println(marksPerSubject);
System.out.println(m);
}
}
If you find anything incorrect in the above-discussed topic and have further questions, please comment below.
Connect on: