0

This outputs-BPBPfalse.

I am really confused as to why the boolean is false when the string obviously is the exact same.

Why isnt it BPBPtrue?

Thanks in advance!

public class mySplit {

public static void main(String myString[])
{
    String myString1= "BP+Car+Bird";
    String myArray[] = {"BP","Car","Bird"};

    String myArray2[]= myString1.split("\\+");
    String A = myArray[0];
    String B = myArray2[0];

    System.out.print(A);
    System.out.print(B);

    boolean One = (A==B);
    System.out.print(One);

}

}

OUTPUT - BPBPfalse

0

1 Answer 1

2
A==B

needs to be

A.equals(B)

because they are different objects.

String one = "one";
String two = one;
boolean isEqual = one == two; // TRUE
boolean notTheSameObject = ("one" == one); // FALSE
boolean areEqual = (one.equals("one")); // TRUE String.equals
Sign up to request clarification or add additional context in comments.

2 Comments

thank you so much.... you have no idea how much pain that had cause me.... haha thanks
If the answer solved your problem, you should flag it as the correct answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.