Keywords : a reserved word holding a predefined meaning to it!

Keywords in Java:

Rupam Jha
JavaMadeTranquil
Published in
11 min readOct 15, 2020

--

Hello Comrades,

As we have gathered to feed on some very basic yet another important topic today, lets do the talking about what a Keyword in Java is? … let us understand that a keyword is precisely any reserved word that holds a specific predefined meaning to it in a language.

I will be mentioning here the list of Java Keywords.[tip: don’t try memorizing this stuff, its just for making you familiar with what are their usages]

  • abstract
  • assert
  • boolean
  • break
  • byte
  • case
  • catch
  • char
  • class
  • count(unused)
  • continue
  • default
  • do
  • double
  • else
  • enum
  • extends
  • final
  • finally
  • float
  • for
  • goto(unused)
  • if
  • implements
  • import
  • instanceof
  • int
  • interface
  • long
  • native
  • new
  • package
  • private
  • protected
  • public
  • return
  • short
  • static
  • strictfp
  • super
  • switch
  • synchronized
  • this
  • throw
  • throws
  • transient
  • try
  • void
  • volatile
  • while
  1. abstract: It is a Java keyword applicable to class and methods. We cannot create an Object of an abstract class. The abstract keyword is recommended to be placed after the access modifier static. An abstract class cannot be final. If a method is declared as abstract, it will have only declaration of it and no implementation written in it. An abstract method cannot be private, final, static or native.

valid abstract class representation:

public static abstract int classA{};

invalid abstract class representation:

public final abstract int classA{};

invalid abstract method representation:

private static final native abstract int methodA(){};

2. assert: It provides definition to the assert statements. A statement is said to be an assert statement when it declares an expected boolean condition in a program. It allows us to test the correctness of any assumptions that the program has. If any program running with assertion enabled, its condition will be checked at runtime and if its condition is false, JVM will throw “AssertionError” and if its condition is true, it does nothing. To configure assert, we can use command line flags i.e. the -ea or the -da [to enable or disable] using a command line tool.

valid assert representation :

assert exp1 [:exp2]; i.e. exp1 is a boolean, that might throw assertion if its false. When an assertion is thrown , it creates AssertionError exception along with the parameter exp2.

3. boolean: It is a Java keyword which allocates either of the two values; true or false. By default its value is set to false. It can be used with a variable, method parameters and method return types. The size of the boolean depends on JVM as it is not precisely defined.

valid boolean representations:

boolean isAlligned = false; →representing a variable

public void setAlignment(boolean align){}; →representing a method parameter

public boolean isAligned(){};→representing a method return type

4. break : It is a Java keyword which terminates the execution of a for loop, while loop or a do-while loop condition thereby allowing the program to execute to the next steps. Using of a break keyword, refers to a “break statement”. A break statement helps us to exit from any block of code. They are of two forms namely Unlabeled break statement and Labeled break statement. The unlabeled break statements are without any labels, where as the labeled break statements holds a label name after the break statement.[as mentioned in egs. below]

A] Unlabeled break statement:

int i =1;

while(true){

if(i<10){

System.out.println(“Number is : ”+ i);

i++;

}else{

break;

}

}

B] Labeled break statement:

