Keywords and Identifiers || Java Programming Basics || Bcis Notes

Keywords and Identifiers || Java Programming Basics || Bcis Notes

Keywords and Identifiers are described below:

Keywords:

          Java keywords are also known as reserved words. Keywords are particular words that act as a key to a code. These are predefined words by Java so it cannot be used as a variable or object name.

In Other Words, Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to use as variable names or objects. Doing this will result in a compile-time error.

 

Keywords and Identifiers || Java Programming Basics || Bcis Notes

Fig: Table of keywords

 

List of java keywords:

  1. abstract: Java abstract keyword is used to declare an abstract class. An Abstract class can provide the implementation of the interface. It can have abstract and non-abstract methods.
  2. boolean: Java boolean keyword is used to declare a variable as a boolean type. It can hold True and False values only.
  3. break: Java break keyword is used to break the loop or switch statement. It breaks the current flow of the program at a specified condition.
  4. byte: Java byte keyword is used to declare a variable that can hold 8-bit data values.
  5. case: Java case keyword is used with the switch statements to mark blocks of text.
  6. catch: Java catch keyword is used to catch the exceptions generated by try statements. It must be used after the try block only.
  7. char: Java char keyword is used to declare a variable that can hold unsigned 16-bit Unicode characters
  8. class: Java class keyword is used to declare a class.
  9. continue: Java continue keyword is used to continue the loop. It continues the current flow of the program and skips the remaining code at the specified condition.
  10. default: Java default keyword is used to specify the default block of code in a switch statement.
  11. do: Java do keyword is used in the control statement to declare a loop. It can iterate a part of the program several times.
  12. double: Java double keyword is used to declare a variable that can hold a 64-bit floating-point number.
  13. else: Java else keyword is used to indicate the alternative branches in an if statement.
  14. enum: Java enum keyword is used to define a fixed set of constants. Enum constructors are always private or default.
  15. extends: Java extends keyword is used to indicate that a class is derived from another class or interface.
  16. final: Java final keyword is used to indicate that a variable holds a constant value. It is applied with a variable. It is used to restrict the user.
  17. finally: Java finally keyword indicates a block of code in a try-catch structure. This block is always executed whether the exception is handled or not.
  18. float: Java float keyword is used to declare a variable that can hold a 32-bit floating-point number.
  19. for: Java for a keyword is used to start a for a loop. It is used to execute a set of instructions/functions repeatedly when some conditions become true. If the number of iteration is fixed, it is recommended to use for loop.
  20. if: Java if keyword tests the condition. It executes the if block if the condition is true.
  21. implements: Java implements keyword is used to implement an interface.
  22. import: Java import keyword makes classes and interfaces available and accessible to the current source code.

Identifiers:

 

Identifiers are the names of variables, methods, classes, packages, and interfaces. Unlike literals, they are not the things themselves, just ways of referring to them. In the HelloWorld program, HelloWorld, String, args, main, and println are identifiers.

Identifiers must be composed of letters, numbers, the underscore _, and the dollar signs $. Identifiers may only begin with a letter, the underscore, or a dollar sign.

Rules when defining Java Identifiers

The rules for naming identifiers are the same as the rules for naming variables & class.

  • An identifier is a sequence of characters that may consist of letters (A-Z, a-z) digits (0-9), an underscore character ( _ ), or the dollar sign ($).
  • An identifier can be one to several characters long.
  • An identifier must start with a letter, an underscore, or a dollar sign.
  • An identifier cannot start with a digit and must not contain any spaces or tabs.
  • An identifier cannot be any of the Java keywords.
  • Identifiers are case sensitive, so count, and Count is distinct identifiers.
  • An identifier cannot be a true, false, or null.
  • It is good programming practice to use the identifier names that reflect the meaning of the programming element being used.
  • Java is case sensitive. Not using the proper uppercase and lowercase letters for an identifier causes a compilation error typically.

 

Examples of valid identifiers :

variable

MY VARIABLE

my variable

x

i

x1

i1

_myvariable

$myvariable

sum_of_array

geeks123

Examples of invalid identifiers:

My Variable  // contains a space

123geeks   // Begins with a digit

a+c // plus sign is not an alphanumeric character

variable-2 // hyphen is not an alphanumeric character

sum_&_difference // ampersand is not an alphanumeric character

 

You may like the history of java.

Be the first to comment

Leave a Reply

Your email address will not be published.


*