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

user2133404
  • 1,816
  • 6
  • 34
  • 60
  • 6
    what is your platform encoding? Try to convert string to hex and compare hex representations http://stackoverflow.com/questions/923863/converting-a-string-to-hexadecimal-in-java – qwazer Sep 17 '14 at 16:38
  • 6
    As @popalka points out, `?` is often substituted for a character which cannot be displayed. It is not necessarily the character `?`. – Hot Licks Sep 17 '14 at 16:40
  • The String consists of `?` symbols. – user2133404 Sep 17 '14 at 16:43
  • Did you manually declare the String to equal "????????????????" ? – user2494817 Sep 17 '14 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)); – Birgit Martinelle Sep 17 '14 at 16:46
  • It reads from a XML file. The XML attribute has Question marks. ???????????????? ???????????????? – user2133404 Sep 17 '14 at 16:47
  • Based on this, you could have more special characters in there that don't display (refer to @icza answer below). Open your XML file in a hex editor if you wish. – user2494817 Sep 17 '14 at 16:50

2 Answers2

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.

icza
  • 389,944
  • 63
  • 907
  • 827
0

Might be because of whitespace it is coming false

check for this :

System.out.println(cName+" "+pName+" "+(cName.trim()).equals(pName.trim()));
Gautam Savaliya
  • 1,403
  • 2
  • 20
  • 31