java

public class MyCustomException extends RuntimeException { public MyCustomException(String message) { super(message); } } class Test { public static void main(String[] args) { Test test = new Test(); try { test.reserveSeats(8); } catch (MyCustomException e) { System.out.println(“Exception occurred with message: ” + e.getMessage()); } } private void reserveSeats(int numberOfSeats) { if (numberOfSeats > 5) { throw…

Read More Custom Exceptions in Java

throw new Exception(); class Test { public static void main(String[] args) { Test test = new Test(); test.reserveSeats(8); } private void reserveSeats(int numberOfSeats) { if (numberOfSeats > 5) { throw new IllegalArgumentException(“No more than 5 seats can be reserved.”); } else { System.out.println(“Reservation successful!”); } } } Output: Exception in thread “main” java.lang.IllegalArgumentException: No more…

Read More Difference Between throw and throws in Java

void methodName() throws Exception class Test { public static void main(String[] args) { } private void validateStatus(String status) { if (status.equals(“invalid”)) { throw new IOException(“Status of the file is invalid, the stream is closed!”); } } } class Test { public static void main(String[] args) { Test test = new Test(); test.validateStatus(“valid”); // OK test.validateStatus(“invalid”);…

Read More Java throws Keyword

throw new Exception(); class Test { public static void main(String[] args) { reserveSeat(new User(“Steve”, “Johnson”, false, 32)); // OK reserveSeat(new User(“Mike”, “Lawson”, true, 16)); // Exception will occur } private static void reserveSeat(User user) { if (user.getAge() < 18) { throw new IllegalArgumentException(“The user is under 18 years old!”); } } } class User {…

Read More Throw an Exception in Java

try { //Statements that may cause an exception } catch { //Handling exception } finally { //Statements to be executed } class Test { public static void main(String[] args) { try { System.out.println(20 / 0); // this will throw ArithmeticException } catch (ArithmeticException ae) { System.out.println(“ArithmeticException occurred!”); } finally { System.out.println(“Finally block…”); } } }…

Read More Java finally block

Java try-catch blocks are an essential part of Java programming, used for handling exceptions that might occur during program execution. When used correctly, try-catch blocks can help you write more robust and error-free code. In this tutorial, I will explain what Java try-catch blocks are, how they work, and how to use them effectively. I’ll…

Read More Java Try-Catch Blocks: A Comprehensive Guide

class Test { public static void main(String[] args) { long startTime = System.currentTimeMillis(); StringBuffer stringBuffer = new StringBuffer(“Java”); for (int i = 0; i < 8000000; i++) { stringBuffer.append(“Programming”); } System.out.println(“Time taken by StringBuffer: ” + (System.currentTimeMillis() – startTime) + “ms”); startTime = System.currentTimeMillis(); StringBuilder stringBuilder = new StringBuilder(“Java”); for (int i = 0; i…

Read More Difference between StringBuilder and StringBuffer classes

public int compareTo(Object obj) class Test { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add(“Python”); list.add(“Java”); list.add(“JavaScript”); list.add(“Ruby”); list.add(“Kotlin”); System.out.println(“Before sorting:”); System.out.println(list); // sorting the list Collections.sort(list); System.out.println(“After sorting:”); System.out.println(list); } } Output: Before sorting: [Python, Java, JavaScript, Ruby, Kotlin] After sorting: [Java, JavaScript, Kotlin, Python, Ruby] Sorting the array of…

Read More Comparable in Java

Queue queue1 = new LinkedList(); Queue queue2 = new PriorityQueue(); class Test { public static void main(String[] args) { Queue<String> queue = new LinkedList(); queue.offer(“Steve”); queue.offer(“Megan”); queue.offer(“Ryan”); queue.offer(“Melissa”); System.out.println(queue); } } Output: [Steve, Megan, Ryan, Melissa]   We can also use the add(Object o) method to add elements to the Queue. In this example, we will…

Read More Queue in Java

HashMap<K, V> map = new HashMap<>(); Map<Integer, String> map = new HashMap<>(); class Test { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, “Java”); map.put(7, “Python”); map.put(10, “Ruby”); map.put(3, “Kotlin”); System.out.println(map); } } Output: {1=Java, 3=Kotlin, 7=Python, 10=Ruby} class Test { public static void main(String[] args) { Map<Integer, String> map…

Read More Map in Java

HashSet<String> set = new HashSet<>(); Set<String> set = new HashSet<>(); class Test { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add(“Java”); set.add(“Kotlin”); set.add(“Python”); set.add(“Ruby”); System.out.println(set); } } Output: [Java, Ruby, Kotlin, Python] class Test { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add(“Kotlin”); set.add(“Java”); set.add(“Ruby”); set.add(“Python”); Set<String>…

Read More Set in Java