Encoding and Decoding

Base64 encoding is an algorithm which uses 64 printable characters to replace each character from original data in an algorithmic sequence, so that as same we can decode these data. In most of the places we need to Encode or even need to Encrypt most sensitive data when we transfer to other network. Encoding is not encryption. In all our below programs we use Base64 to encode a string that scrambles the output and it may appear to be unreadable binary data. But it can be easily decoded if some have knowledge on encoding algorithms. Most often encoding a string will end with "=" symbol. Apart from encoding there are lot of advanced encryption algorithms like MD5, RSA-SHA etc., which are all not easy to break it.
In java we have got function to encode and decode strings. Lets see simple example for encoding and decoding using sun.misc package classes,


public class EncodeDecodeTest {

 public static void main(String[] args) {

  String str = "Java Discover & Questions $$$";
  System.out.println("Input   : "+str);
  try{
   str = new sun.misc.BASE64Encoder().encode(str.getBytes("UTF8"));
   System.out.println("Encoded : "+str);
   
   str = new String((new sun.misc.BASE64Decoder().decodeBuffer(str)), "UTF8");
   System.out.println("Decoded : "+str);
  
  }catch (IOException e) {
   e.printStackTrace();
  }
 }
}


OUTPUT:


Input   : Java Discover & Questions $$$
Encoded : SmF2YSBEaXNjb3ZlciAmIFF1ZXN0aW9ucyAkJCQ=
Decoded : Java Discover & Questions $$$


sun.misc classes are not guaranteed to be consistent across different versions of JRE. In those cases we can use 3rd part open source code or jars to encode our data. There are lot of other mechanism to encode and decode our sensitive data and few are,
  • Using Apache Commons Codec
  • Using Java Mail API (MIME Utility)
  • Using MiGBase64



Using Apache Commons Codec - Link to Download 


import org.apache.commons.codec.binary.Base64;

public class EncodeDecodeTest {

 public static void main(String[] args) {

  String str = "Java Discover & Questions $$$";
  System.out.println("Input   : "+str);
  
  str = new String(Base64.encodeBase64(str.getBytes()));
  System.out.println("Encoded : "+str);

  str = new String(Base64.decodeBase64(str.getBytes()));
  System.out.println("Decoded : "+str);     
 }
}

OUTPUT:


Input  : Java Discover & Questions $$$
Encoded : SmF2YSBEaXNjb3ZlciAmIFF1ZXN0aW9ucyAkJCQ=
Decoded : Java Discover & Questions $$$



Using Java Mail API (MIME Utility) - Link to Download


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;


public class EncodeDecodeTest {

