Posts

Showing posts from November, 2010

Java Puzzle–Immutable String

what will be output of the following program ? 1: class StringTest2 2: { 3: public static void main(String[] args) 4: { 5: String str1 = " Hello "; 6: String str2 = " HELLO "; 7: String str3 = str1.toUpperCase(); 8: String str4 = str2.toUpperCase(); 9: System.out.println(str1 == str3); 10: System.out.println(str2 == str4); 11: } 12: }

Java puzzle–StringBuffer

What will be the output of following program ? 1: class StringBufferTest2 2: { 3: public static void main(String[] args) 4: { 5: StringBuffer sb1,sb2; 6: sb1 = new StringBuffer(); 7: sb2 = new StringBuffer(); 8: sb1.append(" Hello "); 9: sb1.append(" Hello "); 10: boolean result = sb1.equals(sb2); 11: System.out.println(" Result = "+ result); 12: } 13: }

Java Puzzle – private fields

Will following program work ? class MyClass { private int privateId = 101; public static void main(String[] args) { System.out.println(" Hello World! "); new MyClass().doStuff( new MyClass()); } public void doStuff(MyClass obj){ //accessing private instance variable System.out.println(obj.privateId); } }

Static Class vs. Singleton Class

I have explained two different implementation for making a class to Singleton, means restricting no of instance of a class to only one.You can refer previous posts Singleton Design Pattern by example & Alternative implementation for Singleton Class . Here by Static Class means a class that’s has only public static methods. Two different implementations as per previous post for restricting class instances to only one are making all constructor private referred as  Singleton  class and declaring all public methods as static.  Singleton class has several advantages over Static class pattern 1. A singleton class can extends class and implement interfaces , while Static class can not. 2. A singleton can be instantiated lazily or asynchronously while a static class is generally initialized when it is first loaded. 3.A singleton class can extended and its methods can be overridden. 4. The most important advantage is the singleton can be handled polymorphic ally without forcing thei

Alternative implementation for Singleton Class

In previous posts on Singleton Designing pattern I have explained one of the way of implementing and solving concurrency issue. Singleton pattern insures that only one instance of the class should be available throughout the application context. There is an alternative implementation for insuring the same functionality that is nothing but “a class with all public static methods” . A static method can be accessed directly by Class name without any class instance. It will serve same purpose as Singleton Class, means it shares only one state across the application. Here is a sample implementation import java.util.*; class SingletonMap { private static Map<String,String> map; static { map = new HashMap<String,String>(); } public static String getValue(String key){ return map.get(key); } public static void putValue(String key, String value){ map.put(key,value); } } Here the SingletonMap class with all methods declared as “static” and private stati

StringBuilder over StringBuffer

As String is an immutable class , any update operations  like replace/substring/append is very expensive. As solution for this StringBuffer class has been introduced for all string update related operations. But problem with StringBuffer is that it is a synchronized class. All its public methods are tagged as “synchronized”. This adds an extra cost to string update operation whenever synchronized is actually not needed. As part of JDK 1.5 a new utility class called StringBuilder has been introduced for non-synchronized string update operations. StringBuilder is designed as a replacement for StringBuffer in single-threaded usage. It means using StrinBuilder instead of StringBuffer where ever synchronization is not an issue will be added to program performance.