Important Java Keywords with Syntax – Part 2
Keywords play a key role in every programming language. They make tasks easier and simpler and are inevitable during the construction of code. Have you ever thought about what would happen if we were unaware of the keywords present in a particular language?
Here is a small demonstration of it. Look at the code below:
Sample program to import input and output classes using super keyword:
import java.io.*; class FirstCode{ public static void main(String args[]){ System.out.println(super); } }
Output:
System.out.println(super);
1 error
Why should I learn Java keywords:
As seen in the above example program, the lack of knowledge about keywords results in lots of errors. Knowing these reserved words and their usage helps a person achieve the desired result effortlessly via programming.
Therefore, keywords form a basic foundation for any programming language. Thus, it becomes highly essential for a programmer to beware of those words before getting into the field.
List of important Java keywords:
Here is a list of all important keywords in Java:
S.No | Keyword | Usage |
1 | abstract | It denotes that a class or method will be implemented later as a subclass. |
2 | assert | It implies that the programmer has assertions or assumptions in the program. If this assertion is encountered to be true, the program runs in a normal mode. Else, the AssertionError will occur at runtime to abort the program. |
3 | boolean | The data type that holds the values true or false. |
4 | break | It breaks out of the loops. |
5 | byte | Data type that holds 8-bit data values. |
6 | case | It denotes individual blocks of text in a switch statement. |
7 | catch | It catches the exceptions thrown by try block. |
8 | char | A data type that holds unsigned 16-bit Unicode characters |
9 | class | It denotes the declaration of a class. |
10 | continue | Sends the control outside the loop block. |
11 | default | It denotes the default code block in switch |
12 | do | Denotes the beginning of a do-while loop |
13 | double | The data type that holds 64-bit floating-point numbers |
14 | else | It includes the alternative block that executes when the ‘if’ block results in false. |
15 | enum | Denotes enumerated data type. |
16 | extends | Specifies that a class is derived from another class. |
17 | final | Specifies that no overring can take place over the value. |
18 | finally | It consists of a code block in a try-catch structure that will predominantly execute. |
19 | float | Data type that holds 32-bit floating-point value. |
20 | for | Indicates the beginning of a ‘for’ loop. |
21 | if | Indicates the beginning of the ‘if’ loop. |
22 | implements | Indicates that the class implements an interface |
23 | import | Denotes the presence of other packages or classes in the program. |
24 | instanceof | We can check if the object is an instance of another class using this keyword. |
25 | int | Data type that holds 32-bit integer. |
26 | interface | To declare an interface. |
27 | long | Data type that holds 64-bit integer values. |
28 | native | Indicates native code. |
29 | new | Keyword to create a new object. |
30 | null | Denotes null reference. |
31 | package | Declaring a package. |
32 | private | It denotes that the variable or method can be accessed only by the class where it is declared. |
33 | protected | It denotes that the variable or method can be accessed by the class, its subclass, and other classes under the same package. |
34 | public | It denotes that the variable or method can be accessed throughout the application. |
35 | return | Returns a value to the method when called. |
36 | short | A data type that holds a 16-bit integer. |
37 | static | To denote that a variable or method is a class. |
38 | strictfp | Prevents the precision and rounding of values present in float data type. |
39 | super | Refers to the base class of a class in the method. |
40 | switch | It indicates the switch statement to test a condition and execute based on the test value. |
41 | synchronized | Used in multithreaded code to denote critical sections and methods. |
42 | this | Refers to the particular object in a method. |
43 | throw | It creates an exception. |
44 | throws | It denotes the exception that the method is likely to throw. |
45 | transient | If any variable or data member is defined as a transient, it cannot be serialized. |
46 | try | It indicates the start of a test block looking for exceptions. |
47 | void | It denotes that the method returns no value. |
48 | volatile | We use this keyword to denote that a variable tends to change asynchronously. |
49 | while | It indicates the start of a while loop. |
The explanation for the 25 keywords given in the above tabel are explained in the article “Important Keywords in Java – Part 1”. You can go through it before getting into the specifications of the keywords given in this article. Let us now see these java keywords in detail:
Explanation of Java keywords:
1. interface in java
We can create interfaces using the interface keyword in Java. These interfaces contain only abstract methods with no method body and implementation.
interface FirstCodeInterface{ //abstract methods }
2. long in java
The keyword long is a data type to declare a variable as a long integer. It usually holds a 64-bit long integer number. As we use f or F to denote float numbers, we use l or L to denote long values.
long num = 67892L;
3. new in java
The new keyword helps us create an object for the class. Even though there are many other ways to create an object, using the new keyword is the most preferred method to achieve it. You can refer to our article about Java object creation to know the other ways to create an object.
Firstcode obj = new FirsstCode(); // here, the object is obj.
4. native in java
When we implement a method using Java Native Interface(JNI), the native can be used to keyword specify it. This keyword acts as a modifier only for a method; it cannot be used with any other entities.
class FirstCode{ //statements } public native String testMethod (String parameter);
5. null in java
The keyword null refers to nothing. It represents a null value. Variables are usually declared as null values.
String str; str = null; //here, the string str holds no value
6. package in java
We use the package keyword to create a new package in Java. It is a collection of various classes and interfaces of the same type. Creating a package is helpful for us as it provides better readability and classification of numerous classes.
package com.firstcode.javacourse.keywords; public class FirstCodeKeywords{ //coding inside the class }
7. public in java
The keyword public is one of the three access specifiers in Java. It allows a keyword, class, constructor, method or interface to be accessed from anywhere. The scope of this access specifier is the widest when compared to the other specifiers.
public class FirstCode{ public int num; public void myCourse(); }
8. private in java
The next access specifier is private. This keyword lets the variables, methods, constructor, interface and class be accessed only within the class where it is declared. We can access those values from outside the class.
It is the most restricted access specifier keyword.
private class FirstCode{ private int num; private void addition(); }
9. protected in java
The last access specifier keyword is protected. It shows some liberty in letting the variables, constructor and method be accessed by the subclass of that class. These subclasses should belong to the same package. It applies only to variables and methods, not to classes and interfaces.
protected int num; protected void addition();
10. return in java
When we require a class to return a specific value, we use the return keyword. Whenever a return keyword is found, the control flow of the program redirects to the calling function.
public int getAge(int age){ age = 21; return age; }
11. static in java
We usually use the static keyword along with a data member, method or a block. It specifies the sharing of variables and methods inside the class.
public static void displayNum() static int num = 5;
12. short in java
The keyword short declares another data type. This data type holds values of a 16-bit integer. The range of values that can be used using this data type is -32, 768 to 32,767.
13. super keyword in java
The super keyword refers to an instance that belongs to the parent or superclass. It usually denotes the value related to the immediate parent class.
class FirsCode{ String course = “Java”; } class JavaCourse extends FirstCode{ void displayCourseName(){ System.out.println(super.course); } } public class Main(){ public static void main(String args[]){ JavaCourse c = new JavaCourse(); c.displayCourseName(); } }
Output:
14. switch keyword in java
To execute the switch statement, we require the switch keyword. It consists of multiple cases in which one matches the condition gets executed.
Sample program to execute switch statement:
public class Rank{ public static void main(String args[]){ int displayrank = 3; switch(displayrank){ case 0: System.out.println(“Rank 1”); break; case 1: System.out.println(“Rank 2”); break; case 2: System.out.println(“Rank 3”); break; case 3: System.out.println(“Rank 4”); break; case 4: System.out.println(“Rank 5”); break; default: System.out.println("You did not come under first five ranks"); } } }
15. synchronized keyword in java
In the Java multi-threaded environment, we can achieve synchronization using the synchronized keyword. This limits the access of methods to only one resource at a time.
synchronized void methodName{ statements }
16. strictfp in java
The strictfp keywords provide platform-independent features to the code. In simple words, the code can return the same output on numerous platforms. The purpose of the strictfp keyword is to guide if the code is running perfectly on every platform. We can use this keyword with classes, interfaces and methods.
Using strictfp keyword with class:
strictfp class FirstCode{ //statements } Using strictfp keyword with interface: strictfp interface FirstCode{ //statements } Using strictfp keyword with a method: strictfp void FirstCode{ //statements }
17. this keyword in java
This keyword in Java is used to indicate current instance in a class that belongs to a method or constructor. It eradicates the ambiguity between an object and parameters.
class FirstCode{ int num; FirstCode(int num){ this.num = num; } }
18. throw keyword in java
Whenever an exception is encountered, this keyword throws the exception. For example, when a user gives more number of values exceeding the limit of an array, the throw keyword throws an exception. A custom exception can also be thrown using this keyword.
public throwingException{ public static void marks(float m){ if(m<40) throw new FailException("You do not pass the minimum eligibility"); else System.out.println("You are eligible to graduate"); } public static void main(String args[]){ throwingException(35); } }
Output:
19. throws keyword in java
You should not confuse between the reserved words throw and throws. The throws keyword also encounters exceptions and throws them. The only difference is that this keyword looks only for the checked exceptions in Java. It is kind of an alert message to the programmers to avoid such exceptions.
public void courseDetails() throws IOException{ //statements }
20. try in java
It plays a major role in exception handling concept. When we want to test if a block of code contains any exceptions, we put it inside the try block. It is followed by a catch or, finally, block.
try{ //statements } catch{ //statements }
21. transient keyword in java
This keyword brings in the concept of serialization in Java. Whenever a variable is used with transient, then that data will not be serialized.
transient int num;
22. void keyword in java
The methods that do not return any value are denoted as void methods.
public void display(){ // statements }
23. volatile keyword in java
When the value of a variable is subject to change asynchronously, it is indicated by the keyword volatile.
class FirstCode{ static volatile int num = 10; }
24. while keyword in java
This keyword is helpful in iterating a program numerous times. When we are unsure of the limit, loops make the task easier.
while(condition){ //statements }
Conclusion:
These are some of the most used Java reserved words. If you want the entire list, you can read the article “Important Keywords in Java – Part 1“. It is always better to have a glance at the entire keywords list. Yet, you can start your Java code with these words.