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.
List of Java Reserved Keywords
Keyword | Description |
abstract | Indicates the class or method that follows this keyword is abstract and that will have to be implemented by a subclass. |
assert | Assert 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. |
boolean | Defines two Boolean values, true or false, 0 and 1. |
break | Used to break out of loops or iterative constructs. |
byte | Data type capable of holding 8-bit data. |
case | Marks blocks of text (cases) in a Switch statement. |
catch | Used to catch exceptions generated in the try block. |
char | Data type able to hold unsigned 16-bit Unicode characters. |
class | Used to declare a new class. |
continue | It helps to take control outside the loop and continue to the next iteration. |
default | Defines the “block of code” that will execute by default in a Switch statement. |
do | Starting keyword for “do-while” loop. |
double | Data type holding 64-bit numbers (floating-point). |
else | Defines else part in the ‘if’ statements. |
enum | Used to declare enumerations in Java. |
extends | Indicates inheritance. A class is derived or inherited from another class. |
final | Defines a variable which will hold constant values or a method that cannot be overridden. |
finally | Defines the finally block that executes after the try-catch block irrespective of whether the exception was caught or not. |
float | Data type able to hold 32-bit floating-point values. |
for | Indicates the start of a ‘for’ loop. |
if | Start of ‘if’ statement. |
implements | Indicates that a class implements an interface. |
import | Used to include or reference other packages/classes into the program. |
instanceOf | Used to check if the given object is an instance of another class. |
int | Data type to hold a 32-bit integer value. |
interface | Used for declaring an interface. |
long | Data type holding 64-bit integer values. |
native | Used to indicate native code (platform-specific). |
new | Operator to create a new object. |
null | Indicates null reference. |
package | Keyword to declare Java package. |
private | Indicates private access specified which means a variable or method can be accessed only by the class in which it is declared. |
protected | This 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. |
public | The public keyword is used to indicate public access specifier. A variable, method, classes, interfaces declared as public can be accessed throughput the application. |
return | Return 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. |
short | Data type holding 16-bit integer number values. |
static | The static keyword indicates the method or a variable is static and cannot be instantiated. |
strictfp | The keyword strictfp restricts the rounding and precision of floating point values calculation. It ensures portability. |
super | Indicates base or superclass of the class. |
switch | Indicates a Switch statement that tests a condition and executes multiple cases depending on the test value. |
synchronized | Indicates synchronized sections for multithreaded code like critical section. |
this | The keyword ‘this’ indicates the current object. |
throw | Throws an exception. |
throws | This indicates the exception that can be thrown by a method. |
transient | Specifies transient variable that is not part of the persistent state of an object. |
try | Try keywords start a block that contains code that might raise exceptions. |
void | Indicates no return value. |
volatile | Used to define variables that are not stored in Main Memory. They can be changed asynchronously. |
while | Keyword while starts a while loop. |
const | The ‘const’ keyword is no more supported in Java |
goto | The ‘goto’ keyword is no more supported in Java |
true, false and null | The 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:
- 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.
- Subsequent Characters: After the initial character, identifiers may contain letters, digits, underscores, or dollar signs. They cannot contain spaces or other special characters.
- Case Sensitivity: Java identifiers are case-sensitive, meaning uppercase and lowercase letters are distinct. For example, “myVariable” and “MyVariable” are treated as different identifiers.
- 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.
- 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 1 – Variables
int itemCount;
double totalPrice;
String customerName;
JavaExample 2 – Class
public class ShoppingCart {
// Class members and methods
}
Javaclass OrderProcessing {
// Class members and methods
}
JavaExample 3 – Method
public void calculateTotalPrice() {
// Method implementation
}
private String formatCustomerName(String firstName, String lastName) {
// Method implementation
}
JavaExample 4 – Interface –
public interface PaymentGateway {
// Interface methods
}
JavaJava Keywords vs Identifiers
Here are some key differences between keywords and identifiers:
S.No. | Keyword | Identifier |
1 | Keywords 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. |
2 | A keyword always starts in lowercase. | The initial character in the identification can be capitalised, lowercase, or start with an underscore. |
3 | The kind of entity is defined. | The entity’s name is categorised. |
4 | It can only have alphabetical characters. | It can have numbers, alphabetical characters, and underscores. |
5 | It should be lowercase. | There are uppercase and lowercase options. |
6 | It aids in defining a certain attribute present in computer languages. | It aids in locating the entity’s name. |
7 | Examples 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
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.