Click here to watch in Youtube :
https://www.youtube.com/watch?v=-8aXVJhMqLk&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
Java Tutorial : Java checked exception and unchecked exception(Version3) |
Java Tutorial : Java checked exception and unchecked exception(Version3) |
Java Tutorial : Java checked exception and unchecked exception(Version2) |
Java Tutorial : Java checked exception and unchecked exception(Version1) |
Java Tutorial : Java Exception class hierarchy(Version3) |
Java Tutorial : Java Exception class hierarchy(Version2) |
Java Tutorial : Java Exception class hierarchy(Version1) |
Java Tutorial : Java Exception class hierarchy(Version1) |
Java Tutorial : Java Exception class hierarchy(Version1) |
Java Tutorial : Java Exception class hierarchy(Version1) |
Java Tutorial : Java Exception class hierarchy(Version1) |
Java Tutorial : Java Exception Handling |
/* * With out exception handling */ public class ExceptionHandlingDemo1 { public static void main(String[] args) { int intValue = 50; System.out.println("intValue = " + intValue); double doubleValue = 45.9; System.out.println("doubleValue = " + doubleValue); String str = null; System.out.println("str = " + str.toString()); float floatValue = 10.f; System.out.println("floatValue = " + floatValue); long longValue = 45000; System.out.println("longValue = " + longValue); } }
intValue = 50 doubleValue = 45.9 Exception in thread "main" java.lang.NullPointerException at ExceptionHandlingDemo1.main(ExceptionHandlingDemo1.java:16)
/* * With exception handling. */ public class ExceptionHandlingDemo2 { public static void main(String[] args) { int intValue = 50; System.out.println("intValue = " + intValue); double doubleValue = 45.9; System.out.println("doubleValue = " + doubleValue); try { String str = null; System.out.println("str = " + str.toString()); } catch(Exception exe) { System.out.println("String value is null"); } float floatValue = 10.f; System.out.println("floatValue = " + floatValue); long longValue = 45000; System.out.println("longValue = " + longValue); } }
intValue = 50 doubleValue = 45.9 String value is null floatValue = 10.0 longValue = 45000
Java Tutorial : Java what is an Exception |
public class ExceptionDemo { public static void main(String[] args) { int intValue = 50; System.out.println("intValue = " + intValue); double doubleValue = 45.9; System.out.println("doubleValue = " + doubleValue); String str = null; System.out.println("str = " + str.toString()); float floatValue = 10.f; System.out.println("floatValue = " + floatValue); long longValue = 45000; System.out.println("longValue = " + longValue); } }
intValue = 50 doubleValue = 45.9 Exception in thread "main" java.lang.NullPointerException at ExceptionDemo.main(ExceptionDemo.java:14)
import java.util.ArrayList; import java.util.List; public class UnBoxingDemo { public static void main(String[] args) { Integer integerObj = new Integer(-8); // 1. Unboxing through method invocation int absVal = absoluteValue(integerObj); System.out.println("absolute value of " + integerObj + " = " + absVal); List<Double> list = new ArrayList<>(); list.add(new Double(12.3)); // 2. Unboxing through assignment double doubleValue = list.get(0); System.out.println("doubleValue = " + doubleValue); } public static int absoluteValue(int i) { return (i < 0) ? -i : i; } }
absolute value of -8 = 8 doubleValue = 12.3
Java Tutorial : Java Unboxing (list) |
Java Tutorial : Java Unboxing (list) |
import java.util.ArrayList; import java.util.List; public class UnBoxingDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(new Integer(1)); list.add(new Integer(2)); for (Integer integerObj : list) { int i = integerObj; // Unboxing System.out.println(i); } } }
1 2
Java Tutorial : Java Autoboxing (list) |
Java Tutorial : Java Autoboxing (list) |
import java.util.ArrayList; import java.util.List; public class AutoBoxingDemo { public static void main(String[] args) { List<Integer> li = new ArrayList<>(); for (int i = 1; i < 20; i += 2) { li.add(i); } System.out.println(li); System.out.println(li.get(0).getClass()); } }
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19] class java.lang.Integer
Java Tutorial : Java Autoboxing and Unboxing |
Java Tutorial : Java Autoboxing and Unboxing |
Java Tutorial : Java Autoboxing and Unboxing |
public class AutoBoxingDemo { public static void main(String[] args) { int intValue = 50; Integer integerObj = intValue;// Boxing System.out.println(integerObj); display(intValue); //Boxing } public static void display(Integer integerObj) { System.out.println(integerObj); } } Output
50 50
UnboxingDemo.java
public class UnboxingDemo { public static void main(String[] args) { Integer integerObj = new Integer(100); int intValue = integerObj; // UnBoxing System.out.println(intValue); display(integerObj);//UnBoxing } public static void display(int intValue) { System.out.println(intValue); } }
Output
100 100
Java Tutorial : Java StringBuilder [supported methods] |
Java Tutorial : Java StringBuilder [supported methods] |
Java Tutorial : Java StringBuilder [length and Capacity] |
public class StringMemoryTest { private static final int KBinBytes = 1024; /** * Perform GC */ private void performGC() { for (int i = 0; i < 10; i++) { System.gc(); try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } /** * Get the memory used value. * * @return long */ private long memoryUsed() { return ((Runtime.getRuntime().totalMemory() / KBinBytes) - (Runtime .getRuntime().freeMemory() / KBinBytes)); } /** * Method to test String object memory consumption in a * BIG loop */ public void testStringMemory() { System.out .println("Initial Total memory in Java virtual machine in KBs=" + Runtime.getRuntime().totalMemory() / KBinBytes); System.out .println("Initial Free memory in Java virtual machine in KBs=" + Runtime.getRuntime().freeMemory() / KBinBytes); long initialmemory = memoryUsed(); long startTime = System.currentTimeMillis(); String str = ""; for (int i = 1; i < 50000; i++) { str = str + "Hello"; } long stopTime = System.currentTimeMillis(); long elapsedTime = stopTime - startTime; System.out.println("Time taken to complete the process in MilliSeconds:" + elapsedTime); System.out.println("Memory used by String in KBs=" + (memoryUsed() - initialmemory)); str = null; performGC(); System.out.println("Free Memory after GC in KBs=" + Runtime.getRuntime().freeMemory() / KBinBytes); } /** * Method to test StringBuilder object memory * consumption in a BIG loop */ public void testStringBuilderMemory() { System.out .println("Initial Total memory in Java virtual machine in KBs=" + Runtime.getRuntime().totalMemory() / KBinBytes); System.out .println("Initial Free memory in Java virtual machine in KBs=" + Runtime.getRuntime().freeMemory() / KBinBytes); long initialmemory = memoryUsed(); long startTime = System.currentTimeMillis(); StringBuilder sb = new StringBuilder(); for (int i = 1; i < 50000; i++) { sb.append("Hello"); } long stopTime = System.currentTimeMillis(); long elapsedTime = stopTime - startTime; System.out .println("Time taken to complete the process in MilliSeconds:" + elapsedTime); System.out.println("Memory used by StringBuilder in KBs=" + (memoryUsed() - initialmemory)); sb = null; performGC(); System.out.println("Free Memory after GC in KBs=" + Runtime.getRuntime().freeMemory() / KBinBytes); } /** * Method to test StringBuffer object memory consumption * in a BIG loop */ public void testStringBufferMemory() { System.out .println("Initial Total memory in Java virtual machine in KBs=" + Runtime.getRuntime().totalMemory() / KBinBytes); System.out .println("Initial Free memory in Java virtual machine in KBs=" + Runtime.getRuntime().freeMemory() / KBinBytes); long initialmemory = memoryUsed(); long startTime = System.currentTimeMillis(); StringBuffer sb = new StringBuffer(); for (int i = 1; i < 50000; i++) { sb.append("Hello"); } long stopTime = System.currentTimeMillis(); long elapsedTime = stopTime - startTime; System.out.println("Time taken to complete the process in MilliSeconds:" + elapsedTime); System.out.println("Memory used by StringBuffer in KBs=" + (memoryUsed() - initialmemory)); sb = null; performGC(); System.out.println("Free Memory after GC in KBs=" + Runtime.getRuntime().freeMemory() / KBinBytes); } /** * Main class * * @param args */ public static void main(String[] args) { System.out .println("Initial Max memory in Java virtual machine in KBs=" + Runtime.getRuntime().maxMemory() / KBinBytes); StringMemoryTest memoryTest = new StringMemoryTest(); System.out.println(".....................................................\nString memory test\n"); memoryTest.testStringMemory(); System.out.println(".................................................\nStringBuffer memory test\n"); memoryTest.testStringBufferMemory(); System.out.println("...............................................\nStringBuilder memory test\n"); memoryTest.testStringBuilderMemory(); } }
Initial Max memory in Java virtual machine in KBs=919552 ........................................................... String memory test Initial Total memory in Java virtual machine in KBs=62976 Initial Free memory in Java virtual machine in KBs=61992 Time taken to complete the process in MilliSeconds:7729 Memory used by String in KBs=130666 Free Memory after GC in KBs=349431 ........................................................... StringBuffer memory test Initial Total memory in Java virtual machine in KBs=355840 Initial Free memory in Java virtual machine in KBs=349431 Time taken to complete the process in MilliSeconds:11 Memory used by StringBuffer in KBs=0 Free Memory after GC in KBs=348918 ............................................................ StringBuilder memory test Initial Total memory in Java virtual machine in KBs=355328 Initial Free memory in Java virtual machine in KBs=348918 Time taken to complete the process in MilliSeconds:5 Memory used by StringBuilder in KBs=0 Free Memory after GC in KBs=348406