String class in java with interview questions and answers

String class in java and its methods.

A string is a class from lang package. Which is public and final class extends Object class and implements Serializable, Comparable<String>, CharSequence interface.  String Class in Java is immutable class. It means once you create an object of String class. It will not be modified read more about immutable String.

Some important point you should remember about String Class.

  1. String class is final so we can not Extend String class in Java.
  2. When you create String object as string literal that objects are stored in String constant pool. and when you create a string object with new keyword its stored in heap memory.
  3. String Class is immutable class.
  4. In Java String is not a Data type it is a literal.

Java interview questions and answers for the string class.

How to create String object in Java.

There is two different way to create String object.

With new keyword:

String s1 = new String();

With String literal:

String = "codebun";

How to compare two String Object in Java.

To compare two String Object in Java we need to use equals()  method. we can not use “==” operator because equals method compare the value of String object but “==” operator compares address of the object.

public class Main {
  
  public static void main(String[] args) {
    String s1 = new String("asd");
    String s2 = new String("asd");
    System.out.println(s1.equals(s2)); // return True
    
    System.out.println(s1==s2); //  return flase
  }

}

The output should we True and False.

How to concatenation two string in Java.

By Using “+” sign and concat() method.

public class Main {
  
  public static void main(String[] args) {
    String s1 = "asd";
    String s2 = "Lasd";
    String s3 = new String("asd");
    String s4 = new String("asd");
    System.out.println(s1+s2);
    System.out.println(s3+s4);
    
    System.out.println(s1.concat(s2));
    System.out.println(s3.concat(s3));
    
    
    /*
     * Output: asdLasd
     *  asdasd
     *   asdLasd 
     *   asdasd
     */
  }

}

The string class in java an is an immutable class so how can we change it. means how can we concat two string object.

Java compiler is too smart, So when we use “+” operator between two string or any variable Compiler transform this in  (new StringBuilder()).append(“Code”).append(” Bun).toString(); .

So StringBuilder and StringBuffer are mutable classes so compiler creates a new object of StringBuilder class and append String class objects and again convert it in String class object. finally, you get modified string.

Explain any 10 methods from String class in Java.

charAt(int n)

charAt method is used to find a char at an index. an index starts from 0 and it will return a char value.

In this example, we have a string “codebun” and we want char at 3rd index.

public class Main {
  
  public static void main(String[] args) {
    String s1 = "codebun";
    char c = s1.charAt(3);
    System.out.println(c);	
    /*
     * Output: e
    
     */
  }

}

replace() and replaceAll()

replace() with replace a char with other char in a String. and replaceAll() method will replace the string with other String Let’s see an Example for replace() and replaceAll() method of String class in Java.

public class Main {
  
  public static void main(String[] args) {
    String s1 = "Java tutorial with Codebun";
    String s2 = s1.replace('c','j');
    String s3 = s1.replaceAll("Java","Core Java");
    System.out.println(s2);	
    System.out.println(s3);	
    /*
     * Output:
     * Java tutorial with Codebun
       Core Java tutorial with Codebun
    
     */
  }

}

toUpperCase() and toLowerCase()

As the name suggests to convert lower case in upper case and to convert upper case in lower case let’s see an example fortoUpperCase() and toLowerCase() method of String class in Java.

public class Main {

  public static void main(String[] args) {
    String s1 = "Java tutorial with Codebun";
    String lower = s1.toLowerCase();
    String upper = s1.toUpperCase();

    System.out.println(lower);
    System.out.print(upper);
    /*
     * Output: java tutorial with codebun 
     * JAVA TUTORIAL WITH CODEBUN
     */
  }

}

indexOf()

int indexOf(int  c )

Used to find the index of a char in the string. it will return an integer value and index start from Zero.

int indexOf(int ch, int fromIndex)

Used to find the index of a char in the string from an index.

int indexOf(String substring)

To find the index of substring.

int indexOf(String substring, int fromIndex)

To find the index of substring from an index.

Lest see an example of indexOf()

public class Main {

  public static void main(String[] args) {
    String s1 = "Java tutorial with Codebun";
    int index1 = s1.indexOf('a');
    System.out.println(index1);
    int index2 = s1.indexOf('t', 8);
    System.out.println(index2);

    int index3 = s1.indexOf("wi");
    System.out.println(index3);

    int index4 = s1.indexOf("wi", 5);
    System.out.println(index4);
    /*
     * 1 
     * 16
     * 14 
     * 14
     */
  }

}

substring()

To find the substring from a string.

public class Main {

  public static void main(String[] args) {
    String s1 = "Java tutorial with Codebun";
    String subString1 = s1.substring(6);
    String subString2 = s1.substring(5, 8);
    System.out.println(subString1);
    System.out.println(subString2);
    /*OutPut:
     * utorial with Codebun 
     * tut
     */
  }

}

split()

Split() is used to convert a string to an array.

public class Main {

  public static void main(String[] args) {
    String s1 = "Java tutorial with Codebun";
    String s[] = s1.split(" ");
    for (int i = 0; i < s.length; i++) {
      System.out.println(s[i]);
    }
    /*
     * OutPut: Java 
     * tutorial
     *  with
     *   Codebun
     */
  }

}

 

 

 

 

 

 

 

 

Add a Comment

Your email address will not be published. Required fields are marked *

7 − one =