Home » Java Keyword and Identifier

Java Keyword and Identifier

Java Keyword and Identifier

Java Keywords

In a programmer’s life, knowing all the keywords are very essential to build a program properly. In this article, we will discuss all the keywords that JDK has in reserve.In the Java programming language,   there are several reserved words that have a predefined meaning in the language. Because of this, programmers cannot use keywords in some contexts, such as names for variables, methods, classes, or as any other java identifiers.

Keywords in Java are reserved words that have predefined meanings and cannot be used for any other purpose than their intended use in the language. Some examples of Java keywords include class, public, static, void, if, else, for, while, return, try, catch, finally, extends, implements, etc.

java-identifiesr-aand-keywords.png

List of Java Reserved Keywords

KeywordDescription
abstractIndicates the class or method that follows this keyword is abstract and that will have to be implemented by a subclass.
assertAssert keyword helps the programmer to declare assertions or assumptions in a program. If an assertion is true, program progresses normally otherwise the AssertionError is thrown at runtime and program aborts.
booleanDefines two Boolean values, true or false, 0 and 1.
breakUsed to break out of loops or iterative constructs.
byteData type capable of holding 8-bit data.
caseMarks blocks of text (cases) in a Switch statement.
catchUsed to catch exceptions generated in the try block.
charData type able to hold unsigned 16-bit Unicode characters.
classUsed to declare a new class.
continueIt helps to take control outside the loop and continue to the next iteration.
defaultDefines the “block of code” that will execute by default in a Switch statement.
doStarting keyword for “do-while” loop.
doubleData type holding 64-bit numbers (floating-point).
elseDefines else part in the ‘if’ statements.
enumUsed to declare enumerations in Java.
extendsIndicates inheritance. A class is derived or inherited from another class.
finalDefines a variable which will hold constant values or a method that cannot be overridden.
finallyDefines the finally block that executes after the try-catch block irrespective of whether the exception was caught or not.
floatData type able to hold 32-bit floating-point values.
forIndicates the start of a ‘for’ loop.
ifStart of ‘if’ statement.
implementsIndicates that a class implements an interface.
importUsed to include or reference other packages/classes into the program.
instanceOfUsed to check if the given object is an instance of another class.
intData type to hold a 32-bit integer value.
interfaceUsed for declaring an interface.
longData type holding 64-bit integer values.
nativeUsed to indicate native code (platform-specific).
newOperator to create a new object.
nullIndicates null reference.
packageKeyword to declare Java package.
privateIndicates private access specified which means a variable or method can be accessed only by the class in which it is declared.
protectedThis keyword indicates a protected access specifier. When a variable or method is protected then that variable or method can be accessed only by the class they are declared in, its subclass, and other classes in the same package.
publicThe public keyword is used to indicate public access specifier. A variable, method, classes, interfaces declared as public can be accessed throughput the application.
returnReturn is used to send back the value of a method to the calling method. It also is used to return the control to the calling method.
shortData type holding 16-bit integer number values.
staticThe static keyword indicates the method or a variable is static and cannot be instantiated.
strictfpThe keyword strictfp restricts the rounding and precision of floating point values calculation. It ensures portability.
superIndicates base or superclass of the class.
switchIndicates a Switch statement that tests a condition and executes multiple cases depending on the test value.
synchronizedIndicates synchronized sections for multithreaded code like critical section.
thisThe keyword ‘this’ indicates the current object.
throwThrows an exception.
throwsThis indicates the exception that can be thrown by a method.
transientSpecifies transient variable that is not part of the persistent state of an object.
tryTry keywords start a block that contains code that might raise exceptions.
voidIndicates no return value.
volatileUsed to define variables that are not stored in Main Memory. They can be changed asynchronously.
whileKeyword while starts a while loop.
constThe ‘const’ keyword is no more supported in Java
gotoThe ‘goto’ keyword is no more supported in Java
true, false and nullThe words “true, false, null” are literals. Still, we cannot use them as identifiers in the program.

Java Identifiers

