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

Category: JAVA STRING INTERVIEW QUESTIONS AND ANSWERS

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 be used on primitives. The == operator is safe to use with null values without throwing errors , whereas if you use null values in equals() method , it will throw NullPointerException .

Leave a Reply

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