Keywords in Java
Keywords in Java are predefined, reserved words that have a special meaning for the compiler. They cannot be used as identifiers (names for variables, methods, classes, etc.). Let’s see the type of keywords and their implementation in detail. Implementation and use of various Java keywords, grouped by their functionality:
-
Data Types
- int, double, float, char, boolean, byte, short, long
- The datatypes are the very first keywords we come across in Java.
- Datatypes are used to represent different data used in the program.
- Let’s see its implementation:
int age = 25;
double salary = 50000.50;
float height = 5.9f;
char initial = ‘A’;
boolean isEmployed = true;
byte level = 1;
short distance = 3000;
long population = 7000000000L;
-
Control Flow Statements
- if, else, switch, case, default, for, while, do, break, continue, return
- Control statements will help us to control the execution flow of the program.
- Let’s see the implementation of control flow statements:
if (age > 18) {
System.out.println(“Adult“);
} else {
System.out.println(“Minor“);
}
-
Exception Handling
- try, catch, finally, throw, throws
- try, catch and finally, keywords are used to handle the exceptions in Java. The statements which are to be monitored for exceptions are kept in the try block. The exceptions thrown by the try block are caught in the catch block. finally, the block is always executed.
- Exception handling is one of the concepts in Java and the keywords used in exception handling are already mentioned
- Let’s see the implementation of the keyword
- More about exception handling then go through the detailed concept in Java.
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println(“Division by zero”);
} finally {
System.out.println(“This will always execute”);
}
public void riskyMethod() throws Exception {
throw new Exception(“Something went wrong”);
}
-
Object-Oriented Programming
Abstract: The abstract keyword is used to implement the abstraction in Java. A method that doesn’t have a method definition must be declared as abstract and the class containing it must be declared as abstract. You can’t instantiate abstract classes. Abstract methods must be implemented in the sub-classes. You can’t use abstract keywords with variables and constructors.
Class: The class keyword is used to define the classes in Java
Extends: The extends keyword is used in inheritance. It is used when a class
extends another class.
Implements: implements keyword is used while implementing an
interface.
Interface: The interface keyword is used to define the interfaces in Java.
Interface which holds only abstract method, methods should be
implemented the sub-class
this, is super, new
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void makeSound() {
System.out.println(“Some sound”);
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
}
@Override
void makeSound() {
System.out.println(“Bark”);
}
}
interface Pet {
void play();
}
class Cat extends Animal implements Pet {
Cat(String name) {
super(name);
}
@Override
void makeSound() {
System.out.println(“Meow”);
}
@Override
public void play() {
System.out.println(“Playing with a toy”);
}
}
Dog dog = new Dog(“Buddy”);
dog.makeSound(); // Outputs: Bark
Cat cat = new Cat(“Whiskers”);
cat.play(); // Outputs: Playing with a toy
Final: The final keyword is used when a class or a method or a field doesn’t need further modifications. a final class can’t be extended, the final method can’t be overridden and the value of a final field can’t be changed.
public, private, protected, static, abstract, synchronized, volatile, transient, native, strictfp
public class Example {
private int counter;
protected String name;
static final int MAX_COUNT = 100;
public static void main(String[] args) {
Example example = new Example();
example.counter = 1;
System.out.println(MAX_COUNT);
}
synchronized void incrementCounter() {
counter++;
}
}
abstract class AbstractClass {
abstract void doSomething();
}
class ConcreteClass extends AbstractClass {
@Override
void doSomething() {
System.out.println(“Doing something”);
}
}
-
Package and Import
Import: The import keyword is used to import the members of a particular package into the current Java file.
package, import
package com.example;
import java.util.List;
import java.util.ArrayList;
public class PackageExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add(“Item 1”);
System.out.println(list);
}
}
-
Others
Assert: The assert keyword is used in the assertion statements. These statements will enable you to test your assumptions about a program. Assertion statements provide the best way to detect and correct the programming errors. Assertion statements take one Boolean expression as input and assumes that this will be always true. If the Boolean expression returns false, AssertionError will be thrown.
void, enum, instanceof, null, true, false
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
public class OtherExamples {
public static void main(String[] args) {
Day today = Day.MONDAY;
assert today != null : “Today cannot be null”;
if (today instanceof Enum) {
System.out.println(“Today is an enum”);
}
Do visit our channel to learn more: Click Here
Author:-
Chetna Malagi
Call the Trainer and Book your free demo Class For Java Call now!!!
| SevenMentor Pvt Ltd.