Full Stack Extension – Java Programming Basics
In-Depth Theory
Java is a high-level, class-based, object-oriented programming language that is platform-independent due
■ In-Depth Concepts:
1. Basics:
- Strongly typed, compiled language
- Write Once, Run Anywhere
- Entry point: public static void main(String[] args)
2. Data Types:
- Primitive: int, float, double, char, boolean
- Non-primitive: String, Arrays, Objects
3. Control Flow:
- if, if-else, switch, for, while, do-while
4. OOP Features:
- Classes, Objects, Inheritance, Polymorphism, Encapsulation, Abstraction
5. Access Modifiers:
- public, private, protected, default (package-private)
6. Important Keywords:
- static, final, this, super, new, return, instanceof
7. Exception Handling:
- try-catch-finally
- throw vs throws
- Checked vs Unchecked exceptions
8. Collections:
- List, Set, Map
- ArrayList, HashMap, HashSet
9. Java Tools:
- javac for compilation
- java for execution
- JDK, JRE, JVM
Code Examples
■ Java Code Examples
1. Hello World:
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
2. Class and Object:
class Car {
String model;
Car(String m) { model = m; }
void drive() { System.out.println(model + " is driving."); }
}
3. ArrayList Example:
import java.util.*;
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
System.out.println(names.get(0));
4. Try-Catch:
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
Practice Assignment
✍■ Practice Assignment - Java Basics
1. Create a class Student with rollNo, name, and marks
- Add a method to print details
2. Write a Java program to:
- Accept 5 numbers into an array
- Sort and print them in ascending order
3. Implement method overloading for a calculator class.
4. Use a HashMap to store employee ID and name pairs, then display them.
5. Bonus:
- Create a program with a base class Animal and derived classes Dog and Cat
- Implement try-catch for NullPointerException