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.
byte: A data type that can hold 8-bit data values.
Example
public class MyClass {public static void main(String[] args) {byte varibale = 8;System.out.println(varibale);}}
case: It is used in switch statements to mark blocks of code. The case is used to label each branch in a switch 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;}}}
catch: It is used to catch the exception generated by try statements. The catch the statement allows you to define a block of code to be executed if an error occurs in the try block
Example
public class MyClass {public static void main(String[] args) {String str = "Hello";try {System.out.println(str.charAt(10));} catch (IndexOutOfBoundsException e) {// TODO: handle exception} catch (Exception e) {// TODO: handle exception}}}
char: A data type that can hold unsigned 16-bit Unicode characters
Example
public class MyClass {public static void main(String[] args) {char ch = 'a';System.out.println(ch);}}
if: Tests a true/false expression and branches accordingly. In other words, if a statement specifies a block of Java code to be executed if a condition is true.
Example
public class MyClass {public static void main(String[] args) {int i = 2;if (i < 5) {System.out.println("Hello Java");}}}
implements: It specifies that a class implements an interface.
Create an interface
public interface MyClass {void display();}
Example
public class MyClassImp implements MyClass {@Overridepublic void display() {System.out.println("Implementing themethod define in interface");}}
import: References other classes. The import keyword is used to import a package, class, or interface.
Example
import java.util.ArrayList;import java.util.List;public class MyClass {public static void main(String[] args) {List<String> list = new ArrayList<String>();list.add("Hello");System.out.println(list);}}
instanceof: It indicates whether an object is an instance of a specific class or implements an interface. The instanceof keyword compares the instance with type. The return value is either true or false.
Example
public class MyClass {public static void main(String[] args) {MyClass myObj = new MyClass();System.out.println(myObj instanceof MyClass);}}
Related Posts
No comments:
Post a Comment