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) { [Link](); } } }
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){ [Link]
.println([Link]()); } }}
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"); [Link]([Link]()); }}
Page 3 of 12
4. Collections - ArrayList
Use ArrayList for dynamic arrays.
import [Link].*;
public class ArrayListDemo { public static void main(String[] args){ List<String> list=new
ArrayList<>(); [Link]("a"); [Link]("b"); for(String s:list) [Link](s);} }
Page 4 of 12
5. Collections - HashMap
Key-value map example.
import [Link].*;
public class HashMapDemo { public static void main(String[] args){ Map<String,Integer> m=n
ew HashMap<>(); [Link]("a",1); [Link]("b",2); [Link]([Link]("a")); } }
Page 5 of 12
6. File IO
Read and write files using try-with-resources.
import [Link].*;
public class FileIODemo{ public static void main(String[] args) throws Exception{ try(Buff
eredWriter bw=new BufferedWriter(new FileWriter("[Link]"))){ [Link]("Hello\n"); } try(B
ufferedReader br=new BufferedReader(new FileReader("[Link]"))){ [Link]([Link]
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++) [Link]([Link]().getName()+" -> "+i); };
Thread t = new Thread(r); [Link](); } }
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++) [Link](); };
Thread t1=new Thread(r), t2=new Thread(r); [Link](); [Link](); [Link](); [Link]();
[Link]([Link]()); }}
Page 8 of 12
9. Lambda Expressions
Use lambdas with functional interfaces.
import [Link].*;
public class LambdaDemo{ public static void main(String[] args){ List<Integer> l=[Link]
List(1,2,3); [Link](x->[Link](x)); } }
Page 9 of 12
10. Stream API
Basic stream operations example.
import [Link].*;
public class StreamDemo{ public static void main(String[] args){ List<Integer> l=[Link]
List(1,2,3,4); [Link]().filter(x->x%2==0).forEach([Link]::println); } }
Page 10 of 12
11. NIO - Paths
Using [Link] and Files.
import [Link].*;
public class NioDemo{ public static void main(String[] args) throws Exception{ Path p=Path
[Link]("[Link]"); [Link](p, [Link]("line1")); [Link]([Link]
dAllLines(p)); } }
Page 11 of 12
12. Reflection - Basic
Inspect class methods/fields at runtime.
import [Link].*;
public class ReflectionDemo{ public static void main(String[] args) throws Exception{ Clas
s<?> c = [Link]("[Link]"); Method[] m = [Link](); [Link]
n([Link] + " methods in String"); } }
Page 12 of 12