Essential Java Programming Examples and Concepts
Posted on Apr 23, 2026 in Computer Engineering in Information Technologies Engineering
Java Programming Examples
1. Reverse Number and Sum of Digits
import java.util.Scanner;
class SumReverse {
int n, rev = 0, sum = 0, r;
Scanner sc = new Scanner(System.in);
void input() {
System.out.print("Enter a Number: ");
n = sc.nextInt();
}
void reverse() {
while (n > 0) {
r = n % 10;
rev = rev * 10 + r;
sum = sum + r;
n = n / 10;
}
System.out.println("Sum of digits: " + sum);
System.out.println("\nThe Reverse of a Number is: " + rev);
}
public static void main(String args[]) {
SumReverse rr = new SumReverse();
rr.input();
rr.reverse();
}
}
2. Sorting Algorithm (Ascending Order)
import java.util.Scanner;
class Ascending {
public void arrange() {
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array: ");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Ascending Order: ");
for (int i = 0; i < n - 1; i++) {
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
public class AscendingOrder {
public static void main(String[] args) {
Ascending a1 = new Ascending();
a1.arrange();
}
}
3. Complex Number Addition
import java.util.Scanner;
public class Complex {
double real, imag;
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
public static void main(String[] args) {
double r, i;
Scanner s = new Scanner(System.in);
System.out.println("Enter the first complex number");
r = s.nextDouble();
i = s.nextDouble();
Complex n1 = new Complex(r, i);
System.out.println("Enter the second complex number");
r = s.nextDouble();
i = s.nextDouble();
Complex n2 = new Complex(r, i);
Complex temp = add(n1, n2);
System.out.printf("Sum = %.1f + %.1fi", temp.real, temp.imag);
}
public static Complex add(Complex n1, Complex n2) {
return new Complex(n1.real + n2.real, n1.imag + n2.imag);
}
}
4. Static Members
class Numob {
static int count = 0;
Numob() {
count++;
}
public static void main(String[] args) {
new Numob();
new Numob();
new Numob();
new Numob();
System.out.println("Number of objects created: " + count);
}
}
5. Method Overloading
import java.util.Scanner;
class Overload {
double area(float l, float w, float h) { return l * w * h; }
double area(float l) { return l * l * l; }
double area(float r, float h) { return 3.1416 * r * r * h; }
}
public class MethodOverloading {
public static void main(String[] args) {
Overload o1 = new Overload();
Scanner s = new Scanner(System.in);
// Implementation logic follows...
}
}
6. Inheritance and Student Results
class Student {
public String name;
public int a[] = new int[5];
public int total = 0, avg;
// Methods for reading and displaying data
}
class Result extends Student {
public void percentCal() {
// Calculation logic
}
}
7. Multilevel Inheritance
class X { X(int i) { System.out.println("Created X"); } }
class Y extends X { Y(int i, int j) { super(i); System.out.println("Created Y"); } }
class Z extends Y { Z(int i, int j, int k) { super(i, j); System.out.println("Created Z"); } }
8. Multithreading
class Odd implements Runnable { public void run() { /* Logic */ } }
class Even implements Runnable { public void run() { /* Logic */ } }
9. Keyboard Events
import java.awt.*;
import java.awt.event.*;
public class KeyExample extends Frame implements KeyListener {
// AWT implementation for handling keyboard input
}