Close
Close

Doubt in string declarations

   B Naveen Kumar , College

Hello sir, 

              I have a doubt regarding declaration of strings. May I know what's the difference between 

String s1 = "Welcome" ; ___(i) And

String s1 = new String("Welcome") ; ___(ii) 

Because when I compared two strings by the (i) format using == operator and equals() method it both  resulted in true. But when I compared both the strings using the (ii) format the == operator resulted in false whereas equals() method resulted true. Please explain me.

  • We can use == operators for reference comparison (address comparison) and . equals() method for content comparison. In simple words, == checks if both objects point to the same memory location whereas . equals() evaluates to the comparison of values in the objects.
    - Zupett1

Answers

Ask Yours
Post Yours
Write your answer