Wednesday, July 6, 2011

Playing with Strings and String Buffer

/*
* A palindromic number reads the same both ways.
* The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
* Find the largest palindrome made from the product of two 3-digit numbers.
*/

/*
* @author Jayashree P
* @date July 5th 2011
*/

public class  Palindrome3D
{
    public static void main(String[] args)
    {
        System.out.println("Plaindrome of 3 digit multiple!")  ;
        for(int firstNumber=99  ; firstNumber >= 10 ;  firstNumber--){
            for(int secondNumber=99; secondNumber >=10 ;  secondNumber--){
                StringBuffer checkPlain = new StringBuffer(secondNumber * firstNumber+"") ;
                StringBuffer reverseCheckPlain = new StringBuffer(checkPlain)  ;
                /* This is interesting, StringBuffers don't equate via equals method.
                    f (secondNumber==91){
                    System.out.println(checkPlain+" , "+firstNumber+" , "+reverseCheckPlain+" , " +reverseCheckPlain.toString().equals(checkPlain.toString()))  ;
                    System.exit(1)  ;
                }*/
                //StringBuffer reverseCheckPlain = checkPlain  ; // This will give you one value with two addresses.
                System.out.println(checkPlain+" , "+reverseCheckPlain.reverse()+" , "+reverseCheckPlain.equals(checkPlain)+" , "+checkPlain)  ;
                if(reverseCheckPlain.toString().equals(checkPlain.toString())){
                    System.out.println(checkPlain);
                    System.exit(0);
                }
            }
        }
    }
}

I have noticed two interesting points here:

                                1. StringBuffer's when trying to equate using equals() method don't not give you expected result, be careful with respect to this.


                                2. StringBuffer's will give you two address to one value you when you use, instantiation as the creation of the object.

The output of the program when the commented code is executed (for point numbered 2 in above list):

D:\MyPrograms\Standalones>java Palindrome3D
Plaindrome of 3 digit multiple!
9801 , 1089 , true , 1089
1089

As you can see the statement:

System.out.println(checkPlain+" , "+reverseCheckPlain.reverse()+" , "+reverseCheckPlain.equals(checkPlain)+" , "+checkPlain)  ;

Gives

9801 , 1089 , true , 1089

The output of the program when the commented code is executed (for point numbered 1 in above list):

D:\MyPrograms\Standalones>java Palindrome3D
Plaindrome of 3 digit multiple!
9009 , 9009 , false , 9009
1089
As you can see the statement:

if(reverseCheckPlain.equals(checkPlain))

Gives


9009 , 9009 , false , 9009

Minute fact, but very interesting.


1 comment:

  1. From Java 5 onwards you can use Stringbuilder in place of StringBuffer in Java , StringBuilder is much faster than StringBuffer but not suitable for concurrent programming.

    Javin
    How Synchronization works in Java

    ReplyDelete