Every programming language will have a huge list of keywords so that the respective compiler or interpreter can understand to perform the actions accordingly, similarly we have 50 keywords in Java and it is very important that you should know to write programs in java.
Most of you fear its a huge list, but by just giving a quick read it will make your life easier to start writing programs in java, you need not know exact purpose and meaning of all 50 java keywords at once, you will learn them as you practise writing more and more programs in java.
These keywords can be categorised on different usage purposes and the list goes below:
- Data Types
- Access modifiers
- Non Access modifiers
- Control structures / Flow structures related
- Error Handling
- Object Oriented Programming related
- Others / Miscellaneous
- Un Used
50 Keywords in Java are explained below for each category with purpose
8 Keywords in Data Types category
Data Types are the different types of data that java can hold and can be represented in memory, each primitive data type has its own capacity of data that it can hold.
Below list of keywords are used to either declare a variable for a specific type supported in java or as a return type to methods in java.
|1||int||Primitive||A data type which can hold ( 32 bit / 4 bytes ) numeric values, i.e. both negative and positive numbers. No decimals are supported.
ex: int i = 10;
|2||boolean||Primitive||A data type which can hold only 2 values, i.e. true and false, can be used directly in control structures like if, while, for, etc..
ex: boolean flag = true;
|3||long||Primitive||A data type which can hold ( 64 bit / 8 bytes ) numeric values, i.e. both negative and positive numbers. No decimals are supported.
ex: long value = 10;
|4||double||Primitive||A data type which can hold ( 64 bit / 8 bytes ) numeric values with double precision (decimals), i.e. both negative and positive numbers with decimal points.|
|5||char||Primitive||A data type which can hold character supported by java character set in 1 byte of memory.
ex: char alpha = ‘a’;
|6||byte||Primitive||A datatype which can hold any data within 1 byte of memory.|
|7||short||Primitive||A data type which can hold ( 16 bit / 2 bytes ) numeric values, i.e. both negative and positive numbers. No decimals are supported.
ex short value = 1;
|8||float||Primitive||A data type which can hold ( 32 bit / 4 bytes ) numeric values with single precision (decimals), i.e. both negative and positive numbers with decimal points.
ex: float value = 10.15;
3 Keywords in Access modifiers category
Access modifiers are the ones which regulate the access of class members in different locations of programming, like following:
- Accessibility in same class
- Accessibility in sub classes
- Accessibility in classes in same package
- Accessibility in classes in other packages
Source & Credits: JIRA
Following are the list of keywords you can use to declare a specific class member (i.e. a method or a field or an inner class) and regulate the accessibility of them.
|9||private||Class members declared with private keyword in a class can be accessible only by the members of the same class.|
|10||public||Class members declared with public keyword in a class can be accessed by the members of the same class or a sub class or any class in same package or even in a class of a different package.|
|11||protected||Class members declared with protected keyword in a class can be accessed by the members of the same class or a sub class or any class present in same package.|
6 Keywords in Non Access modifiers category
These are the list of keywords which regulate the behaviour of class members when declared with them.
|12||static||static keyword is used when you want a property(variable/attribute), method to be accessible even without creating an object for a class. The non static property gets a separate memory allocated whenever a new object is created, but for static it has all the time. that is it can be accessed even when object is not created.
|13||final||“final” keyword in java is used for multiple purposes, especially if you want to have only one time assignment we can use final keyword. For example, if you want to assign a value to a variable only once like a constant we can use final keyword.
final int acLevel = 4;
|14||volatile||volatile keyword is not much used and need not required to learn deep about it, when you are trying to learn java for test automation only.
But still let us understand a brief meaning about volatile: When a variable/attribute is declared with this attribute, the values are stored directly in main memory and it never gets cached especially during the read / write actions.
|15||transient||This is not much required to learn, when you are trying to learn java for test automation only. you can search in google if you are still interested|
|16||abstract||Abstract keyword is used for a class where you want to just create a template for your subclass, and we cannot create an object for an abstract class. When we declare an abstract class, it is mandatory that at least one method in class is abstract, that is it will not have a definition but just a declaration of a method.
We may not use much of this concept in test automation, you can explore more in google if you want to learn deep about it.
|17||synchronized||Synchronized is generally used when we want to work with threads and is an advanced concept. We may not use much of this concept in test automation, you can explore more in google if you want to learn deep about it.|
11 Keywords in Control / Flow structure category
These are the keywords which will determine the code flow, i.e. if you want to repeat some specific set of statements or if you want certain set of statements to be executed or skipped based on conditions of the business logic that is written in the program.
|23||break||Jumping statement – generally used with loops in conjunction with conditional constructs|
|24||continue||Jumping statement – generally used with loops in conjunction with conditional constructs|
|25||return||Jumping statement – generally used with conditional constructs & functions|
|27||default||Jumping statement – generally used with conditional construct Switch|
|28||case||Jumping statement – generally used with conditional construct Switch|
In order to dwell more in to each of the above conditional & looping constructs I would recommend you to read the article – “All about Conditional and looping constructs in Java”
6 Keywords in exception handling category
|29||try||try keyword is used when you want to handle exception handling, i.e. at any time if you are suspicious that certain set of commands can raise an execution at run time.
We can include those statements in side try block, so that when ever a run exception is raised, instead of stopping the execution it will jump the control to corresponding catch block.
Single try block can have single or multiple catch blocks, i.e. we can catch different kinds of exceptions by specifying them individually and handle them separately as per your needs. Exceptions like FileNotFoundException, ClassNotFoundException, IOException, etc..
|30||catch||You might have understood by now the purpose of catch keyword in above example.|
7 Keywords in Object Oriented Programming category
|35||class||This keyword is used to declare a class
|36||implements||This keyword is used if you want to implement an interface, i.e. if a sub class wants to implement parent class( i.e. interface), This is mainly used to achieve inheritance concept of Object oriented programming|
|37||extends||This keyword is used if you want to extend a class or an interface. This is mainly used to achieve inheritance concept of Object oriented programming|
|38||interface||A class in which you just don’t want to have any method to be defined but just be declared.|
|39||new||new keyword in java is used to create an object for a class.
7 Keywords in Miscellaneous category
|42||strictfp||we may not use this much in Test Automation, but basically it is used to declare a method or class a to restrict all its attributes to have non decimal values.|
|43||import||this keyword is used especially when you want to include some libraries or jar files.
Java has a huge set of packages already developed for developers, if you want to use those classes from those packages, we can include those packages in the program and then can use the respective classes in the Java program.
2 Keywords in Un Used category
Below are the 2 keywords in Java which are never used, but we should be aware if these keywords. As we know that we cannot declare any variable with keywords, so we need to know that we cannot create variables with below 2 keywords as well.
Founder of TestingTools.co, constantly shares knowledge on different test automation tools. Has experience in building proof of concepts, solutions, frameworks, platforms & implementation of test automation projects.
In pursuit of building a platform with inbuilt framework and reusable components for Oracle Cloud applications ( Oracle HCM, CX, SCM, Financials clouds, Salesforce and other cloud applications )
Major accomplishments in his career:
Product architect and development manager for test automation platforms
Oracle Flow Builder @ Oracle
CloudTestr @ Suneratech
2 times to oracle open world
More than 20 successful POCs ( Proof of concepts )
Designed pricing models.
Trained more than 50 members.
Built more than 100 re-usable functions.
Worked with tools: