java

View on GitHub

1.02 Reserved Words

In Java some words are reserved to represent some meaning or functionality such type of words are called Reserved Words

Reserved Word Description
null Literal (Default value for object reference) Indicates that a reference does not refer to anything
true Literal Indicates that a reference condition is true
false Literal Indicates that a reference condition is false
const Unused Unused Reserved Keywords
goto Unused Unused Reserved Keywords
byte DataType A data type that can hold 8-bit data values
short DataType A data type that can hold a 16-bit integer
int DataType A data type that can hold a 32-bit signed integer
long DataType A data type that holds a 64-bit integer
float DataType A data type that holds a 32-bit floating-point number
double DataType A data type that can hold 64-bit floating-point numbers
boolean DataType A data type that can hold True and False values only
char DataType A data type that can hold unsigned 16-bit Unicode characters
if FlowControl Tests a true/false expression and branches accordingly
else FlowControl Indicates alternative branches in an if statement
switch FlowControl A statement that executes code based on a test value
case FlowControl Used in switch statements to mark blocks of text
default FlowControl Specifies the default block of code in a switch statement
while FlowControl Starts a while loop
do FlowControl Starts a do-while loop
for FlowControl Used to start a for loop
break FlowControl A control statement for breaking out of loops
continue FlowControl Sends control back outside a loop
return FlowControl Sends control and possibly a return value back from a called method
public Modifier An access specifier used for classes, interfaces, methods, and variables indicating that an item is accessible throughout the application (or where the class that defines it is accessible)
private ModifierAn access specifier indicating that a method or variable may be accessed only in the class it’s declared in
protected Modifier An access specifier indicating that a method or variable may only be accessed in the class it’s declared in (or a subclass of the class it’s declared in or other classes in the same package)
static Modifier Indicates that a variable or method is a class method (rather than being limited to one particular object)
final Modifier Indicates that a variable holds a constant value or that a method will not be overridden
abstract Modifier Specifies that a class or method will be implemented later, in a subclass
synchronized Modifier Specifies critical sections or methods in multithreaded code
assert Modifier Assert describes a predicate (a true–false statement) placed in a Java program to indicate that the developer thinks that the predicate is always true
native Modifier Specifies that a method is implemented with native (platform-specific) code
strictfp Modifier Version A Java keyword used to restrict the precision and rounding of floating point calculations to ensure portability.
transient Modifier Specifies that a variable is not part of an object’s persistent state
volatile Modifier Indicates that a variable may change asynchronously
try Exception Starts a block of code that will be tested for exceptions
catch Exception Catches exceptions generated by try statements
throw Exception Creates an exception
throws Exception Indicates what exceptions may be thrown by a method
finally Exception Indicates a block of code in a try-catch structure that will always be executed
assert Exception Exception Assert describes a predicate (a true–false statement) placed in a Java program to indicate that the developer thinks that the predicate is always true at that place. If an assertion evaluates to false at run-time, an assertion failure results, which typically causes execution to abort.
package ClassR Declares a Java package classes
import ClassR References other classes
class ClassR Declares a new class
interface ClassR Declares an interface
extends ClassR Indicates that a class is derived from another class or interface
implements ClassR Specifies that a class implements an interface
new ClassR Creates new objects
instanceof ClassR Indicates whether an object is an instance of a specific class or implements an interface
super ClassR Refers to a class’s base class (used in a method or class constructor)
this ClassR Refers to the current object in a method or constructor
void Specifies that a method does not have a return value
enum Version A Java keyword used to declare an enumerated type. Enumerations extend the base class.

NOTE 01

  1. In java return type is mandatory, if a method doesn’t return anything then we have to declare the method with void return type.
  2. Usage of goto created several problems hence it is banned in Java.
  3. Use final instead of const
  4. The keywords const and goto are reserved, even they are not currently in use.
  5. We can use enum to define a group of named constants.

Example of enum:

enum month {
    JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEPT,OCT,NOV,DEC
}

Conclusions

  1. All 53 reserved words contains lower-case alphabets only instanceof, strictfp
  2. In Java we have only new keyword and there is no delete keyword because destruction of useless objects is the responsibility of Garbage Collector.
  3. The following are newly added Keywords in Java:

    • strictfp Version
    • assert Version
    • enum Version

NOTE 02

  1. strictfp but not strictFp
  2. instanceof but not instanceOf
  3. synchonized but not synchronise
  4. extends but not extend
  5. implements but not implement
  6. import but not imports
  7. const but not constant

TEST

Q. Which of the following lists contains only Java reserved words?

Sr. No. word
1. new, delete
2. goto constant
3. break, continue, return, exit
4. final, finally, finalize
5. throw, throws, thrown
6. notify, notifyAll
7. implements, extends, imports
8. sizeof, instanceof
9. instanceOf, strictFp
10. byte, short, Int
11. None of Above

Answers

Sr. No. word Answer
1. new, delete delete is not a keyword in Java
2. goto, constant const is keyword, constant is not a keyword
3. break, continue, return, exit exit is not a keyword in Java (Method)
4. final, finally, finalize finalize is not a keyword in Java
5. throw, throws, thrown thrown is not a Keyword in Java
6. notify, notifyAll both are not Java Keywords. (Methods)
7. implements, extends, imports imports is not a Java Keywords
8. sizeof, instanceof sizeof is not a Java Keyword
9. instanceOf, strictFp Java keywords contain lower-case alphabets only
10. byte, short, Int Java keywords contain lower-case alphabets only
11. None of Above Solution: No list contains Java keywords only.

Q. Which of the following are Java reserved words?

public, static, void, main, String, args :
First three only. String is a ‘pre-defined’ class name.

Next Topic: 1.03 Data Types