Java Binary Search

This post explains : Java code for binary search in list Java code for recursive binary search Java code for iterative binary search   Searching List in Java If a list is not sorted, searching can only be done by element by element by comparison. So, its better to sort an unsorted list before running […]

String to XML and XML to String in Java

This article provides utility methods to : Convert an xml document object to string Convert a string to xml document object     Convert xml document object to string For converting xml document to string, we will use DOMSource and Transformer classes as follows :     private static String toXmlString(Document document) throws TransformerException {     TransformerFactory […]

String in switch statement in Java 7

Java started supporting Strings in switch statement since Java 7. Here is an example of using Strings in switch block : String status = "one"; switch(status) { case "one":         System.out.println("Choice one"); // Matching case         break; case "two:         System.out.println("Choice two");         break; default:         System.out.println("Choice other than one or two");         break; } Things to note: 1) The String […]

Java String immutability

String class and Immutability String class is immutable. String class is final so no one can modify behavior of any of the string methods. So, no body can override the String methods to alter the immutability.   String Constant Pool Java maintains a pool of all string literals in order to minimize the memory usage […]

Exception Handling in Java

This article provides an introduction to exception and exception handling in Java.   Exceptions in Java   Exceptions represent exception conditions that alter the normal program flow. Exceptions can be hardware failure, resource availability,or even code issues or bugs.   Every exceptions an instance of a class that has Exception class in its inheritance hierarchy. […]