Keywords of Java

Oretes
11 min readApr 18, 2020

--

Introduction of Java Keywords

We know that, Keywords are an essential part of a language definition. In java programming language keywords are special tokens which have reserved use out of five java tokens such as Identifiers, Literals, Operators, and Separators. They implement specific features of the language. Keywords in java represent predefined actions, internal processes etc. syntax are used in java to highlighting the display of keywords in different way like color etc. for easy identification. Because java is a predefined and high level language.

Features of Java Keywords

  • Lower case letter are written by all keywords.
  • We can changing one or more letter to upper case by using some words as identifiers, since java is case sensitive.
  • According to syntax, these keywords combined with operators and separators, form definition of the Java language.
  • We cannot use keywords as variables, classes, methods etc.., because keywords are specific meaning in java.
  • Java language has reserved 51 words as keywords, out of these 49 are in use and 2 are not in use.

Examples

int id;

  • Here “int” is a keyword, it indicates the variable “id”.
  • You cannot use keyword like int, for, class act as variable or identifier.
  • Besides these keywords you cannot use true, false and null as an identifier because they are literal.

Categories of Java Keywords

  1. Access modifiers:- private, protected, public
  2. Class method variables modifier: — Abstract, class, extends, final, implements, interface, native, new, static, synchronized, transient, volatile.
  3. Flow control:- Break, case, continue, default, do, else, for, if, instance of, return, switch, while.
  4. Package control:- Import, package.
  5. Primitive types:- Boolean, byte, char, double, float, int, long, short.
  6. Error handling:- Assert, catch, finally, throw, try
  7. Enumeration:- Enum.
  8. Others:- supper, this, void.
  9. Unused:- Const, goto

Declaration of Keywords in Java Program

The keywords are used in static way and final way.

Static Way

Classes, variables, methods and blocks are application of static keyword, the class members , class and blocks can be made static by using static keyword . Using static keyword in front of the name of then class members, class and blocks respectively when a class member as static, it becomes global for all other members of the class.

1. Static Variable

  • Static variable acts like a worldwide variable for all other data members of the class.
  • A static variable can be accessed with class name in which it is defined followed by the DOT (.) operator

Example:- class-name. Static-variable // accessing static variable of the class

2. Static Class

A static nested class cannot access the non-static member of outer class. The static member of the outer class is accessing by static class.

3. Static Block

when the class is loaded used to initialize the static variables of the class, then the static block is used.

Final Way

Final is a keyword applicable to the class variable and methods, the class variable and the method is declared as final using the keyword “final” preceded by their name , once a variable declare as final it cannot be modified further in the program.

What is the use of static and final keyword in Java?

  1. The main difference between a static and final keyword is that static keyword is use to define the class member that can use in independently of any objection in the class
  2. Final keyword is used is used to declare a constant variable, a method which cannot be inherited

Comparison Chart

Description of Java Keywords

We know, the java programing language has 50 or more reserved keywords which have special meaning for the compiler and cannot be used as variable name.

Following is java keywords in alphabetical order

  1. Abstract

The abstract keyword is use to declare a class or a method as abstract. Abstract specifies that a class or methods will be implemented later, in a sub-class

Syntax:-

Abstract class person {

public string fname — “Ranu”;

public int age — 21;

public abstract void study(); // abstract method.

}

2. Assert

Assert is a java keyword is used to define an assert statement is used to declare an expected Boolean condition in a program

Assertion enables developers to test assumptions in their program in compile time as a way to defect and fix bugs, it continue since java 1.4.

Syntax:-

public class Assertion example {

public static void main (string[] args) {

// get a number in the first argument

int number = integer. Parseint (args[0]);

assert number <= 10; //stop

if number>10 system.out.println(“pass”);

}

}

3. Boolean

Boolean keyword is a data type that can only take the value true or false The Boolean keyword is used to declare return type of a method Syntax:-

Class Boolean {

Public static void main(string [] args)

Boolean is java wings = true;

Boolean is fish tasty = false;

{ System.out.println (is java wings);

System.out.println (is fish tasty);

}

}

4. Break

In java break keyword stops execution of for loop, while loop and switch-case construct.When the construct is broken, then the next statement is executed.

Syntax:-

For (int i=5; i<=10; i++) {

System.out.println (“count =” +i);

If (i >10) {

break;

}

}

5. Byte

In java, the byte keyword is used to declare a variable as a numeric type. A byte value can hold an 8-bits integer. The byte keyword declared return type of method.

