This article describes the list of keywords in the Java programming language. The keywords
are reserved, even though they are not currently used.
might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.
The abstract keyword is used to declare a class or a method as abstract. An abstract class is a class that is declared abstract means it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.
An abstract method is a method that is declared without an implementation.
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. Optionally enable by ClassLoader method.
Defines a boolean variable for the values "true" or "false" only. By default, the value of boolean primitive type is false. This keyword is also used to declare that a method returns a value of the primitive type boolean.
The byte keyword is used to declare a field that can hold an 8-bit signed two's complement integer. This keyword is also used to declare that a method returns a value of the primitive type byte.
A statement in the
switch block can be labeled with one or more
case or default labels. The switch statement evaluates its expression, then executes all statements that follow the matching case label.
Used in conjunction with a try block and an optional finally block. The statements in the catch block specify what to do if a specific type of exception is thrown by the try block.
- The char keyword is used to declare character variable.
- char is a Java primitive type.
- In Java, the data type used to store characters is char. The char data type is a single 16-bit Unicode character.
- It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
- The Wrapper Character class represents char primitive type as an object.
The class keyword is used to define the classes in Java. A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. In short, a class is the specification or template of an object.
Used to resume program execution at the end of the current loop body. If followed by a label, continue resumes execution at the end of the enclosing labeled loop body.
The continue statement skips the current iteration of a for, while or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop.
The
default keyword can optionally be used in a
switch statement to label a block of statements to be executed if no case matches the specified value; see a switch. Alternatively, the
default keyword can also be used to declare default values in a Java annotation. From Java 8 onwards, the
default keyword is also used to specify that a method in an interface provides the default implementation of a method.
The
do keyword is used in conjunction with while to create a
do-while loop, which executes a block of statements associated with the loop and then tests a boolean expression associated with the while. If the expression evaluates to
true, the block is executed again; this continues until the expression evaluates to
false.
- The double keyword is used to create a double primitive type.
- A double variable may store a double−precision floating point value. Double precision, as denoted by the double keyword, uses 64 bits to store a value.
- Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations.
- The Double class wraps a value of the primitive type double in an object. An object of type Double contains a single field whose type is double.
The Java if-else statement also tests the condition. It executes the if block if a condition is true otherwise else block, is executed.
Syntex
if(condition){
statement 1; //code if condition is true
}else{
statement 2; //code if condition is false
}
An enum Java keyword used to declare an enumerated type. Enumerations extend the base class Enum.
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY;
}
Read more about enum keyword with a complete example at Enums in Java.
The extends keyword is used in a class or interface declaration to indicate that the class or interface being declared is a subclass of the class or interface whose name follows the extends keyword.
The final keyword in Java is used to restrict the user. The final keyword can be used with variable, method, and class.
- The final keyword may be applied to a variable, indicating the value of the final variable can not be changed (It will be constant).
- The final keyword may be applied to a class, indicating the class may not be extended (subclassed).
- The final keyword may be applied to a method, indicating the method may not be overridden in any subclass
The finally used to define a block of statements for a block defined previously by the try keyword. 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.
- Java finally block is a block that is used to execute important code such as closing connection, streametc.
- Java finally block is always executed whether an exception is handled or not.
- Java finally block follows try/catch block.
- For each try block, there can be zero or more catch blocks, but only one finally block.
- The finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).
The float keyword is used to declare float primitive variable. A float variable may store a single−precision floating point value. The type float specifies a single-precision value that uses 32 bits of storage. Single precision is faster on some processors and takes half as much space as double precision, but will become imprecise when the values are either very large or very small.
The
for keyword is used to create a
for loop, which specifies a variable initialization, a boolean expression, and an incrementation. The variable initialization is performed first, and then the boolean expression is evaluated. If the expression evaluates to
true, the block of statements associated with the loop is executed, and then the incrementation is performed. The boolean expression is then evaluated again; this continues until the expression evaluates to
false.
The
if keyword is used to create an
if statement, which tests a
boolean expression; if the expression evaluates to
true, the block of statements associated with the if statement is executed. This keyword can also be used to create an
if-else statement.
The implements keyword is used in a class declaration to indicate that the class being declared provides implementations for all methods declared in the interface whose name follows the implements keyword.
The import keyword makes one class or all classes in a package visible in the current Java source file. Imported classes can be referenced without the use of fully−qualified class names.
In simple words, if a class wants to use another class in the same package, the package name does not need to be used. Classes in the same package find each other.
The Java instanceof keyword is used to test whether the object is an instance of the specified type (class or subclass or interface).
Syntex:
The left side is the instance and right side is the Java class name. Java instanceof operator returns a boolean result.
The instanceof in Java is also known as type comparison operator because it compares the instance with type. It returns either true or false. If we apply the instanceof operator with any variable that has a null value, it returns false.
- The int keyword is used to declare a variable as a numeric type. For example, int a = 10;
- A int variable is a signed 32-bit type that has a range from –2,147,483,648 to 2,147,483,647.
- The Integer class is a wrapper class for the int primitive type. It defines MIN_VALUE and MAX_VALUE constants representing the range of values for this type.
Used to declare a special type of class that only contains abstract or default methods, constant (static final) fields and static interfaces. It can later be implemented by classes that declare the interface with the implements keyword. As multiple inheritance is not allowed in Java, interfaces are used to circumvent it. An interface can be defined within another interface.
public interface Vehicle {
String getBrand();
String speedUp();
String slowDown();
default String turnAlarmOn() {
return "Turning the vehice alarm on.";
}
default String turnAlarmOff() {
return "Turning the vehicle alarm off.";
}
static int getHorsePower(int rpm, int torque) {
return (rpm * torque) / 5252;
}
}
- The long keyword is used to declare a variable as a long primitive type.
- The long primitive type is a signed 64-bit type and is useful for those occasions where an int type is not large enough to hold the desired value.
- The Long class is a wrapper class for the long primitive type. It defines MIN_VALUE and MAX_VALUE constants representing the range of values for this type.
- The native keyword may be applied to a method to indicate that the method is implemented in a language other than Java.
- The native keyword is used to declare a method which is implemented in platform-dependent code such as C or C++. When a method is marked as native, it cannot have a body and must ends with a semicolon instead.
- The Java Native Interface (JNI) specification governs rules and guidelines for implementing native methods, such as data type conversion between Java and the native application.
The new keyword is used to create a new instance of a class.
The new Java Keyword Examples
- The new keyword is used to create a new instance of a class.
Student student = new Student("Tom", 20);
- The new keyword can be used to create a new array object:
// use the new keyword to create an int array object
int[] intArray = new int[10];
- The new keyword can be used to create a new String array object:
// use the new keyword to create a String object
String string = new String();
Java package is a group of similar classes and interfaces. Packages are declared with the package keyword.
For example:
package java.lang;
package java.util;
The private keyword is used in the declaration of a method, field, or inner class; private members can only be accessed by other members of their own class
The protected keyword is used in the declaration of a method, field, or inner class; protected members can only be accessed by members of their own class, that class's subclasses or classes from the same package
The public keyword is an access control modifier that may be applied to a class, a method or a field (a variable declared in a class).
If a class or its members are declared as public, they can be accessed from any other class regardless of the package boundary. It is comparable to a public place in the real world, such as a company cafeteria that all employees can use irrespective of their department.
The return keyword causes a method to return to the method that called it, passing a value that matches the return type of the returning method.
- The short keyword is used to declare a variable as a numeric type.
- short is a signed 16-bit type.
- It has a range from –32,768 to 32,767.
- The Short class is a wrapper class for the short primitive type. It defines MIN_VALUE and MAX_VALUE constants representing the range of values for this type.
The static keyword is used to declare a field, method, or inner class as a class field. Classes maintain one copy of class fields regardless of how many instances exist of that class. static also is used to define a method as a class method. Class methods are bound to the class instead of to a specific instance, and can only operate on class fields.
strictfp is a
keyword in the
Java programming language that restricts
floating-point calculations to ensure portability.
Read more about strictfp keyword with a complete example at strictfp.
The super keyword in Java is a reference variable that is used to refer parent class object.
The super has two general forms:
- The first calls the superclass constructor.
- The second is used to access methods or instance variables of the superclass that has been hidden by a member of a subclass.
The switch statement is Java’s multiway branch statement. It provides an easy way to dispatch execution to different parts of your code based on the value of an expression. As such, it often provides a better alternative than a large series of
if-else-if statements.
Used in the declaration of a method or code block to acquire the mutex lock for an object while the current thread executes the code.
We can synchronize our code in either of two ways. Both involve the use of the synchronized keyword.
- Using Synchronized Methods
- Using Synchronized Statement Or Block
In Java, this is a reference variable that refers to the current object.
Causes the declared exception instance to be thrown. This causes execution to continue with the first enclosing exception handler declared by the catch keyword to handle an assignment compatible exception type. If no such exception handler is found in the current method, then the method returns and the process is repeated in the calling method. If no exception handler is found in any method call on the stack, then the exception is passed to the thread's uncaught exception handler.
The Java throws keyword is used to declare an exception. It gives information to the programmer that there may occur an exception so it is better for the programmer to provide the exception handling code so that normal flow can be maintained.
Java
transient keyword is used in serialization. If you define any data member as transient, it will not be serialized.
Let's take an example, I have declared a class as
Employee, it has three data members id, name, and age. If you serialize the object, all the values will be serialized but I don't want to serialize one value, e.g.
age then we can declare the age data member as
transient.
Enclose the code that might throw an exception within a try block. If an exception occurs within the try block, that exception is handled by an exception handler associated with it. The try block contains at least one catchblock or finally block.
The void keyword is used to declare that a method does not return any value.
Used in field declarations to specify that the variable is modified asynchronously by concurrently running threads. Methods, classes, and interfaces thus cannot be declared volatile, nor can local variables or parameters.
The
while keyword is used to create a
while loop, which tests a
boolean expression and executes the block of statements associated with the loop if the expression evaluates to true; this continues until the expression evaluates to false. This keyword can also be used to create a do-while loop.
Simple while Loop Example
Here is a while loop that counts down from 10, printing exactly ten lines of "tick":
package net.javaguides.corejava.controlstatements.loops;
public class WhileLoopExample {
public static void main(String args[]) {
int n = 10;
while (n > 0) {
System.out.println("tick " + n);
n--;
}
}
}
Output:
tick 10
tick 9
tick 8
tick 7
tick 6
tick 5
tick 4
tick 3
tick 2
tick 1
Although goto keyword is reserved as a keyword in Java, that goto is not used and has no function.
Although const keyword is a reserved in Java,
const is not used and has no function. For defining constants in Java, see the
final keyword.
Good java keywords reference !.
ReplyDelete