JAVA STRING INTERVIEW QUESTIONS AND ANSWERS

JAVA STRING INTERVIEW QUESTIONS AND ANSWERS. FIND MOST FREQUENTLY ASKED JAVA STRING INTERVIEW QUESTIONS AND ANSWERS

15.Q. Can String be used in the switch case in Java?

Answer: Yes , String can be used in the switch case in Java. Example: public class StringSwitchCase{ public static void main(String args[] ) { String gift =”Apple Watch”; switch(gift ) { case “IPhone” : System.out.println(“gift is Iphone”); break; case “Car” : System.out.println(“Gift is car”); break; case “AppleWatch” : System.out.println(“Gift is apple watch”); break; default :

15.Q. Can String be used in the switch case in Java? Read More »

14.Q. Is it possible to compare Strings using the == operator? If so, what is the problem?

Answer: Yes, it is possible to compare two strings using == operator . If you compare two strings using == operator , it compares the reference or adress value, but don’t compare the actual content . If you use equals() method to compare two strings , then it will compare the actual content of two

14.Q. Is it possible to compare Strings using the == operator? If so, what is the problem? Read More »

13.Q.Difference between StringBuffer and StringBuilder

Answer : The basic difference between StringBuffer and StringBuilder is Thread safety and Synchronization . StringBuffer is Synchronized and thread-safe due to which multiple threads can safely use it . While StringBuilder is not Synchronized and so not thread-safe . StringBuffer is slower due to synchronization overhead. Whereas StringBuilder is fast as it avoids locking

13.Q.Difference between StringBuffer and StringBuilder Read More »

12.Q. What is the difference between String and StringBuffer?

Answer: A String object is immutable means its value cannot be changed after creation , whereas StringBuffer object is mutable , which means it’s characters or length can be changed directly without creating new memory objects . String uses the String Constant Pool for memory allocation , whereas StringBuffer directly use Heap Memory for memory

12.Q. What is the difference between String and StringBuffer? Read More »

11.Q.Why should you avoid using + for String concatenation in loops ?

You should avoid using the + operator for string concatenation inside loops because it triggers excessive memory consumption and quadratic O(n^2) time complexity. As Strings are immutable , they cannot be modified in place . Every single + operation copies the entire existing string into a brand-new memory location . Instead of using + in

11.Q.Why should you avoid using + for String concatenation in loops ? Read More »

7.Q. What is the difference between == & .equals() in Java Strings?

In Java == is a relational operator , whereas equals() is a method of String class. The == operator compares object references , whereas the .equals() method compares the actual text content present inside the string objects. The == operator can be used to compare primitive values like int or char, whereas equals() method cannot

7.Q. What is the difference between == & .equals() in Java Strings? Read More »