Syntax:-

Public byte getmonth( ) {

Return 12;

}

6. Case

Each case is tested from top to bottom, until a case is matched and a break statement is found. If a case matches the expression, the statement block after the case clause are executed, until a break statement is reached. The switch-case construct is a flow control structure that tests value of a variable against a list of value.

Syntax:-

Switch (expression) { case constant-1:

// statement 1 break:

case constant- 2:

// statement 2 break:

case constant — 3:

// statement 3:

break:

default:

// if all the case do not match

}

7. Catch

Catches exception generated by try statement Syntax:-

String input =” ”;

// capture input from user…… int number ;

try {

number = Integer. Parseint (input) ; catch (Number format exception ex) { number =1 ;

}

}

8. Char

In java, the char keyword is used to declare a variable as a character type. A char variable represents a single character. Syntax:-

Public char get a letter ( ) {

Return ‘A’;

}

9. Class

The class keyword is used to declare a class, in java programing. Class is a fundamental structure in object oriented programing Syntax:-

Public class MyClass {

Int x = 10;

Public static void main (string [ ] args) {

MyClass my car = new my car( );

System.out.println( my car.x);

}

}

10. Const

In java const is a reserved keyword are not being used

11. Continue

In java, the continue keyword is use to stop execution of a current repetition in a for loop or a while loop, then advance to the next repetition.

Syntax:-

For/while (expression) {

// statement 1 If (condition) { Continue;

}

}

12. Default

Basically, there are three places you can use the default keyword in java

a) Specially the default value in switch case statement.

b) Declare default values in a java annotation.

c) Declare default method in an interface.

Syntax:-

Public static void main(string [ ] args) { Public static int get days of month (int month) {

Switch (month) { Case 2:

return 28;

case 4:

case 6:

case 9:

case 11:

return 30;

default: return 31;

}

}

}

13. Do

The do-while construct is a loop structure which iteratively executes one or some statements until a condition becomes false. In other words, it repeats the statements while the condition is true.

Syntax:-

do {

// statements

} while (expression);

14. Double

In java, the double keyword is used to declare a variable as a numeric type . A double value can hold a 64-bit floating number.

Syntax:-

Public double get balance ( ) {

Return balance amount:

}

15. Else

In java, the if-else construct is used to check if a condition is satisfy then do something, otherwise do something else.

Syntax:-

If (condition) {

// do something if condition is met

} else {

// do something else if condition is not satisfy

}

16. Enum

In java, the enum keyword is used to declare a new enumeration type. This keyword has been introduced since java 5.

Syntax:-

class week {

enum day of week (mon, tue, wed, thu, fri, sat, sun,) ;

}

17. Extend

In java, when class declaring a class as a subclass of another class, the extends

keyword is used

Syntax:-

Class a { int value ;

void do any work( ) {

}

}

class b extends a {

}

18. Final

In java, the final keyword can be applied to declaration of classes, methods and variables.

Class c {

Final void number ( );

}

19. Finally

It indicates a block of code in a try catch structure that will always be executed Syntax:-

class test Finally Block {

public static void main (string args [ ]) { try{

int a = 30/5;

system.out.println (a);

}

Catch ( null pointer exception e){

system.out.println(e);

}

Finally {system.out.println(“finally block is always executed”);} System.out.println(“rest of the code…..”);

}

}

20. Float

A data type that holds a 32-bit floating point number Syntax:-

Public class java float example { float f1= new float (22);

float f2=new float(f1/2);

system.out.println (f2+”value for isinfinite() method is :”+ float.isinfinite(f2));

}

21. For

It is used to start a for loop. Syntax:-

Public class for example {

Public static void main(string[ ] args) {

For (int i=1; i<=10; i++) {

System.out.println(i);

}

}

}

22. If

it tests a true/false expression and branches accordingly Syntax:-

Public class if demo {

Public static void main(string args[ ]) { Int i=100;

If (i<15)

System.out.println(“I is smaller than 15”); Else

System.out.println(“i is greater than 15”) ;

}

}

23. Implements

It specifies that implement class in an interface . Syntax:-

Interface vehicle { Void start ( ); Void stop ( );

}

class car implements vehicle { void start ( ) {

// starts the engine

}

Void stop ( ) {

// stop the engine

}

}

24. Import

It references other classes

Syntax:-

import java. awt .*;

import java. utill. List;

import java.io.file;

25. Instanceof

It indicates whether an object is an instance of a specific class or implements an interface

Syntax:-

