Getting Strange Output When Printing Result of a String Comparison

Getting strange output when printing result of a string comparison

You're missing a set of brackets:

System.out.println("Using == ::" + (s3==s4));

In your version, "Using == ::" + s3 is being compared via == to s4, which isn't what you want.

In general, + has a higher precedence than ==, which is why "Using == ::" + s3==s4 is being evaluated as ("Using == ::" + s3) == s4.

String not getting printed in the output

Indeed the output is "false" because first the concatenation is done so it is "Result: hello" and then it is compared to r2 ("hello") which returns false.
That's why you see "false" in console.

If you want to see "Result: true" you need to use equals instead of == because that's how we compare Strings in java. See below post to understand differences:
How do I compare strings in Java?

If you really want to compare them using "==" you need to add brackets so the comparison will be first before concatenation.

    String r1 = "hello";
String r2 = "hello";
System.out.println("Result: " + r1 == r2); // output: "false"
System.out.println("Result: " + r1.equals(r2)); // output: "Result: true"
System.out.println("Result: " + (r1 == r2)); // output: "Result: true"

Weird Java String comparison

Does one of your strings have a null character within it? These might not be visible when you use System.out.println(...).

For example, consider this class:

public class StringComparison {
public static void main(String[] args) {
String s = "a|b";
String t = "a|b\0";
System.out.println(":" + s + ":" + t + ":");
System.out.println(s.equals(t));
}
}

When I ran this on Linux it gave me the following output:


:a|b:a|b:
false

(I also ran it on Windows, but the null character showed up as a space.)

java println(a==b+is+a==b) prints false instead of a==b is true

A single false is printed because you didn't group your boolean expression.

The expression:

"a==b"+"is"+a==b

is evaluated as

("a==b"+"is"+a) == (b)

while you wanted it to do a string concatenation:

"a==b"+"is"+ (a==b)

That said, you shouldn't compare strings using ==, as others pointed out.

Practicing JUnit strange behavior comparing strings

Line seperators are platform dependant. For example, For Windows, Line Separator means \r\n

\r - carriage return
\n - new line

The problem is that the expected String has \n only while when the String gets generated using Scanner, it has \r\n. Due to this both strings were not equal.

To solve this issue, you can add platform independent line separator through

System.lineSeparator()

Using this will ensure, the code doesn't break in other operating systems.

System.out.println(); prints false with == operator

It prints false because of the relative precedence of + and ==. + has higher precedence, so it is equivalent to:

System.out.println((s1+ " "+ s2 +" "+ s1)==s2);

so the argument to System.out.println is a boolean, not a string.

Add parentheses:

System.out.println(s1+ " "+ s2 +" "+ (s1==s2));

S3==S4 is returning as false where both are pointing to same string in SCP ,please suggest why

a+b==c gets parsed as (a+b)==c, not a + (b==c). I.e, you need parentheses around the comparisons.



Related Topics



Leave a reply



Submit