Skip to content

Latest commit

 

History

History
116 lines (88 loc) · 4.53 KB

File metadata and controls

116 lines (88 loc) · 4.53 KB

1.01 Identifiers

Definition of Identifiers

Identifier: A name in Java program is called Identifier, which can be used for identification purpose.
It can be any of the following:

  • Variable Name
  • Class Name
  • Label Name

Q. In the following code identify number of Identifiers present and highlight them -

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

Identifiers:

  • Test - class name
  • main - method name
  • String - predefined java class name
  • args - name of array
  • x - name of variable

Total 5 identifiers in above example


Rules for defining Java identifiers

  1. Characters we can use: The only allowed characters are-
    a-z, A-Z, 0-9, '$', '_'
    Examples:

    1. total_Num - Valid
    2. totalNum# - Invalid
      If we are using any other character we'll get compile time error.
  2. Identifier should mot start with digit As the rule name suggest an Identifier can't start with digit.
    Examples:

    1. total123 - Valid
    2. 123total - Invalid
      If we are using any other character we'll get compile time error.
  3. Case-Sensitive: Not just Identifiers, whole Java language is treated as Case-Sensitive programming language. Q. Is following code valid?

    class Test {
        public static void main(String[] args) {
            int number = 10;
            int Number = 20;
            int NUMBER = 30;
        }

    Valid

  4. Length Limit: Even though there is no limit for java identifiers, it is not recommended to take too lengthy identifiers. Examples:

    1. int JavaSupportsReallyReallyLongLongIdentifier = 0; - Valid
  5. We can't use reserved words as identifiers:: In Java we can't use reserved words as identifiers. Examples:

    1. int a = 10; - Valid
    2. int if = 0; - Invalid
  6. Pre-Defined Java class names and interface as Identifiers All pre-defined Java classnames and interface can be used as Identifiers. Even though it is valid, but it is not a good programming practice, because it reduces Readability and creates confusion.

    class Test {
        public static void main(String[] args) {
            int String = 888;
            System.out.println(String);
        }

    Valid Not Recommended Output

    class Test {
        public static void main(String[] args) {
            int Runnable = 999;
            System.out.println(Runnable);
        }

    Valid Not_Recommended Output


TEST

Q. Which of the following are valid Java identifiers?

Sr. No. Identifier Answer
1. total_number Valid
2. total# Invalid Rule No.1
3. 123total Invalid Rule No.2
4. total123 Valid
5. ca$h Valid
6. _$_$_$_$_ Valid
7. all@hands Invalid Rule No.1
8. Integer Valid Not Recommended
9. Int Valid
10. int Invalid Rule No.5