Monthly Archives: April 2013

Interesting Java Facts -2

Here is a interesting Java question. What should be values of x and y in below to make the while loop infinite / non terminating

while (x <= y&& x >= y && x != y) {
System.out.println(“hello”);
}

Hint: Answer lies in autoboxing.

Ok, the answer is

Integer x = new Integer(0);
Integer y = new Integer(0);

Now try to think why?

Wrapper Classes and Autoboxing in Java

We know in Java everything is an object, except, primitive types. Primitive types are char, int, boolean etc. As all other things in Java are objects, it is desirable to use our primitve types as Objects. An example situation is when we want to add int to an ArrayList which only accepts objects. The answer to this problem is wrapper classes like Integer class. We can wrap our primitive types into these class objects.

Integer iVal=new Integer(123);

So that is a wrapper class. Lets understand autoboxing now.

How to increment the value of iVal by 10?

Normal Scenario

Integer iVal=new Integer(123);
int temp=iVal.intValue();
temp+=10;
ival=new Integer(temp);

Java knows this is bit of a pain so it provides you help in form of autoboxing.

Autoboxing works

Integer iVal=new Integer(123);
iVal++;

How to use switch statement for String before Java 1.7

How to use switch statement for String before Java 1.7

We know that Java has provided us the functionality of using switch statements directly using Strings. If you are using an older version for some reasons, you can achieve the similar functionality using enums.

Here is an example

public class test2 {

private enum MyEnum{
red, green, blue;
}

public static void main (String s[])
{
MyEnum e=MyEnum.valueOf(“blue”);

switch(e)
{
case red:System.out.println(“red string”);break;
case green:System.out.println(“green string”);break;
case blue:System.out.println(“blue string”);break;
default: System.out.println(“default string”);
}

}
}

Singleton Pattern Round Up: All about singleton pattern

Here is summary of what I have written so far on singleton patterns. The examples taken here are mostly from Java.

Singleton Pattern #1: Basics, Definition, Usage
Gives a basic idea of singleton pattern is all about.

Singleton Pattern #2: Lazy vs Eager Instantiation for a Singleton Class
The difference between two types of instantiations, and which one is better under what circumstances.

Singleton Pattern #3: Problems with Singleton, Thread-Safe Singleton Class
Issues with using singleton, how to make your singleton classes threadsafe.

Singleton Pattern #4: Difference between Singleton and Static classes
Sometimes people confuse between singleton and static classes. When should one use them and why?

Singleton Pattern #4: Difference between Singleton and Static classes

The basic differences between singleton and static classes (when we say  static class in java, it is a class with static members) in nutshell are:

1. Singleton classes can hold instance variable while static can’t
2. Singleton classes can be inherited
3. Static classes are more useful while handling the stateless data
4. At a later stage if you think that you need more instances of a singleton class, you can simply modify the class. Basically make the constructor public/ make the singleton class non-singleton, depending on your requirement
5. For simple functionalities where we need utility methods to be implemented on some data and simple return statements, static classes are of better help. For example, creating a string utility or math utility class which have methods like sqare (takes an integer say 2 and return 4 ), cube (takes 2 and simply returns 8 )

Singleton Pattern #3: Problems with Singleton, Thread-Safe Singleton Class

A side effect of using singleton class: as the constructor of the singleton class is private, you cannot create a subclass of a singleton class, but in most of the cases that is desirable. Additionally you can declare the class as final to explicitly show that the class can’t be subclassed. But in case if it is required to create a subclass of singleton class one can define the constructor of singleton class as protective instead of private.

Another point to note is that the singleton classes are not thread-safe. Simply putting it if the two threads enter the

If(instance==null)
{
//create new instance.
}

block simultaneously, it will create two different instances of the class. This is an undesirable condition and cause unwanted behavior in the application. Also note here that in case of eager instantiation this problem does not occur as the instance gets created beforehand.

Making your singleton class thread-safe is easy, you just have to make your getinstance method synchronized.

Public synchronized static singletonExample getInstance()
{
//if instance is null create else return old instance
}

Originally posted February 15, 2007

Brought back using archive.org

Singleton Pattern #2: Lazy vs Eager Instantiation for a Singleton Class

This post is in continuation to my previous post where I discussed about basics of singleton pattern. The kind of instantiation mentioned in that post, where we create an instance of singleton class only when it is called for the first time is called lazy instantiation. Another variation can be creating the instance before it is called, this kind of instantiation is called eager instantiation

Public Class singletonExample {
private static singletonExample instance=new singletonExample();
private singletonExample{
// no instantiation is possible
}

public static singletonExample getInstance()
{
return instance;
}
}

If you look carefully, here we are using

private static singletonExample instance=new singletonExample();

