this program is not working correctly

public class samp3 { public static void main(String args[]) { StringBuffer d=new StringBuffer("mom"); System.out.println(d); StringBuffer c=new StringBuffer(d); c.reverse(); System.out.println(c); if(c==d) { System.out.println("It is palindrome"); } else { System.out.println("It is not palindrome"); } } }


6 Answers
1-6 of  6
6 Answers
  • public class samp3 { 
        public static void main(String args[]) { 
            StringBuffer d=new StringBuffer("mom"); 
            System.out.println(d); 
            StringBuffer c=new StringBuffer(d); 
            c.reverse(); 
            System.out.println(c); 
            if(c.toString().equals(d.toString())) { 
                System.out.println("It is palindrome"); 
                
            } 
            else {
                System.out.println("It is not palindrome"); 
                
            } } }

  • Check this(I use the equals() method instead of == beacuse in a string pallindrome we can check the context not refference and I use toString() because everything in java is represented in the form of object except primitive data type. So, toString() is to convert the object into string).
    class Str{
    public static void main(String args[]) {
                StringBuffer d=new StringBuffer("dad"); 
                System.out.println(d); 
                StringBuffer c=new StringBuffer(d);
                c.reverse();
                System.out.println(c);
                if(c.toString().equals(d.toString())) 
                { System.out.println("It is palindrome");
                }
                else { 
                    System.out.println("It is not palindrome"); 
                } 
            }
    }

  • public class samp3 {
                 public static void main(String args[]) {
                     StringBuffer d=new StringBuffer("mom");
                     System.out.println(d);
                     
                     StringBuffer c=d.reverse();
                    
                   
                     System.out.println(c);
                     if(c.equals(d)) {
                         System.out.println("It is palindrome");
                         
                     } else
                     {
                         System.out.println("It is not palindrome");
                         
                     }
                     
                     
          
         }
    }

    now it will be executed,try this one

  • It is Working

    output-  mom
    mom
    It is not palindrome

    but check case option ...   it not declare according to java 

  • Class name should be in upper case 

Core Java

Didn't get the answer.
Contact people of Talent-Core Java directly by clicking here