String Related Questions

Take this Java quiz and see how much you learn

  1. What is immutable class?
    Ans:Immutable class is class, whose object's state cannot be modified after it is created.Like in java String is an immutable class. After creating its object, it  can't be modified.
    For example
    String str=new String("xyz");
    str=str.concat("abc");
    When we are tring to concat "abc" with "xyz", its crating a new object altogether, its no more pointing preveous object.
  2. Write piece of code for creating an  immutable class?
    Ans:There are various way to create immutable class, this is one of them
    final class ImmutableClass {
        private  String name;
        public String getName() {
            return this.name;
        }
        public ImmutableClass(String name) {
           // do something here , to initialize the String name
        this.name=name;
    }
    }
  3. What is difference between
    String s="abc" and String s= new String("abc");?

    Ans:String s="abc"; means its creating one reference not an object and it will store in stack.
    String s=new String("abc"); means its creating one string object and it'll store in heap.
  4. String s= new String("abc");
    How many objects will be creating here?

    Ans:Two objects. One is through new String() another when we are passing "abc" to string object.
  5. What is output of this program?
    package com;
    public class TestString {
        public static void main(String a[]){
            String str="abc";
            String str1=new String("abc");
            System.out.println(str==str1);
           
        }
     
    }
    Ans: false
  6. What is output of this program?
    package com;
    public class TestString {
        public static void main(String a[]){
            String str="abc";
            String str1=new String("abc");
            System.out.println(str.equals(str1));
            }
     }
    Ans: true
  7. What is output of following program?
    package com;
    public class TestString {
        public static void main(String a[]){
            String str1=new String("abc");
            String str2=new String("abc");
            System.out.println(str1==str2);
            }
     }
    Ans: false
  8. What is output of following program?
    package com;
    public class TestString {
        public static void main(String a[]){
            String str1="abc";
            String str2="abc";
            System.out.println(str1==str2);
            }
     }
    Ans: true
  9. What is difference between "==" and "equals()" methods?
    Ans: equal() method checks weather two String contains same content or not.
    Like String s1=new Sting("xyz")
              String s2 ="xyz"
    When I'll write s1.equals(s2), answer is true.
    The operator "==", compares  the references, whether two reference pointing to same object or not.
    In above example If I'll write
    s1==s2
    Answer is false;
    If I'll write like
    String s="asd";
    String s1="asd";
    In this case ,s==s1 is true.
  10. What is difference between String Buffer , String Builder and String Buffer?
    Ans: String: String is immutable in nature. We can't modify String object after creating.
     String Builder: Its mutable and  non-synchronized . So its perpermance is faster than String Buffer.
     String Buffer: Its also mutable and synchronized.
  11. When We should use String and when StringBuffer?
    When the requirement like what ever is there in object should not cheange that case we should go for String.
    But when we have to change frequently in object, in that case , we should go for StringBuffer.
    For example String s="some";
                    s= s.concat("com");
    In this case two object will unnecessarily create, and it will take more memory space as well.
    Bu in case of StingButter, when we'll write
                   StringBuffer sb= new StringBuffer("some");
                    sb=sb.append("come");
    It's all together one object.