Welcome to Programming Tutorials by SourceTricks. Tutorials on CPP, Data Structures, Android, Java, HTML, Design Patterns and Algorithms. Includes programming interview questions with code samples.

Java Tutorials

These articles provide list of key concepts around Java with code samples.

Java Interview Questions

This article contains common core java interview questions and answers.

Programming in C++

 The page provide list of concepts to understand programming in C++. Includes code samples to explain the described C++ concepts.

Programming interview questions and answers in C++

These articles lists commonly encountered programming interview questions and answers and provides sample implementation in C++.

Data Structures using C++

These tutorials explain commonly used data structures and provides sample implementations in C++.

Algorithms in C++

These articles provide sample implementations of common searching and sorting algorithms in C++.

Design Patterns using C++

These articles explain the principles behind commonly used design patterns and provide sample implementations in C++.

Android

This pages provides a set of android tutorials with code samples.

HTML, CSS and Javascript

These articles provide tutorials on HTML, CSS, JavaScript with code samples.

Saturday, July 19, 2014

Generics is a key element in the Java programming language and a solid understanding is essential for any Java programmer. This article provides an introduction to the concepts around Generics in Java.

Generics Introduction

Generics provide the ability to create classes, methods and interfaces to be used with different types of data in a type-safe manner. Generics by adding type safety to the code enables bugs to be detected at compile time rather than at run time. This provides stability to the implementation. The Collections Framework is a great example for understanding Generics. Collections classes like Lists and Maps can used in a type-safe manner by using Generics.

Generalized classes with Object type

Java has the capability to create generalized classes, method and interfaces by using Object types. Let us consider a quick example of creating a stack class using Object type. In this example, we use an ArrayList of objects to hold the data.

package com.sourcetricks.generics;

import java.util.ArrayList;
import java.util.List;

public class ObjectStack {
   private List<Object> data = new ArrayList<Object>();
 
   public void push(Object item) {
     data.add(item);
   }
 
   public Object pop() {
     if ( data.size() > 0 ) {
        Object val = data.get(data.size()-1);
        data.remove(data.size()-1);
        return val;
     }
     return null;
   }
}

To use this stack our main would something like below. Please note that we need to do the type casting every time we pop an element from the stack. Assuming the intent is to create a stack of String if someone my mistake puts in a integer type then it would result in a ClassCastException. Note that it is not possible to detect these mistakes at compile time. Run-time issues are hard to debug for large projects.

public class JavaGenerics {
   public static void main(String[] args) {
     ObjectStack st = new ObjectStack();
     st.push("string");
     st.push(10);
     int i = (Integer) st.pop();
     String s = (String) st.pop();   
  }
}

Generalized classes with Generic types

Generic types add the type safety that is lacking in the previous example. There is also no need to cast an Object type into the actual data type. Let us redo the previous example using generic types. Note that we have replaced Object type with type parameter T in this example.

public class Stack<T> {
    private List<T> data = new ArrayList<T>();
 
    public void push(T item) {
       data.add(item);
    }
 
    public T pop() {
      if ( data.size() > 0 ) {
        T val = data.get(data.size()-1);
        data.remove(data.size()-1);
        return val;
      }
      return null;
    }
}

To use this stack our main would something like below. Please note when we instantiate the class we specify the concrete type to be used. If we try to add to the stack any type other than String then we would get a compile time error.

public class JavaGenerics {
   public static void main(String[] args) {
     Stack<String> st = new Stack<String>();
     st.push("string1");
     st.push("string2");
     String s1 = st.pop();
     String s2 = st.pop();   
   }
}

Raw types

A raw type is the name of a generic class or interface without any type arguments. We can rewrite our main as below. This approach again makes the code unsafe and using of raw types should be avoided.

public class JavaGenerics {
   public static void main(String[] args) {
     Stack st = new Stack();
     st.push("string1");
     st.push("string2");
     String s1 = (String) st.pop();
     String s2 = (String) st.pop();     
   }
}

The Diamond

In Java 7, we can replace the type arguments required to invoke the constructor with empty <>. The compiler infers the type from the context. Using <> is called diamond. We can rewrite the previous example as below.

public class JavaGenerics {
   public static void main(String[] args) {
     Stack<String> st = new Stack<>();
     st.push("string1");
     st.push("string2");
     String s1 = st.pop();
     String s2 = st.pop();   
   }
}

Generic methods

Generics can be used with methods as well. We can declare generic methods that uses one or more type parameters.

public static <T> boolean myfunc(T a, T b) {
  if ( a == b ) return true;
  return false;
}

Bounded type parameters

When specifying a type parameter we can specify an upper bound from which all type arguments should be derived.

The previous example doesn't make sense for String type. We can restrict the method to be limited to Number type by specifying the superclass as Number.

public static <T extends Number> boolean myfunc(T a, T b) {
  if ( a == b ) return true;
  return false;
}

Generic Wildcards

In generic code, the question mark (?) referred as wildcard represents an unknown type.

  • Upper bounded wildcards are used to relax the restrictions on a variable. To declare an upper bounded wildcard, use the wildcard(?) followed by the extends keywords and the upper bound.
  • The unbounded wildcard type is specified using the wildcard character (?).
  • Lower bounded wildcard restricts the unknown type to be a specific type or a super type of that type. To declare a lower bounded wildcard is expressed using the wildcard character(?), followed by the super keyword, followed by its lower bound.

The Java documentation provides guidelines on when to use upper bound and lower bound wildcards. Please refer this page.

package com.sourcetricks.generics;

import java.util.ArrayList;
import java.util.List;

public class JavaGenerics {
 
