Java Keywords


This image has an empty alt attribute; its file name is image-9.png

Java has keywords which are the words that act like keys for the code. However, please note those cannot be used as variable or class or object names. Those are the special words that need to start with lowercase letters. We have already learnt some of them, such as; class, void, int, … etc. And you can see the rest of them in the following table.

KeywordDescription
abstractSpecifies that a class or method will be implemented later, in a subclass
assertFor debugging
booleanA data type that can only store true and false values
breakBreaks out of a loop or a switch block
byteA data type that can hold 8-bit data values
caseMarks a block of code in switch statement
catchCatches exceptions generated by try statements
charA data type that is used to store a single character
classDefines a class
continueSends control back outside a loop
defaultSpecifies the default block of code in a switch statement
doStarts a do-while loop
doubleA data type that can hold 64-bit floating-point numbers
elseIndicates alternative branches in an if statement
enumDeclares an enumerated (unchangeable) type and extend the base class
exportsExports a package with a module. New in Java 9
extendsIndicates that a class is derived from another class or interface
finalIndicates that a variable holds a constant value or that a method will not be overridden
finallyIndicates a block of code in a try-catch structure that will always be executed
floatA data type that holds a 32-bit floating-point number
forCreate a for loop
ifMakes a conditional statement
implementsImplements an interface
importUsed to import a package, class or interface
instanceofIndicates whether an object is an instance of a specific class or implements an interface
intA data type that can hold a 32-bit signed integer
interfaceDeclares an interface
longA data type that holds a 64-bit integer
moduleDeclares a module. New in Java 9
nativeSpecifies that a method is implemented with native (platform-specific) code
newCreates new objects
packageDeclares a package
privateAn access modifier used for attributes, methods and constructors, making them only accessible within the declared class
protectedAn access modifier used for attributes, methods and constructors, making them accessible in the same package and subclasses
publicAn access modifier used for classes, attributes, methods and constructors, making them accessible by any other class
requiresSpecifies required libraries inside a module. New in Java 9
returnFinished the execution of a method, and can be used to return a value from a method
shortA data type that can store whole numbers from -32768 to 32767
staticA non-access modifier used for methods and attributes. Static methods/attributes can be accessed without creating an object of a class
strictfpRestrict the precision and rounding of floating point calculations
superRefers to superclass (parent) objects
switchSelects one of many code blocks to be executed
synchronizedA non-access modifier, which specifies that methods can only be accessed by one thread at a time
thisRefers to the current object in a method or constructor
throwCreates a custom error
throwsIndicates what exceptions may be thrown by a method
transientA non-accesss modifier, which specifies that an attribute is not part of an object’s persistent state
tryCreates a try…catch statement
varDeclares a variable. New in Java 10
voidSpecifies that a method should not have a return value
volatileIndicates that an attribute is not cached thread-locally, and is always read from the “main memory”
whileCreates a while loop

Example:

This is an example for finding the smallest and largest numbers from a given set of numbers. In our example we enter three numbers and the program tells us smallest and largest. You will see that we used multiple keywords; int, do, while, if and else.

Leave a comment