Basic To Advance Java
Basic To Advance Java
Arrays
Methods
Inheritance
Exception handling
Collections (ArrayList)
import java.util.*;
// Base class
class Person {
String name;
int age;
void introduce() {
System.out.println("Hi, I'm " + name + ", age " + age);
}
}
@Override
void introduce() {
super.introduce();
System.out.println("I'm a student in grade " + grade);
}
}
// Basic input/output
System.out.print("Enter your name: ");
String user = sc.nextLine();
System.out.println("Hello, " + user);
// Conditional statements
int x = 10, y = 20;
if (x < y) System.out.println(x + " is less than " + y);
// Switch case
int choice = 2;
switch (choice) {
case 1 -> System.out.println("Choice is 1");
case 2 -> System.out.println("Choice is 2");
default -> System.out.println("Unknown");
}
// While loop
int i = 0;
while (i < 3) {
System.out.println("i = " + i);
i++;
}
// Exception handling
try {
int res = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
// Collections
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
System.out.println("Fruits: " + fruits);
sc.close();
}
}
| Lines | Concepts |
| ----- | ------------------------------------------------------------ |
| 1–8 | Classes, constructors |
| 9–15 | Inheritance, method overriding |
| 17–71 | Main logic: I/O, arrays, loops, OOP, exceptions, collections |
🛠 How to Run:
Save the file as Main.java.