Essential Java Concepts: OOP, JVM, and Exception Handling
Java Program: Convert String to Uppercase
This simple Java program demonstrates how to use the built-in toUpperCase() method of the String class.
public class UpperCase
{
public static void main(String[] args)
{
String str = "hello world";
System.out.println(str.toUpperCase());
}
}Understanding JVM and JDK
JVM (Java Virtual Machine)
The JVM is the runtime engine that executes the Java bytecode. It is an abstract machine that enables Java to be platform-independent, adhering to the principle: “Write Once, Run Anywhere.” The JVM interprets the compiled .class files (bytecode) and converts them into machine code specific to the operating system it is running on. It is also responsible for crucial tasks like memory management (Garbage Collection).
JDK (Java Development Kit)
The JDK is a software development environment used to develop Java applications and applets. It includes the JRE (Java Runtime Environment) and a comprehensive set of development tools, such as:
- The compiler (
javac) - The debugger
- The archiver (
jar)
The JDK is the essential toolset a programmer uses to write, compile, and run a Java program.
Java Layout Managers Explained
Layout Managers are specialized objects in Java’s Abstract Window Toolkit (AWT) and Swing libraries that automatically arrange and position graphical components (like buttons, text fields, and panels) within a container.
They handle the complex task of resizing and arranging components when the container size changes, ensuring that the Graphical User Interface (GUI) looks consistent across different screen sizes and platforms.
Examples include:
FlowLayoutBorderLayoutGridLayout
Inheritance in Java OOP
Inheritance is a core Object-Oriented Programming (OOP) concept where a new class (called the subclass or child class) derives properties and methods from an existing class (called the superclass or parent class).
It promotes code reusability and establishes an “is-a” relationship between classes. The extends keyword is used in Java to implement inheritance.
Types of Inheritance in Java
- Single Inheritance: A class inherits from only one superclass.
- Multilevel Inheritance: A class inherits from another class, which itself is inherited from a third class (A → B → C).
- Hierarchical Inheritance: Multiple subclasses inherit from a single superclass.
Note: Multiple and Hybrid inheritance are not supported directly by classes in Java, but they can be achieved using Interfaces.
Java Packages: Definition and Example
A package in Java is a mechanism used to group related classes and interfaces. It functions similarly to a folder or directory structure used to organize files.
Key Purposes of Java Packages
- Organization: Categorizes classes into logical units.
- Access Protection: Controls the visibility and access rights of classes and their members.
- Name Collision Prevention: Allows two classes in different packages to have the same name (e.g.,
java.util.Listandjava.awt.List).
Package Example
package mypack;
class A {
void show() {
System.out.println("Hello");
}
}Applet Program for a Simple Login Page
This example demonstrates creating a basic login interface using Java Applets and AWT components.
import java.applet.*;
import java.awt.*;
/*
<applet code="LoginApplet" width=300 height=200></applet>
*/
public class LoginApplet extends Applet
{
Label l1, l2;
TextField t1, t2;
Button b1;
public void init()
{
l1 = new Label("Username:");
l2 = new Label("Password:");
t1 = new TextField(15);
t2 = new TextField(15);
t2.setEchoChar('*');
b1 = new Button("Login");
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
}
}Abstract Classes vs. Interfaces in Java
Abstract Class Definition
An Abstract Class is a class declared using the abstract keyword that cannot be instantiated (you cannot create an object of it). It is primarily designed to be a base class for concrete classes, providing a common template.
abstract class Animal {
abstract void sound();
void eat() { System.out.println("Eating"); }
}Interface Definition
An Interface is a blueprint of a class that defines method signatures. Since Java 8, interfaces can also include static and default methods. It acts as a contract specifying a set of methods a class must implement.
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() { System.out.println("Bark"); }
}Java Exception Handling Fundamentals
An Exception is an event that occurs during the execution of a program that disrupts the normal flow of the program’s instructions. When an exceptional event occurs, an object representing that event is created and handed over to the runtime system—this process is called throwing an exception.
Methods for Handling Exceptions
Java uses three primary blocks for structured exception handling:
try: The block that contains code that might throw an exception.catch: The block that handles the specific exception thrown.finally: The block that executes always, regardless of whether an exception occurred or was handled.
Exception Handling Example
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Program End");
}