Keywords & Identifiers In Java

In this tutorial, you will learn a basic unerstanding of Keywords and their list, Identifiers and rules for naming Identifiers and valid and invalid variables.


Keywords in Java:

Java has a set of reserved words that are known as keywords. These keywords have a specific meaning in the language and cannot be used as identifiers. If we try to use a keyword as an identifier, we will get a compile-time error. Here is a list of some of the keywords in Java:

C Language Keywords List

Java keywords list :

  1. abstract: Java abstract keyword is used to declare abstract class, abstract methods. It is basically used to hide implementation details from outsiders and only functionality is provided.
  2. assert: assert keyword is used for debugging.
  3. boolean: boolean Keyword is a data type that can only store true and false values(1 or 0).
  4. break: break Keyword is used to break a loop or a switch block. It breaks the current execution flow of the program at specified conditions.
  5. byte: byte Keyword is to declare a variable that can hold 8-bit data values. It can store whole numbers from -128 and 127.
  6. case: case keyword is used with the switch statements to mark blocks of text.
  7. catch: catch keyword is used to catch the exceptions generated by try statements. This block must be followed by the try block.
  8. char: char keyword is used to declare a variable that can hold unsigned 16-bit Unicode characters. It is used to store a single character.
  9. class: class keyword is used to declare a new class.
  10. continue: continue keyword is used to continue the next iteration of a loop. It is used to skip the current iteration of the loop and resume to the next iteration.
  11. default: default keyword is used to specify the default block of code in a switch statement.
  12. do: do keyword is used in the control statement to declare a loop. In do loop, the loop is executed atleast once and then check for the specified condition.
  13. double: double keyword is used to declare a variable that can hold 64-bit floating-point numbers. It is used to store whole numbers from 1.7e−308 to 1.7e+308
  14. else: else keyword is used to indicate the alternative branches in an if statement. Used in conditional statements.
  15. enum: enum keyword is used to define a fixed set of constants.
  16. extends: extends keyword is used to indicate that a class is derived from another class or interface.
  17. final: final keyword is used to declare constant variable values.
  18. finally: finally keyword indicates a block of code in a try-catch structure. Finally block will be executed by default.
  19. float: float keyword is used to hold a 32-bit floating-point number. Store whole numbers from 3.4e−038 to 3.4e+038
  20. for: for keyword is used to start a for loop. It is used to execute a set of instructions repeatedly when some conditions become true.
  21. if: if keyword is used to make a conditional statement. It executes the if block if the condition is true.
  22. implements: implements keyword is used to implement an interface.
  23. import: import keyword import packages, classes, and interfaces.
  24. instanceof: instanceof is used to checks whether an object is an instance of a specific class or an interface
  25. int: int is a data type that can hold a 32-bit signed integer. Store whole numbers from -2147483648 to 2147483647
  26. interface: interface keyword is used to declare a special type of class that only contains abstract methods
  27. long: long keyword is used to declare a variable that can hold a 64-bit integer. Store whole numbers from -9223372036854775808 to 9223372036854775808
  28. new: new keyword is used to create new objects.
  29. package: package keyword is used to declare a Java package that includes the classes.
  30. private: private keyword is an access modifier. It is used to indicate that attributes, methods, and constructors, making them only accessible within the declared class
  31. protected: protected keyword is an access modifier. It is used to indicate attributes, methods, and constructors, making them accessible in the same package and subclasses
  32. public: public keyword is an access modifier. It is used to indicate that classes, attributes, methods and constructors, making them accessible by any other class
  33. return: return keyword is used to return from a method when its execution is complete.
  34. short: short is a data type that can hold a 16-bit integer. Store whole numbers from -32768 to 32767.
  35. static: static keyword is used for memory management mainly. single copy to be shared by all instances of the class.
  36. super: super keyword is a refer immediate parent class variable. this keyword is used to access data members or fields of the parent class. By using this keyword we can call the variable, method, and constructor of the superclass.
  37. switch: Java switch keyword contains a switch statement that executes code based on test value.
  38. this: this keyword refers to the current object in a method or constructor.
  39. throw: throw keyword is mainly used to throw a custom exception or user-defined exceptions.
  40. try: try keyword is used to start a block of code that will be tested for exceptions. try block contain multiple catch blocks and single finally block.
  41. void: void keyword is used to declare that a method does not have a return value.
  42. while: while keyword is used to start a while loop. while loop comes under repetition statements.

Identifiers in Java:

Identifiers in Java are symbolic names used for identification, such as class names, method names, variable names, or labels. It's important to follow some rules when defining identifiers to ensure that your code is valid and easy to read.


There are some rules, which we need to follow While defining an identifier.

  1. An identifier can only have alphabates (a-z, A-Z), numeric characters (0-9) and underscore (_).
  2. The first letter of an identifier should be either a letter (a-z, A-Z) or underscore (_). So a valid identifier can't start with a numeric character.
  3. We can't use the Java reserved keywords as an identifier such as int, float, double, char, etc.
  4. No special characters like semicolon, period, whitespaces, slash, or comma are permitted to be used as a identifier character.
  5. Java identifiers are case-sensitive.
  6. As a good practice, we should always provide meaningful and short names to identifiers.
  7. As per the standard conventions, identifier length should be a minimum of 4 to a maximum of 15 characters.
  8. Some examples of valid Identifiers in java: HashVariable, Hash12 ,hashvariable, a, i, _hashvariable , Hash_Variable , hash_code_variable .
  9. Some examples of Invalid Identifiers in java: Hash Variable, 12Hash, hash+variable, hash-variable, Hash_&_Variable , hash'variable .
Examples of Valid and Invalid Identifiers:

Here are some examples of valid identifiers in Java:

  • HashVariable
  • Hash12
  • hashvariable
  • a
  • i
  • _hashvariable
  • Hash_Variable
  • hash_code_variable

And here are some examples of invalid identifiers in Java:

  • Hash Variable (contains a whitespace)
  • 12Hash (starts with a numeric character)
  • hash+variable (contains a special character)
  • hash-variable (contains a special character)
  • Hash_&_Variable (contains a special character)
  • hash'variable (contains a special character)