blocklabel{

int i =10;

if(i==5){

break blocklabel;

}

if(i==10){

System.out.println(“Number is : ” +i);

}

here, the break statement will eventually terminate the labeled statement and shall not transfer the flow of control to the label[blocklabel here]. Control is transferred to the statement immediately following the labelled statement.

5. byte: A 8 bit signed integer primitive type is designated to a byte keyword.

6. case: It is a Java keyword which is a part of a switch statement, to find if the value passed matches the value followed by any case. [mentioned in eg. below]

int i =5;

switch(i){

case 1:

System.out.println(“number is : 1 ” );

break;

case 3:

System.out.println(“number is : 1 ” );

break;

case 5:

System.out.println(“number is : 1 ” );

break;

default:

System.out.println(“number is not 1,3,5” );

}

7.catch: It is a Java keyword which is a part of try block. It is an exception which is thrown by the try block which later is compared to any exception type declared in this catch block and if the exception matches to the exception type present in the catch block , the exception gets handled.[we will discuss in detail on this in the upcoming articles].

8.char: It is a Java keyword which defines the primitive datatype character.

9.class: It is a Java keyword which begins the declaration and definition of a Java class.

10.const: It is a reserved keyword which are currently not being used. But is used to declare a constant in other languages like C++.

11.continue: It is a Java keyword which skips the current condition of the loop and continues with the next iterations.[mentioned in eg.]

int a =6;

for(int i =0;i≤a;i++){

if(i==3){

continue;

}

System.out.print(“iteration = ”+i);

}

o/p =0 1 2 4 5 6

12. default: It is a Java keyword in Java which the optional part of a switch statement and gets executed only when none of the case is matched.

13.do: It is a Java keyword which is a part of a do-while loop. The statement present within the do block always gets executed.

14.double: It is a Java keyword which stores the primitive data type 64-bit double’s value into it.

15.else: It is a Java keyword which is a part of a branching statement and gets executed only when the if condition is stated as false.

16.enum: It is a special class in Java, which represents a group of constants. It is also a Java keyword used to create enum class.

17.extends: It is a Java keyword used in a class or an interface definitions. Basically if any class or an interface extends[tries using methods or variables from a parent class] some class or interface it uses the extends keyword.

18.final: It is a Java keyword which is used for a class, a method, or a variable. If used for a class, we cannot inherit that class. If used for a method we cannot override that method and if used for a variable, we cannot change its value throughout the program execution.

19.finally: It is a Java keyword which is an optional part of try-catch block. The line of code present inside the finally block will always get executed no matter if the try block either has thrown an error or has a return statement. But in case any exception which happens before the try-catch block then finally block is not executed. If any return statement is used inside the finally block , it will always override the return method of the try-catch block.

20.float: It is a keyword designated to a 32-bit float primitive datatype.

21.for: It is a Java keyword used in a looping statement. It initiates the iteration from any mentioned range with in its parameter[ mentioned eg.]

for(int i -0; i< 10; i++){

System.out.println(“number is ” +i);

}

22.goto: It is reserved keyword currently not being used. It was used to jump out of a loop. As we have an alternative to it : labeled break statement, we don’t use it anymore.

23.if: It is a Java keyword used in a branching statement. It gets executed only when the if conditions are true.

24. implements: It is a Java keyword used to declare if an interface is being implemented by a class.

25.import: It is a Java keyword which is used to access all the Java classes and packages for the code written in any program.

26.instanceOf: It is a Java keyword which checks if an object reference is an instance of the type and returns true if the type matches.[mentioned eg.]

Apple instanceOf(Fruit);

27.int: It is a Java keyword which is designated to 32-bit primitive datatype integer.

28.interface: It is a Java keyword which is used to declare a Java Interface.

29.long: It is a Java keyword which is designated to a 64-bit signed integer primitive datatype.

30.native: It is Java keyword which marks a method which is implemented in any other language other than Java. The method will be without a body and cannot be abstract. It works along with JNI(Java Native Interface). The use of a native method was to write performance critical sections.

31.new: It is a Java keyword which creates a new object and allocates the memory for the object on heap.

32.package: It is a Java keyword which declares a namespace for a Java class. It has to be the first Java Statement line in any program.

33.private: It is an access modifier which declares a member’s access as private i.e. the member will be visible only within the class and not from any other class(sub-classes).

accessibility : Only within class

Note: As access modifiers are not handled at instance (object)levels but are handled at class levels, private members can be visible from other instances(objects) of the same class.

34.protected: It is also an access modifier which is used for a method or a class member to mention that the variable or method can only be accessed by the elements residing in its own class in the same package and also from the sub-classes of it in either the same package or a different package.

accessibility : within class, sub-classes and packages

35.public: It is also an access modifier which helps declare a member’s access publicly. i.e. visible to all classes.

accessibility : within class, sub-classes ,packages and outside packages

Note: A best practice is always to give the fields a private access and reserve the public access to the methods thereby giving all the public constants a final access modifier. This enables encapsulation and information hiding; as it allows us to change the implementation of the class without affecting the customer using the public APIs.

36.return: It is a Java keyword which returns the Object’s reference and not the Object’s value.

37.short: It is a Java keyword designated to a 16 bit signed integer primitive type.

38.static: The prime function of the static keyword is memory management. It is a non-access modifier which can be used for any method, a variables, blocks or an inner class[nested classes]. The main() method of every class is generally labelled as static. When any member of a class is declared as static, it can be accessed before the object of the class is created as it doesn’t require any object reference to call it.

Note: If a block is made static it gets executed only once and is initialized when the class is firstly loaded. If a variable is declared as static, it holds a single copy of itself among’st all the objects of that class. They can be created at a class-level only. If a method is declared as static,it can access the static data directly and can call the other static methods only. If a class is declared as static, it has to be only a nested class[a class within a class].

39.strictfp: It is a Java Keyword which is used to restrict the floating point calculations there by ensuring same result on every platform [be it any of the 16-bit/32-bit/64-bit platform]while performing operations on floating -point variables.

Note: It is to be used with classes, interfaces and methods only. If a class or an interface is declared strictfp, then all the underlying method of the class or interface are implicitly made strictfp. It cannot be used with an abstract method,but can be used along an abstract class or interfaces. As methods of an interface are abstract in nature implicitly, hence strictfp cannot be used for any methods inside of an interface.

40.super: It is a Java keyword used inside a sub-class[child class] method definition to call any methods of its parent[super] class. If any method is declared as private in parent class, then its child class cannot access it using super keyword. It also is been used in constructors to call the parent[super] constructor. It cannot be used along an static method.

41.switch: It is a Java Keyword used for a branching statement, based on a numeric value which can be either of a char type, a byte type, a short type or int type.[mentioned in eg. below]

switch(3){

case 1:

System.out.println(“number is 1”);

break;

case 2:

System.out.println(“number is 2”);

break;

case 3:

System.out.println(“number is 3”);

break;

case 4:

System.out.println(“number is 4”);

break;

default:

System.out.println(“number is neither 1,2,3,4”);
}

when the integer value mentioned in switch() matches one of the mentioned case labels, then the statements of that specific case is executed, including the following labels’ statement ,until the end of switch block or until a break keyword is reached.

42.synchronized: A scenario of multi threading occurs where multiple threads try to access a same resource there by producing an erroneous output. So Java has introduced a concept of synchronization where we can acquire a lock on the resource that can be used by multiple threads. By either creating a synchronized block or by marking a method as synchronized.

Note: The synchronization is always associated to an object. If the method is static the associated object here is the class, i.e. a static synchronized method will lock the class instead of an object.

43.Singleton: It is a keyword used for a class. If a class is declared as singleton, only one instance[object] will be created of that class at a time[we will discuss in detail on this in the upcoming articles].

44.this: It is a Java keyword which contains the current object reference. Basically used to pass the current object as a parameter to any method.

45.throw: It is a Java keyword which is a part of a try- catch block. It throws an exception[we will discuss in detail on this in the upcoming articles].

46.throws: It is a Java keyword which is used in the method definition to declare the exception to be thrown by a method.

47.transient: It is a Java keyword which marks the variables not to be serialized. When an object is transferred through the network, the object gets serialized[ converts its state from object to streams of byte], and then are transmitted across the network. The member variables marked as transient don’t get transferred across the network and are intentionally made lost [we will discuss in detail on this in the upcoming articles].

48.try: It is a Java keyword which initiates the try block. If an exception is occurred in this block it gets compared to the exceptions present in the catch block.

49.void: It is a Java keyword used at a method declaration and definition to assure the method does not return any type.

50.volatile: It is a Java keyword used to mark a member variable as volatile i.e. being stored in a memory and not in cache. It is yet another way to make a class thread-safe. i.e. the keyword volatile tells the compiler that the value of the variable must never be cached as its value might change outside the scope of the program[we will discuss in detail on this in the upcoming articles].

51.while: It is a Java keyword used in starting a looping block of code. It is used in while loop and do-while loop conditions.

Hope this article was worth your time investing reading it!!!

That is it on this article from my end, for any further detailed information needed on any of the keywords , kindly comment in the sections and I shall be privileged answering them.

Until then….

Peace Out!

Rupam Pawan Jha

--

--

Rupam Jha
JavaMadeTranquil

Senior DevOps Engineer. Automation Enthusiast. Sharing my experiences on AWS, CI/CD, Docker, Kubernetes, and IaC.