print
Advertisment
Advertisment

reserved words in java

In computer programming, a keyword is a reserved word that has a predefined and special meaning within a programming language. Keywords are an integral part of the language's syntax and semantics, and they are used to define the structure and behavior of programs. These words cannot be used as identifiers for variables, functions, classes, or other user-defined elements because they are reserved for specific language features. Keywords are typically used to declare control structures (e.g., if, while, for), data types (e.g., int, boolean, double), access modifiers (e.g., public, private, protected), and other fundamental elements of a programming language. For example, in Java, the keyword public is used to declare a member as publicly accessible, while int is used to define a variable as an integer data type. Attempting to use these words as variable names would result in a compilation error because they are reserved for their respective language constructs. In summary, keywords are predefined words with specific meanings in a programming language, and they play a crucial role in defining the syntax and behavior of programs written in that language.

Reserved words in java for data types: (8)

1) byte

2) short

3) int

4) long

5) float

6) double

7) char

8) boolean

EXAMPLES OF DATA TYPES

ReservedWordsInDataType.java
public class ReservedWordsInDataType {
	public static void main(String[] args) {
        byte variableForByte = 1;
        short variableForShort = 2566;
        int variableForInt = 123;
        float variableForFloat = 15.12f;
        long variableForLong = 7865432l;
        double variableForDouble = 678.28739437d;
        char variableForChar = 1;
        boolean variableForBoolean = true;
        System.out.println(((Object) variableForByte).getClass().getSimpleName());
        System.out.println(((Object) variableForShort).getClass().getSimpleName());
        System.out.println(((Object) variableForInt).getClass().getSimpleName());
        System.out.println(((Object) variableForFloat).getClass().getSimpleName());
        System.out.println(((Object) variableForLong).getClass().getSimpleName());
        System.out.println(((Object) variableForDouble).getClass().getSimpleName());
        System.out.println(((Object) variableForChar).getClass().getSimpleName());
        System.out.println(((Object) variableForBoolean).getClass().getSimpleName());
    }
}

Output :


Byte
Short
Integer
Float
Long
Double
Character
Boolean

Process finished with exit code 0

Reserved words in java for flow control:(11)

1) if

2) else

3) switch

4) case

5) default

6) for

7) do

8) while

9) break

10) continue

11) return

EXAMPLES OF RESERVED WORDS FOR FLOW CONTROL STATEMENT IN JAVA

ReservedWordsForFlowControl.java
public class ReservedWordsForFlowControl {
    public static void main(String[] args) {
        int i = 15;
        int r = 20;
        if (i>r) System.out.println("i is greater than r");
        else System.out.println("i is less than r");
    }
}

Output :


i is less than r

Process finished with exit code 0


ReservedWordsForSwitchCase.java
public class ReservedWordsForSwitchCase {
    public static void main(String[] args) {
        int day = 4;
        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            case 6:
                System.out.println("Saturday");
                break;
            case 7:
                System.out.println("Sunday");
                break;
            default:
                System.out.println("Please select a valid number from 1 to 7");
        }
    }
}

Output :


Thursday

Process finished with exit code 0


ReservedWordForContinue.java
public class ReservedWordForContinue {
    public static void main(String[] args) {
        for (int z = 0; z < 7; z++) {
            if (z == 5) {
                continue;
            }
            System.out.println(z);
        }

    }
}


Output :


0
1
2
3
4
6

Process finished with exit code 0

ReservedWordForDoWhile.java
public class ReservedWordForDoWhile {
     public static void main(String[] args) {
         int i = 14;
         do {
             System.out.println(i);
         }while (i>=15);
         System.out.println(i);
         i++;
     }
}

Output :


14
14

Process finished with exit code 0

ReservedWordForReturn.java
public  class ReservedWordForReturn {
     static int myMethod(int x){
         return 10+x;
     }

     public static void main(String[] args) {
         System.out.println(myMethod(10));
     }
}


Output :


20

Process finished with exit code 0


Reserved words for modifiers:(11)

1) public

2) private

3) protected

4) static

5) final

6) abstract

7) synchronized

8) native

9) strictfp(1.2 version)

10) transient

11) volatile

EXAMPLES OF RESERVED WORDS FOR MODIFIERS

ReservedWordsForPublic.java
public class ReservedWordsForPublic{
    public static void main(String[] args) {
        int variableForInt = 123;
        System.out.println(variableForInt);
    }
}

Output :


123

Process finished with exit code 0

ReservedWordsForPrivate.java
public class ReservedWordsForPrivate{
    private int data=40;
}
//private data type can not be accessed directly you will learn about it more in further topics

Output :

javac ReservedWordsForPrivate.java
ReservedWordsForPrivate.java:4: error: non-static variable age cannot be referenced from a static context
System.out.println(age);
^
1 error     
ReservedWordsForProtected.java
public class MainClass{
    protected int i = 20;
    }

//protected data type can not be accessed directly you will learn about it more in further topics
ReservedWordsForFinal.java
public class ReservedWordsForFinal {
    final int i = 15;

    public static void main(String[] args) {
        ReservedWordsForFinal finalVar = new ReservedWordsForFinal();
        finalVar.i = 25;
        System.out.println(finalVar.i);
    }
}

// will generate an error: cannot assign a value to a final variable

ExampleForAbstract.java
abstract class ExampleForAbstract {
    public String name = "smith";
    public int id = 5;
    public abstract void study(); // abstract method
}
/*
abstract method do not have body its body is provided in subclass to know more keep learning b2eprogrammers.com
* */

Reserved words for exception handling:(6)


1) try
2) catch
3) finally
4) throw
5) throws
6) assert(1.4 version)

Advertisment

Class related Reserved words in java:(6)


1) class
2) package
3) import
4) extends
5) implements
6) interface		

Object related reserved words in java:(4)


1) new
2) instanceof
3) super
4) this		

If a method won't return anything compulsory that method should be declared with the void return type in java but it is optional in C++.


1) void

Reserved literals in java:


1) true values for boolean data type.
2) false
3) null----------------- default value for object reference.

Advertisment

Conclusions :


1. All reserved words in java contain only lowercase alphabet symbols.
2. New keywords in java are:
3. strictfp-----------1.2v
4. assert-------------1.4v
5. enum--------------1.5v
6. In java we have only new keyword but not delete because destruction of useless
objects is the responsibility of Garbage Collection.
7. instanceof but not instanceOf
8. strictfp but not strictFp
9. const but not Constant
10. syncronized but not syncronize
11. extends but not extend
12. implements but not implement
13. import but not imports
14. int but not Int

Which of the following list contains only java reserved words ?

	
1. final, finally, finalize (invalid) //here finalize is a method in Object class.
2. throw, throws, thrown(invalid) //thrown is not available in java
3. break, continue, return, exit(invalid) //exit is not reserved keyword
4. goto, constant(invalid) //here constant is not reserved keyword
5. byte, short, Integer, long(invalid) //here Integer is a wrapper class
6. extends, implements, imports(invalid) //imports keyword is not available in java	
7. finalize, synchronized(invalid) //finalize is a method in Object class
8. instanceof, sizeOf(invalid) //sizeOf is not reserved keyword
9. new, delete(invalid) //delete is not a keyword
10. None of the above(valid)	

Which of the following are valid java keywords?

	
1. public(valid)
2. static(valid)
3. void(valid)
4. main(invalid)
5. String(invalid)
6. args(invalid)
	

Java Keywords, Reserved Words, Java Programming, Java Syntax

Advertisment
Advertisment
arrow_upward