0

I was trying to reverse a String and then compare it with first string. When I print my variables A and C for the value "madam" gets the same value, but when I try to do a boolean evaluation, it returns false. I don't know why.

public class Random_practise {
    public static  void main (String[] args){
        String A ="madam";
        int i = A.length() - 1;
        char B[] = new char[A.length()];
        for (int k = 0; k<A.length(); k++ ){
                B[k] = A.charAt(i);
                i--;
        }
        String C = valueOf(B);
        if (A == C){
            System.out.println("Yes");
        }
        else{
            System.out.println("No");
        }
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – maloomeister Aug 18 '21 at 09:23
  • Also, your code currently doesn't compile, so you actually shouldn't be getting any output at all. `String C = valueOf(B)` should be `String C = String.valueOf(B)`. – maloomeister Aug 18 '21 at 09:27
  • @maloomeister unless there is a static import – Stultuske Aug 18 '21 at 11:18
  • @Stultuske you're right, but since the imports are not included in the snippet, I figured the hint wouldn't hurt. – maloomeister Aug 18 '21 at 11:47

0 Answers0