java string with example and java interview questions and answers.

Java string is a class. which is a class of “lang” package. before String class, we need to discuss what is a string in java. so in this tutorial, we are going to focus on everything about java string and java interview questions and answers.

What is String

In Java, String is a sequence of character or you can say an array of character. or a word or a sentence in common language. for example “codebun”.  In Java Java.lang.String class is used to represent the string object.

There is two wat to create string object. by using string literal and new keyword.

String literal: 

String objects are stored in string constant pool which is a memory area. to make memory management efficient for java string. when you create a new object of string JVM will check that string in the pool if the string exists already than new object will not create.

for example, I am going to define two string. there is will we two different reference going to point a single object. because string same.

String hero: "Salman";

String bhai: "Salman";

String object by new keyword:

If you create a string object with a new keyword then strings are stored in heap memory. because objects are stored in heap memory and in this case object is created for every reference whether string same or not.

String s1 = new String("codenun");

Java string with an example.

class Main {
  public static void main(String[] args) {
    String s1 = "codebun";
    String s2 = "codebun";
    String s3 = new String("let's code");
    String s4 = new String("let's code");
    
    System.out.println(s1);
    System.out.println(s2);
    System.out.println(s3);
    System.out.println(s4);
  }
}


Immutable String in Java.

Immutable means unchangeable, It means if you create an object of string class than it will not change. for example.

class Main {
  public static void main(String args[]) {

    String s1 = new String("codenum");
    s1.concat(".com");

    System.out.println(s1);
  }
}

The output should we codebun because object s1 will not modify.

Mutable string in Java.

Mutable string means you can insert, append or delete any character from the string. so StringBuffer and StringBuilder is a mutable string class in Java. Output for the below code will be “codebun.com”.

class Main {
  public static void main(String args[]) {

    StringBuffer sb = new StringBuffer("Codebun");
    sb.append(".com");

    System.out.println(sb);
  }
}

The important point about string in Java, you should remember

  • Remeber the meaning of some word which is related to string family like String literal, Immutable String, mutable String, String array.
  • String object is stored in heap memory.
  • String literal is stored in string constant pool.
  • String constant pool is a separate part in the heap memory.
  • The object of String class is immutable.
  • String index is starting from zero.
  • The string is a class it’s not a primitive datatype.

String in Java with interview questions and answers.

How to take input as a string from the user or what is Scanner class.

java.util.Scanner class provides two methods to take input from user first one is next() and other one is nextLine();

import java.util.Scanner;

class Main{  
 public static void main(String args[]){  
   Scanner sc = new Scanner(System.in);
   String s1 = sc.next();
   System.out.println(s1);
 }  
}

What is String Pool?

String pool is part of heap memory which is used to store String literals.

How to compare two string in Java.

String s1 = "codebun";
String s2 = "codebun";
String s3= new String("codebun");
System.out.println((s1==s2)); //true
System.out.println("(s1==s3)); //false
System.out.println("(s1.equals(s3))); //true

What is the output of below program?

public class Main {

  public static void main(String[] args) {
    String s1 = new String("codebun");
    String s2 = new String("CodeBun");
    System.out.println(s1 = s2);
  }

}

Find the output

public class Main {

  public static void main(String[] args) {
    String s1 = "Codebun";
    String s2 = new String(s1);
    System.out.println(s1.equals(s2));
  }

}