Sunday, 6 July 2014

StarCount Program

public class Star
    {
            public static void main(String[] args)
            {
                    for(int i=0;i<4;i++)
               {
                        for(int j=0;j<=i;j++)
                       {
                               System.out.print("*");
                       }
                       System.out.println();
               }
      }
    }


output :

*
**
***
****

Tuesday, 24 June 2014

Why java is not supporting multiple inheritance

- - First reason is ambiguity around Diamond problem occurs.

- - multiple inheritances does complicate the design and creates problem during casting, constructor chaining etc 

- - Since interface only have method declaration and doesn't provide any implementation there will only be just one implementation of specific method hence there would not be any ambiguity.



Example :
   
  1. class A{  
  2. void msg(){System.out.println("Hello");}  
  3. }  
  4.   
  5. class B{  
  6. void msg(){System.out.println("Welcome");}  
  7. }  
  8.   
  9. class C extends A,B{//suppose if it were  
  10.    
  11.  Public Static void main(String args[]){  
  12.    C obj=new C();  
  13.    obj.msg();//Now which msg() method would be invoked?  
  14. }  
  15. }  

How to convert String to Number in java program

To convert a string into an int, use:
String str = "1234";
int num = Integer.parseInt(str);
To convert a number into a string, use:
int num = 1234;   
String str = String.valueOf(num);

Why there are no global variables in Java?

- -   The global variables breaks the referential transparency

- -   Global variables creates collisions in namespace.
- -  Global variables are usually a design flaw.
- - Your components should be self-contained and should not need any global state.
Instead, use private static fields.
- - Global variables (in the Java context - public static variables) are bad, because:
  • harder to maintain - you can't put a breakpoint or log each change to a variable, hence unexpected values at runtime will be very hard to track and fix
  • harder to test - read Miško Havery's post
  • harder to read - when someone sees the code he'll wonder:
    • where does this come from?
    • where else it is read?
    • where else it is modified?
    • how can I know what's its current value?
    • where is it documented?
To make one clarification that seems needed - variables != constants. Variables change, and that's the problem. So having a public static final int DAYS_IN_WEEK = 7 is perfectly fine - no one can change it.

Wednesday, 28 May 2014

concept of factory method and Singleton process

Singleton :

                A singleton class is one which allows  us to create only one object for JVM.
                we can use it to create a connection pool.

class Stest {
    private static Stest st;  // private variable

    private Stest() {       // private constructor
        System.out.println("OBJECT CREATED FIRST TIME");
    }

    public static Stest create()  //Factory method
    {
        if (st == null)
        {
            st = new Stest();
        } else
                {
                System.out.println("OBJECT ALREADY CREATED");
            }
       
        return st;
    }

    public static void main(String[] args) {
        Stest st1 = Stest.create();
        Stest st2 = Stest.create();
        Stest st3 = Stest.create();
        if ((st1 == st2) && (st2 == st3) && (st3 == st1)) {
            System.out.println("ALL OBJECTS ARE SAME");
        } else {
            System.out.println("ALL OBJECTS ARE NOT SAME");
        }
    }
}

OUTPUT :

OBJECT CREATED FIRST TIME
OBJECT ALREADY CREATED
OBJECT ALREADY CREATED
ALL OBJECTS ARE SAME

Tuesday, 27 May 2014

Algorithm to find if linked list contains loops or cycles

1) Use two pointers fast and slow
2) Move fast two nodes and slow one node in each iteration
3) If fast and slow meet then linked list contains cycle
4) if fast points to null or fast.next points to null then linked list is not cyclic



boolean hasLoop(Node first) {

    if(first == null) // list does not exist..so no loop either.
        return false;

    Node slow, fast; // create two references.

    slow = fast = first; // make both refer to the start of the list.

    while(true) {

        slow = slow.next;          // 1 hop.

        if(fast.next != null)
            fast = fast.next.next; // 2 hops.
        else
            return false;          // next node null => no loop.

        if(slow == null || fast == null) // if either hits null..no loop.
            return false;

        if(slow == fast) // if the two ever meet...we must have a loop.
            return true;
    }
}

Monday, 26 May 2014

When to use abstract class

public abstract Animal
{
   public void eat(Food food)
   {
        // do something with food.... 
   }

   public void sleep(int hours)
   {
        try
 {
  // 1000 milliseconds * 60 seconds * 60 minutes * hours
  Thread.sleep ( 1000 * 60 * 60 * hours);
 }
 catch (InterruptedException ie) { /* ignore */ } 
   }

   public abstract void makeNoise();
}
Note that the abstract keyword is used to denote both an abstract method, and an abstract class. Now, any animal that wants to be instantiated (like a dog or cow) must implement the makeNoise method - otherwise it is impossible to create an instance of that class. Let's look at a Dog and Cow subclass that extends the Animal class.
public Dog extends Animal
{
   public void makeNoise() { System.out.println ("Bark! Bark!"); }
}

public Cow extends Animal
{
   public void makeNoise() { System.out.println ("Moo! Moo!"); }
}
Now you may be wondering why not declare an abstract class as an interface, and have the Dog and Cow implement the interface. Sure you could - but you'd also need to implement the eat and sleep methods. By using abstract classes, you can inherit the implementation of other (non-abstract) methods. You can't do that with interfaces - an interface cannot provide any method implementations.