Home >Java >javaTutorial >Java Keywords
A Java keyword is a reserved word in a programming language that is not used as an Identifier. Java Keywords are called reserved words because they are ‘reserved’ by the language you are using. They have a special meaning which is defined in the library of the language.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Like any other programming language, a Keyword in Java programming language is a reserved word with a special meaning. Keywords are reserved for internal processes and have some predefined actions. In Java, if the keywords are used otherwise, the program or code will encounter a ‘Compile Time Error.’ For ease of use for the programmer and identification of the Keywords, generally, they are a bit ‘highlighted’ in Java.
We have tried to list the important keywords in use currently in Java below in alphabetical order. You can also observe the working of every keyword by following the given examples for the same. Let’s see these keywords one by one.
1. Abstract: Abstract The keyword is used to declare a class to be used later in the abstract program. Abstract used with a class declaration makes that class an abstract and specifies that the class will be implemented in a subclass at a later stage. Below is an example of the usage of the ‘Abstract’ keyword.2. Assert: The ‘assert’ keyword was added to Java 1.4. This keyword allows a programmer to test his assumptions in the program.3. Boolean: Boolean is a data type that can hold only two values; it’s either a ‘True’ or a ‘False’.4. Break: This keyword helps the programmer want his code to jump out of a loop, for example, a For loop. It’s a control statement that breaks the current flow of the execution on a specific condition provided by the programmer.5. Byte: Byte keyword is one of the data types that are able to store whole numbers. The numbers range from -128 to 1276. Case: Case keyword is used in a switch statement. The ‘case’ keyword creates a block of code and marks it.7. Catch: The ‘catch’ keyword catches the exceptions that were created by the ‘try’ statements. The catch block is used after Try block.8. Char: The ‘char’ is one of the data types that is used to hold one character at a time.9. Class: The class keyword is used to create a class in java. Everything that runs in Java programming resides inside a class.10. Continue: The ‘continue’ keyword is used to skip the current execution and continues to the next step in a loop like a For loop or a While loop.11. Default: The ‘Default’ keyword is used to create a default block of code containing a set of statements executed by default if the required case is not present.12. Do: The ‘do’ keyword is used when creating a ‘do-while’ loop. The ‘do’ block is executed at least once before checking the condition in a while block.13. Double: The ‘double’ keyword is one of the data types used for holding fractional numbers. The fractional numbers range from 1.7e – 308 to 1.7e + 308. While declaring a fraction, the value is ended with a ‘d’.14. Else: The ‘Else’ keyword is used to create a block of code that will be executed if the condition is not ‘True’ in an ‘If’ statement.15. Enum: The ‘enum’ keyword is used to create enumerations, unchangeable data types. The enum keyword defines a fixed set of constants.16. Extends: Extends keyword is used to inherit properties of a superclass’s attributes and methods by a subclass.17. Final: The ‘Final’ keyword is used to set a final value to a variable, making it impossible to overwrite or change altogether. If the value of a final variable is tried to change, it throws an error. When hovered over, it shows the following error.18. Finally: Finally keyword is used along try and catch statements, which has a block that is executed even if the exception is not handled.19. Float: The float keyword is a data type that holds fractional numbers that range from 3.4e−038 to 3.4e+038. While declaring a fractional number, the value should be ended with an ‘f’.20. For: The ‘For’ keyword is used to create a loop, a for a loop. The for loop executes the enclosed set of code a number of times, which is defined inside the for loop condition statement.21. If: The ‘If’ keyword is used to create a set or block of code or statements that are executed if the condition is ‘True’.22. Implements: Implement is another keyword that is reserved for declaring interfaces. It is used during the declaration of the class and specifies one or more interfaces.23. Import: The import keyword is used for importing packages or a class or classes or maybe an interface as well.24. Instanceof: The ‘instanceof’ keyword is used to check if the object in question is an instance of the class or the interface.25. Int: The ‘int’ keyword is one of the data types for declaring an integer type.26. Interface: The interface keyword is used to declare a special type of class that will only contain abstract methods. For accessing the interface methods, the interface must be “implemented” like we use the inheritance concept by another class by using the ‘implements’ keyword.
27. Long: The ‘long’ keyword is again one of the data types for holding whole numbers. They range from -9223372036854775808 to 9223372036854775808. The whole number value should end with an ‘L’.
28. New: The ‘new’ keyword is used to create an object.
29. Package: The ‘package’ keyword is used to create packages.30. Private: The ‘private’ keyword is used to create attributes or methods for a private mode class. ‘Private’ is an access modifier that allows only the declared class to use private properties or attributes or methods.31. Protected: ‘Protected’ is an access modifier that allows the properties or methods to be used only by the same package or subclasses.32. Public: The ‘public’ keyword is another access modifier that allows one class to use a publicly defined class’s attributes or methods or etc., within itself.
Below is a publicly defined class ‘test.java’ and another class, class ‘Test2.java’, access it.Class – Test2.java33. Return: The ‘return’ keyword can be used as an execution end for a method, and also, it is used to return a value from a method.34. Short: The ‘short’ keyword is a type of data type that is used to hold the whole number ranging from -32768 to 32767.35. Static: The ‘static’ keyword is used to create a method static, which means that method can be called without creating its object.36. Super: The ‘super’ keyword is used to call the super or parent class or objects.37. Switch: The ‘switch’ keyword is used to create different scenarios into different cases and is used to select one out of them. The switch statement contains a value or expression, which then is compared with all the cases present, and the match one is selected.38. This: the ‘this’ keyword is used to refer to the current object of a method or a constructor.39. Throw: The ‘throw’ keyword is used to throw an exception message on the screen for the user.40. Throws: The ‘throws’ keyword is used when the programmer want to decide and throw a type of exception.41 specifically. Try: The ‘try’ keyword starts a ‘try-catch’ block. The ‘try’ block is where the exceptions are tested.42. Void: The ‘void’ keyword, when used with a method, specifies that the method will not have a return value.43. While: The ‘while’ keyword creates a loop with a condition in its statements. The loop is executed until the statement is true.
Keywords are the tokens in the Java programming language that are reserved and have special meaning. They have a reserved use and a predefined action in the language.
The above is the detailed content of Java Keywords. For more information, please follow other related articles on the PHP Chinese website!