Updates from February, 2009 Toggle Comment Threads | Keyboard Shortcuts

  • Subinkrishna Gopi 2:34 pm on February 5, 2009 Permalink |
    Tags: shortcode, syntax,   

    A small WordPress stuff – Syntax highlighting using Shortcode 

    Whenever I tried to post something which contains some Java/Javascript/XML code snippet, I always find it difficult to add the source code with syntax highlighted. I used to put them in <pre> and set the color using <span> . And that was horrible.

    But we have a better option of using shortcodes. Just put some special tags on either side of your code block, specify the language and we are done! This is pretty cool! Wow!

    Your code goes here

    Read more about it:

  • Subinkrishna Gopi 6:30 pm on September 13, 2007 Permalink |  

    Different ways to create objects in Java 

    This is a trivia. Yeah, it’s a bit tricky question and people often get confused. I had searched a lot to get all my doubts cleared.

    There are four different ways (I really don’t know is there a fifth way to do this) to create objects in java:

    1. Using new keyword
    This is the most common way to create an object in java. I read somewhere that almost 99% of objects are created in this way.

    MyObject object = new MyObject();

    2. Using Class.forName()
    If we know the name of the class & if it has a public default constructor we can create an object in this way.

    MyObject object = (MyObject) Class.forName("subin.rnd.MyObject").newInstance();

     3. Using clone()
    The clone() can be used to create a copy of an existing object.

    MyObject anotherObject = new MyObject(); 
    MyObject object = anotherObject.clone();

    4. Using object deserialization
    Object deserialization is nothing but creating an object from its serialized form.

    ObjectInputStream inStream = new ObjectInputStream(anInputStream ); 
    MyObject object = (MyObject) inStream.readObject();

    Now you know how to create an object. But its advised to create objects only when it is necessary to do so.

    • amareswar 4:48 pm on October 18, 2007 Permalink

      one more is through creation of object using classloader

      like this.getClass().getClassLoader().loadClass(“com.amar.myobject”).newInstance();

    • Subin 11:40 am on October 19, 2007 Permalink

      Hi Amar,

      Thank you for this one. I wish to hear more from you 🙂


    • subavinodhini 12:41 pm on September 3, 2009 Permalink

      It is also possible to create an object through
      factory methods

      Ex:- NumberFormat obj=NumberFormat.getInstance();

    • Subinkrishna G 12:54 pm on September 3, 2009 Permalink

      Thank you for your comment. But NumberFormat.getInstance() is just a wrapper. Internally the getInstance() is using ‘new’ keyword to create an object.

      — Subin

    • subavinodhini 5:36 pm on September 14, 2009 Permalink

      Hi Subin,

      Thanks for your immediate reply.I have checked the Java API,Then the newInstane() is also using the new operator internally right.


    • Subinkrishna G 9:59 pm on September 15, 2009 Permalink

      Class.forName().newInstance() is using the reflection API to create an object.


    • pacesettergraam 3:39 pm on December 25, 2012 Permalink

      Reblogged this on pacesettergraam.

    • maadamvenkataramana 5:03 pm on June 10, 2013 Permalink

      Another way is
      Class c=MyClassName.class;
      MyClassName mcn=(MyClassName)c.newInstance();

  • Subinkrishna Gopi 3:15 pm on July 13, 2007 Permalink |
    Tags: collections, , map, ,   

    TreeMap vs HashMap 

    Both TreeMap & HashMap are two different implementation of the Map interface. Even though this post is titled “TreeMap vs HashMap” I would like to say how they are connected and how much similar they are.

    Both TreeMap & HashMap are not synchronized. To make it synchronized we have to explicitly call Collections.synchronizedMap( mapName ) . Both supports “fail-fast” iterators. Both of them doesn’t support duplicate keys.


    HashMap allows null as both keys and values. HashMap is useful when we need to access the map without cosidering how they are added to the map (means, unordered lookup of values using their keys). HashMap is synchronized while it is being looked up. HashMap doesn’t allow duplicated entries.

    The performance of HashMap is based on two optional parameter which we can specify during the creation of the HashMap. Initial capacity & load factor. Initial capacity is the bucket size assigned to a HashMap during it’s creation. Load factor decides when the HashMap needs to be expanded. If the load factor is 0.75, the size will be increased when the current size of the map crosses 75% of its capacity.


    The basic difference between HashMap & TreeMap is that, in a TreeMap the elements are stored in a tree. TreeMap allows us to retrieve the elements in some sorted order defined by the user. So we can say that TreeMap is slower than HashMap. This is the only implementation based on SortedMap interface.

    TreeMap allows us to specify an optional Comparator object during its creation. The keys should be compatible with the comparator specified. This comparator decides the order by which the keys need to be sorted.

    public interface Comparator
        public int compare (Object object1, Object object2);
        public boolean equals (Object object);

    If we are not specifying the comparator, TreeMap will try to sort the keys in the natural order. Means will consider them as instances of Comparable interface.

    public interface Comparable
        public int compareTo (Object objectToCompare);

    Classes like Integer, String, Double etc implements Comparable interface. So if we are to use an object of a custom class as the key, ensure that it’ s class implements the Comparable interface.

    public class MyCustomKey implements Comparable
        private int value;
        public MyCustomKey(int value)
           this.value = value;
        public int compareTo (MyCustomKey key)
           int comparison = 0;           
           // Note:
           // Return -1 if this.value < key.value
           // Return  0 if this.value = key.value
           // Return  1 if this.value > key.value           
           return (comparison);

    A common mistake that everyone does is not to override the hashcode(). If we are failing to do so, map.get(new MyCustomKey(<value>)); may not give you what you were expecting. So it is always adviced to override the hashCode() if objects of that class is being used as a key.

    public class MyCustomKey implements Comparable
        private int value;
        public MyCustomKey(int value)
        public int compareTo (MyCustomKey key)
        public int hashCode()
            // Note:
            // If two objects are equal then their corresponding hashCode ()
            // should return the same value too.
            return (this.value * 199);
Compose new post
Next post/Next comment
Previous post/Previous comment
Show/Hide comments
Go to top
Go to login
Show/Hide help
shift + esc