Java String Interview Questions and Answers (2024)

1). What is String?


String is an object that represents a sequence of characters.


2). How many ways to create String objects?


There are 2 ways to create String object:


    I). String literal: it is create by without new keyword and it is created in String Constant Pool area.So you can’t modify this object. This is known as Immutable(Un-modify).


    II). By new keyword: with new keyword.this is created in Heap Area.So it we can modify this object.



3) Can you tell me about String Methods ?


Methods to perform operations on strings such as compare(), concat(), equals(), hashcode(), split(), length(), replace(), compareTo(), intern(), substring() etc


4) How many ways to compare the String ?           (Most Imp Question)


There is 3 ways to compare the String Objects.


   I). By Using equals() Method

   II). By Using == Operator

   III). By compareTo() Method



 I). equals() Method: It only compares content or value of the string not heap or pool area object


 Example:  String s1 = “JAVA”;        String s2 = “JAVA”;   so it is return true condition


Note: Whenever you override equals() method, you should also override hashCode() method.


II). Using == operator:


The == operator compares references not values.

If object from same area like Heap then it will True and if both object in different area(Heap and Pool area) then it will false.


III).compareTo() method:


The String class compareTo() method compares values lexicographically and returns an integer value.

o s1 == s2 : The method returns 0.

o s1 > s2 : The method returns a positive value.

o s1 < s2 : The method returns a negative value.



4). What is Immutable Object and How to create Immutable class?     (Most most Imp Question)


Immutable object is one whose state can not be changed or you can’t modify that Object, once created.


Multi-threaded applications, developers always want to protect the state of their objects from concurrent modifications 

of several threads at the same time, for this purpose, developers normally use the Synchronized blocks whenever they modify the state of an object.

With the help of immutable classes, object states are never modified.


To create an immutable class, you should follow the below steps:


I). Make your class final, so that no other classes can extend it.


II). Make all your fields final, so that they’re initialized only once inside the constructor and never modified afterward.


III). Don’t write the setter methods.because nobody access your from external.


IV). When exposing methods which modify the state of the class, you must always return a new instance of the class.


Example:


final class Employee { // class final

final String penCardNumber; // variable final

public Employee(String penCardNumber) {

this.penCardNumber = penCardNumber;

}

public String getpenCardNumber() { // only getter methods but no setter methods

return penCardNumber;

}}


public class Immutable_Class {

public static void main(String[] args) {

Employee em = new Employee(“Aryan123”);

System.out.println(“penCardNumber is :” + em.penCardNumber);

}}



Note: For other Important String programs please click on this link or section :  Java 8 Stream String Programs | Java 8 Streams Programs – Insight Interview


5). What is a Buffer?


A buffer is a small portion of the device memory storage used to temporarily store some amount of data. Usually, Buffers use the RAM of the device to store the temporary data, and hence, accessing data from the buffer is much faster than accessing the same amount of data from the hard drive.


Note: In every java interview, one String program always asked by interviewer. So plese check the String Interview Programs or String coding Programs in this site. In ups to you can write programs in Java 7 or Java 8 (Functional Programs). Now days all interviewer want to wrtie program in Java 8 way. So please check the Java 8 Stream Programs in this site. Link here:  Java 8 Stream String Programs | Java 8 Streams Programs – Insight Interview