 public static void main(String[] args){

  String str = "Java Discover & Questions $$$";
  System.out.println("Input   : "+str);
  try{
   ByteArrayOutputStream byteAOS = new ByteArrayOutputStream();
   OutputStream outStream = MimeUtility.encode(byteAOS, "base64");
   outStream.write(str.getBytes());
   outStream.close();
   System.out.println("Encoded : " + byteAOS.toString());

   ByteArrayInputStream byteAIS = new ByteArrayInputStream(byteAOS.toByteArray());
   InputStream intStream = MimeUtility.decode(byteAIS, "base64");
   byte[] tmp = new byte[byteAOS.toByteArray().length];
   int length = intStream.read(tmp);
   byte[] decoded = new byte[length];
   System.arraycopy(tmp, 0, decoded, 0, length);
   System.out.println("Decoded : " + new String(decoded));
      
  }catch (MessagingException  e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

OUTPUT:


Input   : Java Discover & Questions $$$
Encoded : SmF2YSBEaXNjb3ZlciAmIFF1ZXN0aW9ucyAkJCQ=

Decoded : Java Discover & Questions $$$



Using MiGBase64 - Link to Download

MiGBase64 is a simple and small java class which is fast and effective encoding and decoding algorithm. 


public class EncodeDecodeTest {

 public static void main(String[] args){

  String str = "Java Discover & Questions $$$";
  System.out.println("Input   : "+str);

  str = Base64.encodeToString(str.getBytes(), true);
  System.out.println("Encoded : " + str);

  str = new String(Base64.decode(str.getBytes()));
  System.out.println("Decoded : " + new String(str));
 }
}

OUTPUT:


Input   : Java Discover & Questions $$$
Encoded : SmF2YSBEaXNjb3ZlciAmIFF1ZXN0aW9ucyAkJCQ=
Decoded : Java Discover & Questions $$$

Difference between ClassNotFoundException and NoClassDefFoundError

When we see in general both ClassNotFoundException and NoClassDefFoundError are errors which comes when JVM or Class Loader not able to find appropriate class while loading at run-time. ClassNotFoundException is a checked exception and NoClassDefFoundError is an Error which comes under unchecked. 
There are different types of Class Loader loads classes from difference sources, sometimes it may cause library JAR files missing or incorrect class-path which causes loader not able to load the class at run-time. Even though both names sounds similar to class file missing there are difference between ClassNotFoundException and NoClassDefFoundError.
Difference between ClassNotFoundException and NoClassDefFoundError

ClassNotFoundException:
ClassNotFoundException comes when we try to load class at run-time using Reflection and if those class files are missing then application or program thrown with ClassNotFoundException Exception. There is nothing to check at compile time since its load those class at run-time. Best example is when we try to load JDBC driver class using reflection and we fail to point driver jars. Then program thrown with ClassNotFoundException as like below example.



import java.sql.Connection;
import java.sql.DriverManager;

public class ExceptionTest {

 public static void main(String[] args) {
  Connection conn = createConnection();
 }

 public static Connection createConnection() {

  Connection conn = null;

  try {
   Class.forName("com.mysql.jdbc.Driver");
   System.out.println("MySQL JDBC Driver Registered!");
   conn = DriverManager.getConnection(
     "jdbc:mysql://192.168.1.2:3306/mydatabase", "root", "root");

  } catch (Exception e) {
   System.err.println("Please configure mysql driver file !!!");
   e.printStackTrace();
  }
  return conn;
 }
}


OUTPUT:


Please configure mysql driver file !!!
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
 at java.net.URLClassLoader$1.run(Unknown Source)


These are the different class loaders from difference classes like,
forName method in class Class.
loadClass method in class ClassLoader.
findSystemClass method in class ClassLoader.


NoClassDefFoundError:

NoClassDefFoundError is thrown when a class has been compiled with a specific class from the class path but if same class not available during run-time. Missing JAR files are the most basic reason to get NoClassDefFoundError. As per Java API docs "The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found."


Lets see simple example to get NoClassDefFoundError Error.


public class SecondTestClass {

 public SecondTestClass(){
  System.out.println("Hello Java");
 }
}



public class ErrorTest {

 public static void main(String[] args) {
  new SecondTestClass();
 } 
}


NOTE:
Once you create both java files compile SecondTestClass.java and ErrorTest.java classes separately. Next delete the generated class file called SecondTestClass.class. Next try to run ErrorTest.class file which will thrown with NoClassDefFoundError error message.

OUTPUT:


Exception in thread "main" java.lang.NoClassDefFoundError: SecondTestClass
 at com.db.ErrorTest.main(ErrorTest.java:7)
Caused by: java.lang.ClassNotFoundException: SecondTestClass







Sorting duplicate elements in single array

 
There are lot of sorting algorithms like bubble sort, quick sort, insertion sort, rad-ix sort etc., But all these sorting algorithm gives array of elements in an ascending or descending order. 
In point of interview if they ask to sort the array with all the duplicate elements should be placed at end of the array again with sorted sub arrays.
Sorting duplicate elements in single array


For example 

Input array : { 5, 6, 2, 4, 8, 3, 5, 1, 3, 4, 5, 2, 1, 5, 3 }

Output array : {1, 2, 3, 4, 5, 6, 8,    1, 2, 3, 4, 5,    3, 5,   5 }

If we see above output, input array has been sorted with sub arrays with all duplicate elements at last. So lets see simple java code with the help of Collection API to sort the given array as per above problem


import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class DuplicateArraySort {

 public static void main(String[] args) {

  int array[] = { 5, 6, 2, 4, 8, 3, 5, 1, 3, 4, 5, 2, 1, 5, 3 };

  System.out.print("Before array sorting : ");
  printArray(array);

  Map<Integer, Integer> myMap = generateMap(array);
  Set<Integer> set = (Set<Integer>) myMap.keySet();

  for (int i = 0; i < array.length;) {

   Iterator<Integer> itr = set.iterator();

   while (itr.hasNext()) {
    int key = itr.next();
    if ((myMap.get(key)) > 0) {
     myMap.put(key, (myMap.get(key) - 1));
     array[i] = key;
     i++;
    }
   }
  }

  System.out.print("\nAfter array sorting : ");
  printArray(array);
 }

 public static Map<Integer, Integer> generateMap(int[] array) {

  Map<Integer, Integer> myMap = new TreeMap<Integer, Integer>();
  for (int i : array) {
   if (myMap.containsKey(i)) {
    myMap.put(i, myMap.get(i) + 1);
   } else {
    myMap.put(i, 1);
   }
  }
  return myMap;
 }

 public static void printArray(int[] array) {
  for (int i : array) {
   System.out.printf("%d, ", i);
  }
 }
}


OUTPUT:


Before array sorting : 5, 6, 2, 4, 8, 3, 5, 1, 3, 4, 5, 2, 1, 5, 3, 
After array sorting : 1, 2, 3, 4, 5, 6, 8, 1, 2, 3, 4, 5, 3, 5, 5, 





Calculate String Length

 
This is one of the simple interview question that need to calculate String length without using length() function. There are lot of ways to find the find the length like converting String to Character array and by iterating it. Lets see simple java code to calculate string length.
Calculate String length




public class StringSize {

 public static void main(String[] args) {
  
                String str = "Java Discover";
  
                int length = getStringLength(str);
  
                System.out.printf("String - %s \nLength - %d ",str,length);
 }
 
 public static int getStringLength(String str){
  
                int length = 0;
  
                char[] array = str.toCharArray();
  for (char c : array) {
   length++;
  }
  
                return length;
 }
}


OUTPUT:


String - Java Discover 
Length - 13



Anagrams

Anagram is a type of word or phrase generating by rearranging the letters of a given word or phrase to produce a new word or phrase. Need to use all the original letters exactly once to frame new word or phrase. Some of the famous Anagram words are given below,
Anagrams


Mother in lawWoman Hitler
Debit cardBad credit
Eleven plus twoTwelve plus one
Payment receivedEvery cent paid me
AstronomerMoon starer
DormitoryDirty room
PunishmentNine Thumps
DesperationA rope ends it
Snooze alarmsAlas! No more Zs

and lot more are there...


Lets implement anagrams in java for only words to generate. 

public class Anagram {

 private HashSet<String> words = new HashSet<String>();
 
 public static void main(String[] args) throws Exception {
  
  String myWord = "art";

  Anagram obj = new Anagram();
  
  obj.getAnagramWords("", myWord);
  
  obj.printAnagramWords();
  
 }

 public void getAnagramWords(String prefix, String word) {
  if (word.length() <= 1) {
   words.add(prefix+word);
  } else {
   for (int i = 0; i < word.length(); i++) {
    String middle = word.substring(i, i + 1);
    String before = word.substring(0, i);
    String after = word.substring(i + 1);
    getAnagramWords(prefix + middle, before + after);
   }
  }  
 }
 
 public void printAnagramWords(){
  System.out.println("LIST OF ANAGRAM WORDS :::");
  for (String string : words) {
   System.out.println(string);   
  }
 }
}

OUTPUT:


LIST OF ANAGRAM WORDS :::
rta
atr
tra
art
tar
rat

Quicksort in Java

In our earlier tutorials we have seen about Bubble sort and Insertion sort. In this tutorial we will see about one of the best divide and conquer algorithm called Quicksort. In divide and conquer sorting algorithm the original elements will be separated into two parts (divided) and then individually sorted (conquered) and then combined. Need to remember that Quciksort can be implemented in same array and no need of additional array required.
quicksort example in java


  • If array contains only one element or zero element then the array is in sorted order already. 
  • If array contains more than 1 element then we need to select middle element as pivot element.
  • Next we need to place all smaller elements than pivot element on left side and all larger elements are placed at right side of the array. 
  • Sort both the arrays recursively by applying Quicksort algorithm.
  • Finally combine both the sorted arrays. 


Now lets see simple java code to sort integer array using Quicksort algorithm.


public class QuickSort {

 public static void main(String[] args) {
  
  int array[] = { 6, 3, 9, 2, 5, 7, 8, 1, 4 };
  
  System.out.print("Array before sorting : ");
  printArray(array);
  
  quickSort(array, 0, array.length - 1);
  
  System.out.print("Array after sorting : ");
  printArray(array);
 }

 public static void quickSort(int array[], int lower, int length) {

  if (lower >= length)
   return;
  int first = lower, last = length;
  int piv = array[(first + last) / 2]; // Picking pivot element

  while (first < last) {
   // Searching for smaller element than pivot element
   while (first < last && array[first] < piv)
    first++;

   // Searching for greater element than pivot element
   while (first < last && array[last] > piv)
    last--;
   if (first < last) {
    int tmp = array[first];
    array[first] = array[last];
    array[last] = tmp;
   }
  }
  if (last < first) {
   int tmp = first;
   first = last;
   last = tmp;
  }
  // Calling recursive
  quickSort(array, lower, first);
  quickSort(array, first == lower ? first + 1 : first, length);
 }

 public static void printArray(int[] array) {
  for (int i : array) {
   System.out.print(i + ", ");
  }
  System.out.println();
 }
}


OUTPUT:


Array before sorting : 6, 3, 9, 2, 5, 7, 8, 1, 4, 
Array after sorting : 1, 2, 3, 4, 5, 6, 7, 8, 9, 



Generics in Java


Generics are introduced in JDK 1.5 on-wards and one of the important and flexible feature in Java by enabling types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Its same as like other formal parameters by providing way of reusing code for different input types. Only the difference in inputs to formal parameters are values where as for inputs to type parameters are types.  
Generics in java

For developers to pass type or method to operate on objects of various types. Also it provides compile time type safety by making fully statically typed language. There are lot of advantage on Generics compared to non-Generic codes like,

  • Use of Multiple Bounds
  • Stronger type checks at compile time.
  • Elimination of casts.
  • Use of wildcards with extends or super to increase API flexibility
  • Enabling programmers to implement generic algorithms.

Now lets see few examples on using Generics in Java.


public class GenericsTest {

 public static void main(String[] args) {
  
  // Without Genric
  List list = new ArrayList();
  list.add("Hello Java Discover");
  // Here we need casting while we use non-generic code
  String val = (String) list.get(0);
  
  
  // With Genric
  List<String> list1 = new ArrayList<String>();
  list1.add("Hello Java Discover");
  // No need of casting while we use Generic
  String val1 = list1.get(0);  
 }
}


If we see in above we declared 2 List object without Generic type and 2nd with "String" type setting. When we get values from 1st List specifically we need to cast those values to corresponding type. But while we get values from 2nd list no need to casting to "String" as like 1st List. 


Next we write single method which will take multiple type inputs like integer, float, String etc.,


public class GenericsTest {

 public static void main(String args[]) {
  
  Integer[] iArray = new Integer[] { 10, 30, 50, 20, 60, 70 };
  System.out.print("Integer Array : ");
  genericMethod(iArray);
  
  Float[] fArray = new Float[]{ 4.41f, 5.23f, 2.23f, 99.41f };
  System.out.print("Float Array   : ");
  genericMethod(fArray);
  
  String[] sArray = new String[]{ "Hello", "Java", "Discover", "2013"};
  System.out.print("String Array  : ");
  genericMethod(sArray);  
 }

 public static <E> void genericMethod(E[] array) {
  for (E element : array) {
   System.out.print(element+", ");
  }
  System.out.println();
 }
}

OUTPUT:


Integer Array : 10, 30, 50, 20, 60, 70, 
Float Array   : 4.41, 5.23, 2.23, 99.41, 
String Array  : Hello, Java, Discover, 2013, 

In above program we have used genericMethod() function to print all 3 different types like integer, float and string. 

Next we will see about creating Generic class using Types.


public class MyGenericClass<T> {

 private T t;

 public MyGenericClass(T t) {
  this.t = t;
 }

 public T getValue() {
  return t;
 }
}


public class GenericsTest {

 public static void main(String args[]) {

  MyGenericClass<Integer> iValue = new MyGenericClass<Integer>(100);
  MyGenericClass<String> sValue = new MyGenericClass<String>("Hello Java Discover");
  MyGenericClass<Double> dValue = new MyGenericClass<Double>(434.23);

  System.out.println("Integer Value : " + iValue.getValue());
  System.out.println("String Value  : " + sValue.getValue());
  System.out.println("Double Value  : " + dValue.getValue());
 }
}

OUTPUT:


Integer Value : 100
String Value  : Hello Java Discover
Double Value  : 434.23

In above example we have created Generic class called "MyGenericClass" which will take different type constructor parameters.