Advanced Java Concepts
1. Exceptions - Try Catch
Handle exceptions with try-catch.
public class ExceptionDemo { public static void main(String[] args) { try { int x = 5/0; }
catch (ArithmeticException e) { e.printStackTrace(); } } }
Page 1 of 12
2. Custom Exception
Define and throw custom exception.
class MyException extends Exception { MyException(String msg){ super(msg);} }
public class CustomEx { public static void check(int n) throws MyException { if(n<0) throw
new MyException("Negative"); }
public static void main(String[] args){ try{ check(-1);} catch(MyException e){ System.out
.println(e.getMessage()); } }}
Page 2 of 12
3. Generics
Generic class example.
class Box<T> { T val; Box(T v){val=v;} T get(){return val;} }
public class GenericsDemo{ public static void main(String[] args){ Box<String> b=new Box<>
("hi"); System.out.println(b.get()); }}
Page 3 of 12
4. Collections - ArrayList
Use ArrayList for dynamic arrays.
import java.util.*;
public class ArrayListDemo { public static void main(String[] args){ List<String> list=new
ArrayList<>(); list.add("a"); list.add("b"); for(String s:list) System.out.println(s);} }
Page 4 of 12
5. Collections - HashMap
Key-value map example.
import java.util.*;
public class HashMapDemo { public static void main(String[] args){ Map<String,Integer> m=n
ew HashMap<>(); m.put("a",1); m.put("b",2); System.out.println(m.get("a")); } }
Page 5 of 12
6. File IO
Read and write files using try-with-resources.
import java.io.*;
public class FileIODemo{ public static void main(String[] args) throws Exception{ try(Buff
eredWriter bw=new BufferedWriter(new FileWriter("out.txt"))){ bw.write("Hello\n"); } try(B
ufferedReader br=new BufferedReader(new FileReader("out.txt"))){ System.out.println(br.rea
dLine()); } } }
Page 6 of 12
7. Threads - Runnable
Create threads via Runnable.
public class ThreadDemo { public static void main(String[] args){ Runnable r = () -> { for
(int i=0;i<5;i++) System.out.println(Thread.currentThread().getName()+" -> "+i); };
Thread t = new Thread(r); t.start(); } }
Page 7 of 12
8. Synchronization
Synchronize access to shared data.
class SyncCounter { private int c=0; public synchronized void inc(){ c++; } public int get
(){ return c; } }
public class SyncDemo{ public static void main(String[] args) throws InterruptedException{
SyncCounter sc=new SyncCounter(); Runnable r=()->{ for(int i=0;i<1000;i++) sc.inc(); };
Thread t1=new Thread(r), t2=new Thread(r); t1.start(); t2.start(); t1.join(); t2.join();
System.out.println(sc.get()); }}
Page 8 of 12
9. Lambda Expressions
Use lambdas with functional interfaces.
import java.util.*;
public class LambdaDemo{ public static void main(String[] args){ List<Integer> l=Arrays.as
List(1,2,3); l.forEach(x->System.out.println(x)); } }
Page 9 of 12
10. Stream API
Basic stream operations example.
import java.util.*;
public class StreamDemo{ public static void main(String[] args){ List<Integer> l=Arrays.as
List(1,2,3,4); l.stream().filter(x->x%2==0).forEach(System.out::println); } }
Page 10 of 12
11. NIO - Paths
Using java.nio.file.Paths and Files.
import java.nio.file.*;
public class NioDemo{ public static void main(String[] args) throws Exception{ Path p=Path
s.get("out.txt"); Files.write(p, java.util.List.of("line1")); System.out.println(Files.rea
dAllLines(p)); } }
Page 11 of 12
12. Reflection - Basic
Inspect class methods/fields at runtime.
import java.lang.reflect.*;
public class ReflectionDemo{ public static void main(String[] args) throws Exception{ Clas
s<?> c = Class.forName("java.lang.String"); Method[] m = c.getMethods(); System.out.printl
n(m.length + " methods in String"); } }
Page 12 of 12