Keywords or reserved words are those which are basically used for internal processes. We cannot create a variable name as reserved keywords.
Below are some of the reserved keywords in java.
protected: An access specifier indicating that a method or variable may only be accessed in the class it’s declared in (or a subclass of the class it’s declared in or other classes in the same package)
Example
public class MyClass {protected int i = 6;public static void main(String[] args) {MyClass myObj = new MyClass();System.out.println(myObj.i);}}
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)
Example
public class MyClass {public int i = 6;public static void main(String[] args) {MyClass myObj = new MyClass();System.out.println(myObj.i);}}
return: Sends control and possibly a return value back from a called method. In other words, come out of the method by returning some value or -1.
Example
public class MyClass {public static void main(String[] args) {MyClass myObj = new MyClass();System.out.println(myObj.sum(10, 20));}private int sum(int a, int b) {return a + b;}}
short: A data type that can hold a 16-bit integer. The short keyword is a data type that can store whole numbers from -32768 to 32767.
Example
public class MyClass {public static void main(String[] args) {short value = 10;System.out.println(value);}}
static: Indicates that a variable or method is a class method (rather than being limited to one particular object). Static methods/attributes can be accessed without creating an object of a class.
Example:
The main method is the static method.
public class MyClass {public static void main(String[] args) {short value = 10;System.out.println(value);}}
strictfp: Java strictfp keyword ensures that you will get the same result on every platform if you perform operations in the floating-point variable.
Example
strictfp public class MyClass {public static void main(String[] args) {float a = 1009.1991f, b = 19019.1991f;float answer = a + b;System.out.println(answer);}}
super: It refers to a class’s base class or parent class. It is used to call superclass methods, and to access the superclass constructor.
Example
class Parent {public void animalSound() {System.out.println("Parent is called");}}class Child extends Parent {public void animalSound() {super.animalSound(); // call the parentSystem.out.println("Child is called");}}public class MyClass {public static void main(String args[]) {Parent myDog = new Child();myDog.animalSound();}}
switch: A statement that executes code based on a test value. In other words, it executes one statement from multiple ones. So we can say it is like an if-else-if ladder statement.
Example
public class MyClass {public static void main(String[] args) {String day = "Monday";switch (day) {case "Monday":System.out.println("Today is Monday");break;case "Tuesday":System.out.println("Today is Tuesday");break;case "Wednesday":System.out.println("Today is Wednesday");break;case "Thursday":System.out.println("Today is Thursday");break;case "Friday":System.out.println("Today is Friday");break;case "Saturday":System.out.println("Today is Saturday");break;case "Sunday":System.out.println("Today is Sunday");break;default:System.out.println("Not a day of week");break;}}}
Related Posts
No comments:
Post a Comment