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.
synchronized: Specifies critical sections or methods in multithreaded code. The process of allowing only a single thread to access the shared data or resource at a particular point of time is known as Synchronization.
Example
public class MyClass {private int count;public static void main(String[] args) {MyClass myObj = new MyClass();myObj.setCount(10);myObj.increment(1);System.out.println(myObj.getCount());}public synchronized void increment(int value) {this.setCount(this.getCount() + value);}public int getCount() {return count;}public void setCount(int count) {this.count = count;}}
this: Refers to the current object in a method or constructor. The most common use of this keyword is to eliminate the confusion between class attributes and parameters with the same name
Example
public class MyClass {private String name;public static void main(String[] args) {MyClass myObj = new MyClass();myObj.setName("Ram");System.out.println(myObj.getName());}public String getName() {return name;}public void setName(String name) {this.name = name;}}
throw: Creates an exception. The throw keyword is used to create a custom error.
Example
public class MyClass {public static void main(String[] args) {int age = 16;if (age < 18) {throw new ArithmeticException("Access denied");} else {System.out.println("Access granted");}}}
throws: Indicate what exceptions may be thrown by a method. It also handles multiple exceptions.
transient: Specifies that a variable is not part of an object’s persistent state. In other words, we can make a variable transient if we don't want to save its value in the file.
Example
import java.io.Serializable;public class MyClass implements Serializable {/****/private static final long serialVersionUID = 1L;transient int age = 16;public static void main(String[] args) {MyClass myObj = new MyClass();System.out.println(myObj.age);}}
try: Starts a block of code that will be tested for exceptions.
Example
public class MyClass {public static void main(String[] args) {try {int[] myNumbers = { 1, 12, 6, 8 };System.out.println(myNumbers[5]);} catch (Exception e) {System.out.println("Exception occured");}}}
void: Specifies that a method does not have a return value.
Example
public class MyClass {public static void main(String[] args) {MyClass myObj = new MyClass();myObj.printHello();}private void printHello() {System.out.println("Hello");}}
volatile: This indicates that a variable may change asynchronously
Example
public class MyClass {static volatile int var = 5;public static void main(String[] args) {System.out.println(var);}}
while: Starts a while loop. The while loop loops through a block of code as long as a specified condition is true:
Example
public class MyClass {public static void main(String[] args) {int i = 1;while (i < 5) {System.out.println(i);i++;}}}
Related Posts
No comments:
Post a Comment