3

I have the following statement in my java code

System.out.println(cName+" "+pName+" "+cName.equals(pName));

Output is

???????????????? ???????????????? false

The equals should return true right as the two strings have equal number of '?'. But I a m getting false

7
  • 6
    what is your platform encoding? Try to convert string to hex and compare hex representations stackoverflow.com/questions/923863/… Commented Sep 17, 2014 at 16:38
  • 6
    As @popalka points out, ? is often substituted for a character which cannot be displayed. It is not necessarily the character ?. Commented Sep 17, 2014 at 16:40
  • The String consists of ? symbols. Commented Sep 17, 2014 at 16:43
  • Did you manually declare the String to equal "????????????????" ? Commented Sep 17, 2014 at 16:44
  • where are cName and pName defined? What version of Java/ what OS are you running? If define the variables it does output true for me ... String a = "????????????????"; String b = "????????????????"; System.out.println(a+" "+b+" "+a.equals(b)); Commented Sep 17, 2014 at 16:46

2 Answers 2

8

Those 2 Strings might be equal in their "printed" form on your console, but their content is certainly not equal as proved by the return value of the String.equals().

They most likely contain characters which your console cannot display, therefore your console displays '?' characters for the "undisplayable" characters.

Another possibility could be they contain characters which when printed on the console have no visual appearance. Such characters may be the zero character ('\0') and control characters (whose code is less than 32) but this depends on the console how and what it displays.

Note: Even if you open the file where these Strings are stored or initialized and you see the same question marks, it is still possible that your editor is also not able to display the characters and the editor also displays question marks ('?') for undisplayable characters or for the characters with no visual appearance.

How to show the difference?

Iterate over the characters of the strings, and print them as int numbers where you will see the difference:

String s = "Test";
for (int i = 0; i < s.length(); i++)
    System.out.println((int) s.charAt(i));

Now if you see the same numbers printed, then yes, you can be sure they are the same, but then String.equals() would return true.

Sign up to request clarification or add additional context in comments.

3 Comments

The string consists of ? symbols. No special characters.
Added another possibility: special characters with no visual appearance (depends on the console).
Zero-width joiner and zero-width non-joiner, for instance.
0

Might be because of whitespace it is coming false

check for this :

System.out.println(cName+" "+pName+" "+(cName.trim()).equals(pName.trim()));

1 Comment

@NicePixel because I tried same thing and I am getting true

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.