0

why '==' operator is not used to compare two strings in java.

what is the difference between equals() method and '==' operator while comparing two string? Here is the piece of code

public class Solution {
    public int numDistinct(String A, String B) {
        int ans =0;
        if(A==B)
         ans = 1;
        return ans;
    }
}

but ans is 0, why?

Shukla JI
  • 1
  • 1
  • 'Why is it not...' is answered 'because the language is not designed that way'. The technical rationale is in the answer by @Foolek – dangling else May 14 '22 at 12:19

0 Answers0