Photo by Dimitry Anikin on Unsplash

IMPORTANT CONCEPTS IN JAVA SERIES

Java Concepts — All 50 Keywords Examples

Explanations, Examples, Codes and Diagrams

Published in
6 min readApr 4, 2022

--

This series on Must Know Java Concepts will introduce all the important Java Concepts.

  1. Java Features & JDK
  2. Fundamentals & OOPS
  3. Generics
  4. Collections
  5. Exception Handling
  6. Files & Input-Output
  7. Serialization
  8. Multithreading
  9. Synchronization
  10. Networking
  11. All 50 Java Keywords ← we are here
Image created by Suryakant Bharti

All 50 Java Keywords with Examples

Below are 48 Keywords in Java (excluding the keywords goto and const as they are not used).

1–8) boolean, char, byte, short, int, long, float, double

These 8 keywords deal with primitive data types and its variables.

The boolean keyword is used to define variables of the boolean type which can hold true or false.

The byte keyword is used to define variables of the byte type which can hold a numeric value from -128 (-27) to 127 (27–1).

The short keyword is used to define variables of the short type which can hold a numeric value from -215 to 215–1.

The int keyword is used to define variables of the integer type which can hold numeric value from -231 to 231–1.

The long keyword is used to define variables of the long type which can hold numeric value from -263 to 263–1.

The float keyword is used to define variables of the float type which can hold fractional numbers with decimal.

The double keyword is used to define variables of the double type which can hold fractional numbers with decimal, double represents double precision decimal number with much larger range of numbers than float.

  • char keyword is used to define variables of the char type which can hold a single character (16-bit integer representing a Unicode-encoded character).

9) enum

The enum keyword is used to define enum types (a data type that allows a variable to be a set of predefined constants).

10–11) if, else

Both if and else keywords are used for condition-based code execution, if statement can be used independently or with else block, we can also create nested if-else or if-else ladders.

12–13) switch, case

Both switch and case keywords are used in the switch-case statement where a code is run based on the condition matched by a case. It is a cleaner version of if-else ladder and provides better performance (particularly, when number of cases are more).

14–16) for, while, do

The for keyword is used to execute the set of code inside a loop until a condition is true.

The while keyword is used to repeat a set of code in a loop until a condition is met.

The do keyword is used in a do–while loop, these loops are used to execute one or more statements repetitively until a condition returns false.

17–18) break, continue

The break keyword is used to stop the execution of a loop (for, while, switch-case) based on some condition.

The continue keyword is used to stop the execution of current iteration and start the execution of next iteration in a loop.

19–23) try, catch, finally, throw, throws

These 5 keywords are used for exception handling.

The code to be observed for exceptions are kept inside the try block. The exceptions thrown by the try block are caught in the catch block. The finally block is always executed irrespective of whether exception is thrown or not.

The throw keyword is used to throw the exceptions manually.

The throws keyword is used to state exceptions that the current method may throw.

24–25) class, interface

The class keyword is used to define a class. In any object-oriented programming language, class is the most fundamental building block.

The interface keyword is used to define the interfaces which is an abstraction mechanism. Interfaces can have only abstract methods (without method body).

26–27) extends, implements

The extends keyword is used when a class inherits another class (with its properties and behaviors).

The implements keyword is used when a class is implementing an interface.

28–29) new, instanceof

The new keyword is used while creating new instances of a class, i.e., create objects.

The instanceof keyword is used to check if an object is a specified type of class, subclass or interface.

30–31) package, import

The package keyword is used in a file to state that the current file belongs to that particular package.

The import keyword is used when we want to use code (classes & methods) from other packages. We can import a particular package and use it in our current code.

32–33) return, void

The return keyword is used to return the control from the method to the caller.

The void keyword is used to indicate that the method does not return an

34–35) this, super

The this keyword is used to access members of the class from where it is being called.

The super keyword is used to access members of the super class (a class inherited by this class) from within a subclass.

36–39) private, protected, public, default

These 4 keywords are access modifiers used to in Java.

The private members are visible only within that class; only inner or nested classes can be private.

The protected members are visible only within that package, or can be inherited to a sub classes.

The public members are visible from anywhere and can be inherited by any sub class.

The default access applies to a class when we don’t define any access-modifier for a class, it is also used to define the default methods in an interface and in the switch-case statements to define default case.

40–47) final, static, abstract, transient, volatile, synchronized, native, strictfp

These 4 keywords are non-access modifiers used to in Java.

The final keyword makes the class, method, or field unchangeable — a final class cannot be extended, a final method cannot be overridden and the value of a final field cannot be changed.

The static keyword is used to define the members of a class which have same value for all instances of that class, these members can be accessed directly through the class name without the need to instantiate a class.

The abstract keyword is used to apply abstraction in java. Any method which does not have a method definition must be declared as abstract and the class which contains it also must be declared as abstract — abstract classes cannot be instantiated, abstract method must be implemented in the sub classes, variables and constructors cannot be abstract.

The transient keyword is used for serialization. A variable which is declared as transient will not be eligible for serialization.

The volatile keyword is used in programs that use concurrency. The value of a variable that is stated as volatile will be written in or read from the main memory.

The synchronized keyword is used to for thread synchronization. If a method or a block is declared as synchronized, only one thread can enter it at a time. Any thread which wants to enter a synchronized method or block must acquire the object lock first.

The native keyword is used to specify that a particular method is implemented in native code with Java Native Interfaces.

The strictfp keyword is used for strict precision of floating-point calculations even when platform varies — strictfp can be used with classes, interfaces and methods.

48) assert

The assert keyword is used in the assertion statements. You can use these statements to test your assumptions about a program. Assertion is the best way to detect and correct the programming errors. Assertion statements take one boolean as input and assumes that it will be always true; if false, AssertionError is thrown

49–50) goto, const

The goto and const are reserved keywords but are not used in Java currently.

Note: true, false and null are not the keywords. They are literals in java.

Congrats! 🎉 We are one step closer to “becoming a Java Expert” now!!

Image created by Suryakant Bharti

This article is supposed to serve as a beginner’s guide for All Keywords.

Give a clap if you liked the article or a leave a comment for any suggestion/discussion/dispute. Thank you!

--

--

Android Developer (Kotlin, Java, OOPS, Data Structures, Algorithms, Design Patterns) github.com/Suryakant-Bharti