Friday 20 July 2012

How to permutate a String?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class StringPermutaion {

    //How this class works
    //1. Get a string from user
    //2. Check for spaces or null
    //3. Call permutation function
    //4. How permutation works?
    //1. take the first element and permutate the rest
    //2. call a recursive function to do this


    public static void main(String[] args) {

        InputStreamReader is=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(is);
        System.out.println("Enter the String   :");
        try {
            String input=br.readLine();
            if(input !=null && input.length()>0){
            StringPermutaion sp=new StringPermutaion();
            sp.permutateString("", input);
            }
            else{
                System.out.println("Please enter a valid String");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void permutateString(String s1, String s2){
       
        if(s2.length()<=1){
            System.out.println(s1+s2);
        }
        else{
            for(int i=0;i<s2.length();i++){
                String s=s2.substring(0,i)+s2.substring(i+1);
                permutateString(s1+s2.charAt(i), s);
            }
        }

    }

}

What is the difference between String s="String"; and String s=new String("String");

String s= "Fool";

The above statement creates a string objects and reference s will point to that object.

String s=new String("Fool");

Then what is the above one....Strange right?

No..The above operation also create an object as a normal java object and reference will be assigned to s.
But the above statement will create two java objects...!!!!

Here we have to notice one important concept called String Pool.

All String objects will be placed in a particular area of  heap called String pool. When you create a String objects, JVM will check in the string pool whether the similar object exists or not, if yes it won't create a new object, simply point the reference to the string pool object, if not it will create a new object and place the object in the string pool and point the reference to it.

String s= "Fool"; 

It will create an object and place it in String pool and point the reference to it, means creating only one object.

String s=new String ( "Fool");

It will create an object (Not in the string pool) as a normal object creation with new operator and assign the reference to it. Also JVM is smart enough, so it will create another object and place it in string pool for future use, means creating two objects.

So which is good????

String s= "Fool"; Bz less memory usage and less overhead for JVM.

Just check the below code and try to understand.

String s1="Fool";

String s2=new String ( "Fool");

System.out.println(s1==s2); ==> print false
System.out.println(s1.equals(s2)); print true