Java Polymorphism, Overloading, Lambdas & Exceptions
Constructor and Method Overloading
class Calculator {
// Constructor overloading: same name, different parameters
Calculator() { System.out.println("Default Calculator created"); }
Calculator(String mode) { System.out.println(mode + " Calculator created"); }
// Method overloading: same name, different parameters
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
}
public class OverloadingDemo {
public static void main(String[] args) Read More
Java OOP Code Samples: Shapes, Resizable, Inner Classes
Java OOP Code Samples: Shapes, Resizable, Inner Classes
Summary: This page presents several Java object-oriented examples in one place. Examples include abstract classes for shapes, a resizable Rectangle via an interface, nested inner classes, a custom exception, and a small package usage example. The original code is preserved as a preformatted block below.
Complete Java Examples (original content preserved)
abstract class Shape {
abstract double calculateArea();
abstract double calculatePerimeter( Read More
Core Java Programming Examples Collection
Core Java Programming Examples
This document contains several fundamental Java code examples demonstrating various programming concepts.
1. Reverse an Integer
The ReverseNumber class demonstrates how to reverse an integer using a while loop.
public class ReverseNumber {
public static void main(String[] args) {
int number = 1234;
int reverse = 0;
int original = number;
while (number != 0) {
int digit = number % 10;
reverse = reverse * 10 + Essential Java Algorithms and Programming Fundamentals
Bubble Sort Algorithm in Java
Bubble Sort is a fundamental sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. It is known for its simplicity, though it is inefficient for large lists.
import java.util.Scanner;
class program {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements: ");
int n = sc.nextInt();
int arr[ Read More
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
Read MoreJava Fundamentals: Bytecode, OOP Concepts, and Programming Examples
Java Bytecode and Platform Independence
Defining Java Bytecode
Java Bytecode is the intermediate code generated by the Java compiler after compiling a Java program. When you write a Java source file (.java) and compile it using javac, the compiler converts it into a bytecode file (.class). This bytecode is not specific to any machine and can be executed by the Java Virtual Machine (JVM) on any platform.
Justification for Platform Independence
Platform independence means that a Java program can run on
Read More