to initialize the object ‘instance’ rather than setting it as null initially. And in our getInstance() method we are just returning the ‘instance’ without any checks. So the advantage in this case is that we are saved making one check everytime getinstance is called. One apparent disadvantage is that in this case the instance of singletonExample will always be present whether it is called or not.

Originally posted February 13, 2007

Brought back using archive.org

Singleton Pattern #1: Basics, Definition, Usage

Singleton pattern: As the name suggests, a singleton pattern refers to a pattern where you need only a ’single’ instance of a class to be present. The examples can be windows manager where you want only one window to be active at a time or a file read write where only one method should be updating a file at a time.

Here is a skeleton example of a singleton class:

Public Class singletonExample
{
private static singletonExample instance=null;
private singletonExample{
// no instantiation is possible
}

public static singletonExample getInstance()
{
if (instance==null)
{
instance=new singletonClass();
}
return instance;
}
}

the caller class

public class singletonUser{
singletonExample singletonInstance= singletonExample.getInstance();
}

Explanation:

Here singletonExample is our singleton class. In the first line we have created an instance of this class as null. We have declared a private constructor for the class to make sure that no object of class can be created outside the class.

getInstance() method is the entry point for the class which will return the only instance present for the class. This method is marked as static as it will be called directly from other classes. In this method we check if the instance of singletonExample is already present, if yes, return that instance else create the instance and return it. This makes sure that only one instance of the class is present at any point.

In the user class, that is calling our singleton class, we just need to call our singletonExample.getinstance() method which will return us the instance of singletonExample.

Originally posted February 13, 2007 at 2:10 pm

Brought back using archive.org

How to create an effective professional PowerPoint presentation

Bringing old post back from dead courtesy archive.org

I have seen a lot of official presentations which are awfully boring and instead of conveying any message they confuse the audience. So when I had to deliver a presentation some days back, I did some research on how to create an effective PowerPoint Presentation; and here are my findings-

1. KISS your presentation. Ok, don’t create the presentation and then kiss the monitor. What I mean by KISS here is “Keep It Simple, Stupid (Sir :) )”. Yes, if you are creating a professional PowerPoint presentation, there is no place for flashy or cool looks.
2. Size of presentation should be controlled; very big presentations can bore the audience.
3. No over attracting backgrounds to the PowerPoint.
4. The background color and font color should be contrasting, so that text can be easily read.
5. Font selection: No designer fonts are recommended, simple Ariel, SanSerif, Verdana etc. should be good. Font Size should be big enough to read clearly, minimum of 22 or 24.
6. Consistency: Changing background, Fonts, Colors can be distracting.
7. Contents: Be precise; don’t give too much detail in your PowerPoint presentation. You can provide additional details separately if required; you can also provide online links in the presentation for detailed info.
8. Animations: No place for cool animations in a professional PowerPoint presentation, too many flying texts and images will distract the audience attention from the actual content. A simple Appear/disappear or similar plain animations should do.
9. Focus: Check for every slide where the default focus is. That is, it may be possible that the important information is on top-left part but the attention is occupied by some other portion as some image, useless text is displayed somewhere else.
Point to note for checking the slide’s focus

->Control font size: Font size can be very important in terms of attracting attention. The bigger the size, the more attention it will attract.
->Use lists to break the complex info in points format
->Make information appear on the screen when you need it. E.g. if there are 4 points (text or images) to be shown and you want to emphasize each point, make them appear one by one.
->Use graphs to simplify complex data.
->Use images and graphics to support your point as they can be memorized better than the words.

10. Finally take a look at your presentation as a viewer and see how others will perceive it. If you are not convinced with your slides, no way others can be influenced by the same.

Suggested readings

http://www.microsoft.com/atwork/getworkdone/presentations.mspx
http://entrepreneurs.about.com/cs/marketing/a/7sinsofppt.htm
http://sbinformation.about.com/od/sales/a/presentationtip.htm
http://headrush.typepad.com/creating_passionate_users/2005/06/kill_your_prese.html

Originally posted March 28, 2007 at 5:02 pm

archive.org- Someone is keeping a track

Came across this website today archive.org. The website claims to take snapshots of all the websites available on the internet. Initially I thought “What use is that of?”. And then for sake of fun I typed in my blog’s url. To my surprise it showed me various snapshots of my blog and to add to my surprise the first snapshot dated back to somewhere in 2005, the year when I started my blog (my blog crashed twice since then and lost a lot of posts).

Man! that brought a lot of nostalgia. I mean my first post was literally one liner-

The first post

May 20, 2005 on 11:28 pm | In General | No Comments

I am only a brain. The rest of me is appendix.

In my defense, that was 8 years back and yeah, that was the first post 🙂

Anyways, I am actually thinking it can be very useful for me. I can check the old posts and see if anything is there which is still relevant and can be re posted to my blog.  Archive(r), thanks for keeping my posts safe.