Loading

Keywords

  • Keywords are the reserved words used by the Java development team. It is used to define some functionality. Each keyword has different rules, functions and uses.

 Key Points:-

  • Keywords cannot be used as identifiers.
  • There are 67  keywords in Java of which 16 are contextually reserved and can sometimes be used as identifiers, unlike some reserved identifiers.
abstractassertbooleanbreakbyte
casecatchcharclasscontinue
defaultdodoubleelseenum
extendsfinalfinallyfloatfor
ifimplementsimportinstanceofint
interfacelongnativenewpackage
privateprotectedpublicreturnshort
staticsuperswitchsynchronizedthis
throwthrowstransienttryvoid
volatilewhile   

Also, there are some contextual keywords used as identifiers and are restricted in some cases:

exportsexportsnon sealedopen
openspermitsprovidesrecord
requiressealedtotransitive
usesvarwithyield

Also, there are some words reserved for literal values:

truefalsenull

Also, there are some unused keywords:

constgotostrictfp

 


Identifiers

  • Identifiers mean to identify.
  • It is used for declaring the name of a class, method, variable, etc. 
  • e.g. class Employee{ } (Here Employee is the class name . So Employee is an identifier.)
  • Legal identifiers must be created by only Unicode characters, numbers, currency symbols, and connecting characters (like underscores).

Example:

public class Addition   //Addition-Class Name
{
     public static void main(String[ ] args)     //main-Method Name
    {
           int  a = 10;   //a-Variable Name
           int  b = 20;   //b-Variable Name
    }
} 

There are some limitations and certain rules for writing valid identifiers:

1. Only alphanumeric characters ( [A-Z], [a-z], [0-9] ) are allowed, no special characters other than  ' $ '(dollar sign) and '_'(underscore)  are allowed.

Example-  quipo@ is not a valid java identifier.

2. Identifiers should not start with numbers like ([0-9]).

Example-  123quipo is not a valid java identifier

3. Java identifiers are case-sensitive which means identifiers written in lower case order and the same written in upper case order are treated differently.

Example-  quipoHouse and QuipoHouse are treated as two different identifiers.

4. Keywords can’t be used as an identifier.

Example-  while, for, main etc. can't be used as identifiers.

5. There is no limit for the length of the identifier but one can use an optimum length of 4 – 15 letters only.

6. One should use camel-casing while declaring some variables as it is industry-suggested.

Example-  newWord,   addFile

7. Space is not allowed between two-word identifiers.

Example-  My Variable // is not a valid java identifier.

Goto and Const

  • The const keyword is a reserved keyword in Java, but it is currently not in use. In other programming languages, such as C, const is used to declare a constant. However, in Java, the final keyword is used instead.
  • The goto keyword is also a reserved keyword in Java, but it is currently not in use.
  •  The goto keyword is used to transfer control from one part of a program to another. However, the goto keyword is considered to be a bad programming practice, as it can make code difficult to read and understand.

Here are some examples of how the const and goto keywords are used in other programming languages:

// C code
const int MAX_VALUE = 100;
goto label;
MAX_VALUE = 100
goto label

In Java, the const and goto keywords cannot be used. Instead, the final keyword and other control flow statements, such as if, else, and for, should be used.

Here are some examples of how the final keyword and other control flow statements are used in Java:

final int MAX_VALUE = 100;
if (condition) {
  // code block
} else {
  // code block
}
for (int i = 0; i < MAX_VALUE; i++) {
  // code block
}

We will learn more about the Final keyword in the next tutorial.