-1

Bellow is snippet of my code. I just want to know why if condition is not being executed even if variable "ans" has value 2020.

private static void solve() throws Exception {
    
        String str="20";
        String str1="20";
        String ans=str+str1;
    
        if(ans=="2020")
         System.out.println("matched");
       else System.out.println("Not matched");

        System.out.println(ans);
}

1 Answers1

1
ans.equals("2020")

this should work.

Valeamor
  • 53
  • 1
  • 6