Java identifiers are names given to various elements in a Java program, such as variables, methods, classes, packages, and interfaces.

All Java variables must be identified with unique names.

These unique names are called identifiers.

Rules for Java Identifiers:

  1. Starting Characters: Identifiers must begin with a letter (a-z or A-Z), a dollar sign ($), or an underscore (_). They cannot start with a digit or any other special character.
  2. Subsequent Characters: After the initial character, identifiers may contain letters, digits, underscores, or dollar signs. They cannot contain spaces or other special characters.
  3. Case Sensitivity: Java identifiers are case-sensitive, meaning uppercase and lowercase letters are distinct. For example, “myVariable” and “MyVariable” are treated as different identifiers.
  4. Reserved Keywords: Identifiers cannot be Java keywords or reserved words, as these have predefined meanings in the language. Attempting to use a reserved word as an identifier will result in a compilation error.
  5. Length: Identifiers can be of any length, but it’s advisable to keep them concise and meaningful for improved code readability. Excessively long identifiers may hinder readability and comprehension.

Best Practoice for Java Naming Identifiers

Examples of Java Identifiers

Example 1Variables

int itemCount;
double totalPrice;
String customerName;
Java

Example 2 – Class

public class ShoppingCart {
// Class members and methods
}
Java
class OrderProcessing {
// Class members and methods
}
Java

Example 3 – Method

public void calculateTotalPrice() {
// Method implementation
}

private String formatCustomerName(String firstName, String lastName) {
// Method implementation
}
Java

Example 4 – Interface –

public interface PaymentGateway {
// Interface methods
}
Java

Java Keywords vs Identifiers

Here are some key differences between keywords and identifiers:

S.No.KeywordIdentifier
1Keywords are restricted words with a definite, predetermined meaning. Any programme statement may be defined with the aid of keywords.An identifier is a unique name created by the programmer to define a variable, structure, class, or function.
2A keyword always starts in lowercase.The initial character in the identification can be capitalised, lowercase, or start with an underscore.
3The kind of entity is defined.The entity’s name is categorised.
4It can only have alphabetical characters.It can have numbers, alphabetical characters, and underscores.
5It should be lowercase.There are uppercase and lowercase options.
6It aids in defining a certain attribute present in computer languages.It aids in locating the entity’s name.
7Examples of keywords are double, int, auto, char, break, and many others.Examples of IDs are test, count1, high speed, etc.

Conclusion

Java keywords are the building blocks of Java programming, defining the syntax and structure of the code. Identifiers, on the other hand, allow programmers to name variables, methods, classes, and other elements in a meaningful way. Adhering to Java’s conventions for both keywords and identifiers is essential for writing clear, readable, and efficient code. A solid grasp of these concepts is foundational for successful Java development, enabling programmers to navigate and utilize the language effectively.

Frequently Asked Questions

Q1. What are Java Keywords?

Ans: Java keywords are reserved words in Java that have predefined meanings in the language. They serve specific purposes and cannot be used as identifiers for naming variables, methods, classes, etc.


Q2. Can Java keywords be used as variable names?

Ans: No, Java keywords cannot be used as variable names or identifiers because they are reserved for special syntax in the language.


Q3. What is an identifier in Java?

Ans: An identifier is a name given to elements in a program, such as variables, methods, classes, and interfaces. Identifiers are user-defined names that follow certain rules.


Q4. Are Java identifiers case-sensitive?

Ans: Yes, Java identifiers are case-sensitive. For example, myVariable and MyVariable are considered two different identifiers.


Q5. Can an identifier start with a number in Java?

Ans: No, an identifier cannot start with a number in Java. It must begin with a letter (a-z or A-Z), a dollar sign ($), or an underscore (_).


Q6. What are some examples of valid Java identifiers?

Ans: Examples of valid Java identifiers include myVariable, _myVariable, $myVariable, and myVariable3.


Q7. Is it possible to use special characters in Java identifiers?

Ans: No, special characters other than underscore (_) and dollar sign ($) cannot be used in Java identifiers.