object msg = new string (“hello”) ;

if ( msg instanceof string ) {

system.out.println( “a string”) ;

}

26. Int

A data type that can hold 32-bit signed integer Syntax:-

Public int getchildcount () {

Return 12;

}

27. Interface

Declares an interface Syntax:-

Interface vehicle {

Void start ( );

Void stop ( );

}

28. Long

It can hold a 64-bit integer and it is one type of data type. Syntax:-

Public long get amount ( ) {

return amount ;

}

29. Native

Specifies that a method is implemented with native code Syntax:-

Public class native example {

Public native void fast copy file, (string source file, string dest file) ;

}

30. New

Creates new objects.

Syntax:-

Class car {

}

Car new car =new car ( );

31. Null

Now it is not use.

32. Package

An access specifier indicating that a method or variable may be accessed only in the class it’s declared in

Syntax:-

Package com. My phone.code;

Public class my class {

}

33. Private

An access specifier indicating that a method or variable may be accessed only in the class it’s declared in privately, it means it dose not run any where.

Syntax:-

Class A {

Private void foo ( ) { } Void bar ( ) {

Foo( );

}

}

Class B { Void woo ( ) {

A a = new A ( );

a. Foo ( ) ; // oh no! since foo is private

}

}

34. Protected

An access specifier indicating that a method or variable may only be accessed in the class it’s declare in ( or a subclass of the class it’s declared in or other classes in the same package) .

Syntax:-

package p1 ;

public class person {

protected string name ;

}

35. Public

An access specifier used for classes, interfaces, methods, and variables indicating that an item is accessible throughout the application (or where the class that defines it is accessible).

Syntax:-

Public class person { Public string name ;

Public void drink ( ) {

}

}

36. Return

Send control and possibly a return value back from a called method. Syntax:-

String get name ( ) { return “peter” ;

int get age () { int my age =23; return my age;

}

}

37. Short

It can hold a 16-bit integer and it is a data type. Syntax:-

Public short get length () {

Return 2213 ;

}

38. Static

Indicates that a variable or method is class method (rather than being limited to one particular object)

Syntax:-

Public class car { Static string colour;

Static void deep () {

}

Void break () {

}

}

39. Super

Refers to a class base class. Syntax:-

Public class super { Protected int number ; Protected show number () {

System .out.println( “ number =”+number) ;

}

}

40. Switch

Switch statement that executes code based on a test value. Syntax:-

Switch (expression) {

Case constant_ 1:

// statement 1 Break:

Case constant _2:

// statement 2 Break:

Default:

// if all the case not match

}

41. Synchronized

Multithread code is specify by critical section or methods Syntax:-

Synchronized (expression) {

// synchronized code block

}

42. This

The current object is refer by a method or constructor. Syntax:-

Class manager {

Employees [ ] employees;

Void manage employees () {

Int total emp =this.employees.length ;

System.ou.println(“total employees:” +total employee);

This.report();

}

Void report () {}

}

43. Throw

Creates an exception. Syntax:-

Void a method () throws exception 1, exception 2 {

// statements …..

If (an exception occur) { Throw new exception() ;

}

// statement…..

If ( another exception occurs) { throw new exception 2 ();

}

}

44. Throws

It indicates what exception may be thrown by a method.

45. Transient

Specifies that a variable not part of an objects persistent state. Syntax:-

class rectangle {

int width;

int height ;

transient long area ;

}

46. Try

Starts a block of code that will be tested for exceptions. Syntax:-

try {

// code that may throws errors/ exceptions

} catch (exception object is being throw) {

// code to handel exception

} finally {

// code always executed

}

47. Void

Specifies that a method does not have a return value. Syntax:-

Public void do something () {

// do something

// no return

}

48. Volatile

Indicates that a variable may change asynchronously.

Syntax:-

class volatile example {

volatile int x;

}

49. While

Starts a while loop.

Syntax:-

While (condition is true) {

// statement

}

NOTE:-

  1. Const and go to are reserved words but not used.

2. True, false and null are literals, not keywords.

3. Since java 8, the default keyword is also used to declare default method in interfaces.

4. Since java 10, the word var is used to declare local variables. For backward compatibility, you can still use var as variable names. So var is a reserve word not keyword.

5. Java 14 adds two new keywords record & yield (but in preview mode)

Written By: Bijayalaxmi Mohanty

Get Oretes Academy Certified in Trending Technologies: http://oretesacademy.com/

If you have any query drop a email at helpdesk@oretes.com

--

--