 // Upper bounded wildcard
 public static double addMyList(List<? extends Number> list) {
  double sum = 0.0;
  for ( Number elem : list ) {
   sum += elem.doubleValue();
  }
  return sum;
 }
 
 // Unbounded wildcard
 public static void printMyList(List<?> list) {
  for ( Object elem : list ) {
   System.out.println(elem);
  }
 }
 
 // Lower bounded wildcard
 public static void fillMyList(List<? super Integer> list) {
  for ( int i = 1; i <= 5; i++ )
   list.add(i);
 }
 
 // Lower bounded wildcard
 public static void main(String[] args) {
  List<Integer> list1 = new ArrayList<>();
  fillMyList(list1);
  System.out.println(addMyList(list1));
  printMyList(list1);
 }
}

Sunday, July 13, 2014

  • Enum type enables a variable to be set with predefined constants. Examples include days of week, directions etc.
  • In Java Enum type is defined using the keyword enum.
  • In Java Enum types are treated as class types and they are more powerful than other languages.
  • Enum types can have constructor, instance variables and methods.
  • One primary difference compared to classes is Enum types are not created using new() unlike class types.
  • Each enumeration constant is treated as an object.
  • Constructor would be called for each Enum constant.

Java Enum Type Example

public enum DefectStates {
 NEW(10), ASSIGNED(20), OPEN(30), RESOLVED(40), CLOSED(50);
 
 private int val;
 DefectStates(int val) {
  this.val = val;
  System.out.println("Constructor: " + val);
 }
 public int getVal() { return val; }
 public static void main(String[] args) {
  DefectStates ds = DefectStates.New;
 }
}

Output:-
Constructor: 10
Constructor: 20
Constructor: 30
Constructor: 40
Constructor: 50

Iterate Enum type values

Enum has static values method that returns an array containing all of the values of the enum in the order they are declared. This method can be used to iterate over the values of an enum type.

public class JavaEnumTest {
   public static void main(String[] args) {
     for ( DefectStates state : DefectStates.values() ) {
       System.out.println(state)
     }
   }
}

Output:-
NEW
ASSIGNED
OPEN
RESOLVED
CLOSED

Enum approach for creating singleton objects

The most simplest approach to implementing Singleton's is the enum approach. Leverages the guarantee from Java that an enum value is initialized only once and is thread safe.

The example below shows a sample implementation with Enum approach.

public enum EnumTest {
 INSTANCE;
 
 // Test code
 public static void main(String[] args) {
  
  // First thread
  Thread t1 = new Thread(new Runnable() {
   @Override
   public void run() {
    EnumTest obj = EnumTest.INSTANCE;
   }   
  });
  t1.start();
  
  // Second thread
  Thread t2 = new Thread(new Runnable() {
   @Override
   public void run() {
    EnumTest obj = EnumTest.INSTANCE;
   }   
  });
  t2.start();
 }
}

Understanding Synchronization

In a multi-threaded program, when 2 or more threads try to access a common resource there needs to be a way to ensure that only one thread uses the resource at a time. The process by which this is achieved is called synchronization. Java provides language constructs to achieve using synchronized keyword.

To understand synchronization better it is necessary to learn the concept of monitor. Monitor is an object that acts as a mutually exclusive lock. Only one thread can own a monitor at a time. A thread acquires a lock on the monitor to access a shared resource. All other threads attempting to access the shared resource will wait until first thread exits the monitor.

Synchronized Methods

Synchronized methods is one the two synchronization idioms available in Java. In synchronized methods synchronized keyword is added at the method level. It is not possible for two invocations of the synchronized methods on the same object to interleave. When one thread is executing a synchronized method all other threads that invoke the synchronized methods block. Synchronized methods also ensures that state of the changes in the object is visible to all threads (happens before relationship) when the methods exits.

public class ThreadSync {

 class MyCounter {
  int count = 0;
  MyCounter() {   
  }
  public synchronized void increment() {
   count = count + 1;
  }
 }
 
 class MyThread extends Thread {
  MyCounter counter = null;
  MyThread(MyCounter counter) {
   this.counter = counter;
  }
  public void run() {
   counter.increment();
  }
 }
 
 public static void main(String[] args) {
  ThreadSync ts = new ThreadSync();
  MyCounter counter = ts.new MyCounter();
  Thread t1 = ts.new MyThread(counter);
  Thread t2 = ts.new MyThread(counter);
  t1.start();
  t2.start();
 }
}

Synchronized Statements

Synchronized statements is another approach of achieving synchronization in Java. This approach needs an object that provides the lock.

There are 2 scenarios where synchronized statements would be useful.

  1. When we are working with third party libraries which are not multi-threaded and we don't have access to source code then synchronized statement blocks could be used to achieve thread safety.
  2. Synchronized statements are also useful for improving concurrency with fine-grained synchronization where the entire method need not be synchronized but only certain lines of the method.

public class ThreadSync {

 class MyCounter {
  private int count = 0;
  private Object lock = new Object();
  MyCounter() {}
  public void increment() {
   synchronized(lock) {
    count = count + 1;
   }
  }
 }
 
 class MyThread extends Thread {
  MyCounter counter = null;
  MyThread(MyCounter counter) {
   this.counter = counter;
  }
  public void run() {
   counter.increment();
  }
 }
 
 public static void main(String[] args) {
  ThreadSync ts = new ThreadSync();
  MyCounter counter = ts.new MyCounter();
  Thread t1 = ts.new MyThread(counter);
  Thread t2 = ts.new MyThread(counter);
  t1.start();
  t2.start();
 }
}

Reentrant synchronization

Consider an example of a synchronized method which is called recursively by a thread. In this case the thread acquires the lock which it already owns. Allowing a thread to acquire the same lock more than once is called re-entrant synchronization.

Trending