Pages

Tuesday, April 9, 2019

Identifiers and Keywords in Java

Java Identifiers and Keywords:

Identifier defined as a set of Java characters and digits and first indexed should be Java letter.

Identifier: Java character or Letter.

Using following method, we can find out Java character. Pass character to below method which returns true.

Character.isJavaIdentifierPart(char c)

we can find out Java Letter using below method which returns true.

Character.isJavaIdentifierPart(int i)

Identifiers and Keywords in Java




Java Letters or Digits or Special Characters :


Upper Case : A-Z (\u0041-\u005a)
Lower Case : a-z (\u0061-\u007a)

Java Digits : 0-9 (\u0030-\u0039)

Underscore : ( _, or \u005f)
Dollar sign : ($, or \u0024).

Unicode character set supports almost all scripts today and can use identifiers in our native languages. Eg. Japanese, Chinese..

Example:



package org.java.w3schools.indentifiers;

public class IdentifierEx {
 public static void main(String[] args) {
  System.out.println(Character.isJavaIdentifierPart('s'));
  System.out.println(Character.isJavaIdentifierPart(2));
 }
} 

Rules for Identifiers:

 We can get the valid identifiers from the above code snippet. Now we will see  the thumb rules as described following.
  1.  Java identifier must start with a alphabetic (a-z or A-Z) or Dollar symbol ($) or underscore (_).
  2. Identifiers should not start with a digit (0-9). If we use the digit at starting of it then will  get compile time error.
  3. Digits can be used after the first legal character.
  4. No limit for the identifiers length in java but size should be less which is better to maintain  understandable identifiers
  5. In Java, all identifiers are case sensitive. Java treats itemsCount and ItemsCount as different  identifiers.
  6. Can not be used java inbuilt keywords as identifiers. E.g. static --> is a not valid.
 

Packages in Java

No comments:

Post a Comment

Please do not add any spam links in the comments section.