Java Keywords
Some identifiers are reserved in Java which has separate functionality and meaning such type of reserved identifiers are called reserved words.
Java has 54 reserved keywords. We can divide them into the following categories.
- Primitive types and void: 9 keywords
- boolean: creates a boolean variable. The only possible values are true and false and the default value is false.
- byte: creates a byte variable. A byte takes 8-bits and ranges from -128 to 127.
- char: used to create a character variable. It takes 2-bytes and it’s unsigned. The value ranges from 0 to 65,536.
- short: create a short variable of 2-bytes. The value ranges from -32,768 to 32,767.
- int: create an integer variable, takes 4-bytes and the range is from -2,147,483,648 to 2,147,483,647
- long: creates a long variable, takes 8-bytes and the range is from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807. - float: creates a signed floating point variable using 4-bytes.
- double: creates a signed double using 8-bytes.
- void: used with methods to specify that it doesn’t return anything.
2.Keywords for flow control: 11
- if: used to create if statement.
- else: used in conjunction with if to create an if-else statement.
- switch: used to create switch-case statements.
- case: used in conjunction with switch to create switch-case statements.
- default: used with the switch-case statements for the default case. From Java 8 onwards, it can also be used to create default methods in the interfaces. We can also use it to declare default value in an annotation
- for: used to create a for loop.
- do: used in conjunction with while to create a do-while loop.
- while: can be used to create while loop or do-while loop.
- break: used in the loops to end the execution of the current loop body.
- continue: used in the loops to skip the execution of the current cycle and proceed with the next cycle
- return: used to return value from a method.
3.Keywords for Exception Handling: 6
- throw: used to throw exceptions.
- throws: used with methods to specify the exceptions that the method may throw.
- assert: added in Java 1.4 to create assertions.
- try: used to create a block of code for exception handling.
- catch: used in conjunction with try block to catch the exceptions and process them.
- finally: used with try-catch block. The finally block code is always executed.
4.Keywords for modifiers: 11
- strictfp: used to restrict the precision and rounding of floating point calculations to enure portability.
- public: used with class, methods, and fields to define their scope. The private identifiers can be accessed from anywhere.
- protected: used with inner class, methods, and fields. The protected members are accessible only from within the class, the sub-classes and the other classes in the same package.
- private: the private keyword is used with class variables, methods, and inner classes. The private members are accessible only within the class code.
- abstract: used to implement abstraction in Java. It’s used with a class declaration to create an abstract class. It can also be used with methods inside an abstract class to declare abstract methods. The abstract methods must be implemented by the subclass. We can’t create an instance of an abstract class.
- static: can be used with fields, methods, and inner class. The static members belong to the class and shared by all the instances of the class.
- final: used with class, fields, and methods. The final class can’t be extended. The final fields value can’t be changed, once assigned. The final method can’t be overridden in the subclass.
- transient: used with class fields to declare that they won’t be part of serialization. When an object is serialized, only non-transient fields are part of the serialization process. When the object is deserialized, the transient fields are assigned with their default values.
- volatile: used with class fields to declare that their value might change by other threads. It was intended to use in case of multithreading, but it has several issues and it’s better to stick with synchronization.
- synchronized: used with a method or to create a code block. It’s used to create a code block that can be executed by only one thread at a time. It’s very useful in maintaining data consistency in a multithreaded environment.
- native: used with java method declaration to specify that the method is not implemented in the same Java class, but rather in another language. For example, System class currentTimeMillis() and arraycopy() are native metho
5.class related key words : 7
- class: used to create a class.
- interface: to create an interface.
- package: defines the package for the class, interface, or enum definitions.
- extends: used to create a subclass by extending another class.
- implements: used to implement an interface.
- import: used to import a class so that we can use its functions.
- enum: added in Java 1.5 to create an enum
6.Object related key words:4
- new: used to create an instance by calling the constructor.
- instanceOf : An operator to check if an object is instance of a class.
- super: used incase of inheritance to access superclass methods, constructors, and variables.
- this :used to get access to the current object.
7.Unused keywords: 2
- const
- goto
8.Miscellaneous Keywords : 4
- _ (underscore): added in Java 1.9 for underscore literals.
- null: used to define null values of a variable.
- true: a boolean literal, returned when a condition is true.
- false: a boolean literal, returned when a condition is false.
Comments
Post a Comment