In this article, we will learn about keywords in Java. We will know about different keywords in Java.
What are Keywords in Java ?
keywords in Java are reserved words. These cannot be used as names of method, variable, class names and other identifiers. These keywords have predefined meanings w.r.t Java language.
Complete List of Keywords in Java
The below is the list of keywords in Java.
- new – used when creating a new object
- switch – please read Java Switch
- case – used in switch statements. Please read Java Switch
- assert – used in assertion statement
- default – used in switch statements(read Java Switch) and also in interface for a default method implementation(please read Interface Default and Static Methods)
- private – Access specifier for class, methods and variables. These can be only accessed inside a class where it is defined. Please read Access Modifiers
- protected – The protected access modifier is specified using the keyword protected. The data members, class or methods having protected access modifier are accessible within the same package and also in the subclasses in different packages. Please read Access Modifiers
- public – The public access modifier is specified using the keyword public. The data members, class or methods having public access modifier are accessible everywhere. Please read Access Modifiers.
- package – declares a Java Package
- synchronized – please read All About Synchronization
- transient – declares that an instance field is not part of the default serialized form of an object. Please read Serialization in Java
- for – used for looping.
- while – we use it to create a while loop.
- do – used in conjunction with while loop
- if – we use it to test a condition. If the condition returns true, it executes the block inside if.
- else – we use it to indicate the alternative branches in an if statement.
- this – please read this and super keywords
- super – please read this and super keywords
- break – please read break and continue in loop
- continue – please read break and continue in loop
- abstract – please read abstract keyword : classes and methods
- class – we use it to define a class.
- interface – we use it to define an interface.
- extends – A Class can use extends to extend another class. An interface can use extends to extend one or more interfaces.
- implements – we use it to implement an interface.
- throw – please read throw and throws
- throws – please read throw and throws
- import – we use it to import a package, class or interface.
- enum – please read Enums
- instanceof -we use it to test whether the object is an instance of the specified class or implements an interface.
- return – we use it to return from a method when its execution is complete. It will return a value if the method return type is not void.
- void – we use it to declare that a method does not return any value.
- try – It is used to start a block of code that will be tested for exceptions. The try block must be followed by either catch or finally block. Please read try, catch, and finally
- catch – used to catch the exceptions generated by try statements. It must be used after the try block only. Please read try, catch, and finally
- finally – used in conjunction with try. The finally block is executed after execution exits the try block and any associated catch clauses regardless of whether an exception was thrown or caught, or execution left method in the middle of the try or catch blocks using the return keyword. Please read try, catch, and finally
- byte – declares a variable that can hold an 8-bit data value.
- int – we can use it to declare a variable that can hold a 32-bit signed integer. please read Widening and Narrowing Primitive Conversions
- short – we can use it to declare a variable that can hold a 16-bit integer.
- char – we can use it to declare a variable that can hold unsigned 16-bit Unicode characters.
- long – we can use it to declare a variable that can hold a 64-bit integer.
- float – we can use it to declare a variable that can hold a 32-bit floating-point number.
- double – we can use it to declare a variable that can hold a 64-bit floating-point numbers.
- boolean – defines a boolean variable for the values “true” or “false” only.
- native – please read native keyword
- final – please read final keyword
- static – please read static keyword
- strictfp – please read strictly
- volatile – please read volatile
The below keywords are reserved but not used.
- const
- goto
Please note that true, false and null are literals, not keywords.
Conclusion: Keywords in Java
Keywords in Java empower us to communicate with the Java compiler effectively. By understanding their meanings and adhering to their usage rules, we ensure that our code follows the syntax and semantics of the language.