4

I have written the following program.

public class StringTest {
    public static void main(String[] args){
        String x = "\0";
        String y = " ";
        System.out.println("This is x - "+x+".");
        System.out.println("This is y - "+y+".");
        System.out.println(x.equals(y));
    }
}

Of course, x.equals(y) should clearly be false, as they are completely different Strings. However, the output surprised me.

This is x -  .
This is y -  .
false

If these two Strings are NOT equal, then how could they produce the same output?

JavaNewbie_M107
  • 2,007
  • 3
  • 21
  • 36

7 Answers7

11

If these two Strings are NOT equal, then how could they produce the same output?

They just look like the same output ... on your console!

If you redirected the console output to a file and examined with a hexadecimal dump tool, you will probably1 find that they are different.

1 - We can't be certain of that. It is also possible that the process of encoding the characters in the platform's default charset is mapping the NUL to a SP. But a hex dump will clarify this.


By the way, null is what people normal mean by "the null literal" in Java. What you have is a String literal ... whose only character is the Unicode code-point 0x0000.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    @arshajii - As I hinted ... it depends how his console displays the `NUL` character. – Stephen C Jul 12 '13 at 12:52
  • But would it ever display as a space? – arshajii Jul 12 '13 at 12:54
  • 1
    @arshajii - the OP says it does ... so there is evidence that it does, on some consoles. (And it is trivially true in that someone would write a console program with that behaviour.) – Stephen C Jul 12 '13 at 12:59
  • @arshajii don't confuse space with whitespace. If the console doesn't have any character to display (or just an "empty" one) it would appear the same as a space in a monospace font. Would be interessting to see how it looks in a non monospace font. – André Stannek Jul 12 '13 at 13:15
2

Having the same ouput (optically) doesn't mean that the string consists of the same chars. E.g. a tab looks the same as four spaces. In UTF-8 there are a lot of chars that look exactly the same but aren't.

This could also apply just to the representation on your console. Even if the null character should look differently your console might be showing it wrongly.

André Stannek
  • 7,773
  • 31
  • 52
1

The actual output of your Java program is

This is x - 
This is y -  .
false

EDIT: note this can be very OS and console dependent because \0 is string terminator in a lot of technologies.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
1

I did this for the 32 control characters + space:

    try (PrintWriter out = new PrintWriter("C:/ctl.txt")) {
        for (int i = 0; i <= 32; ++i) {
            String s = "" + (char)i;
            System.out.println("(char)" + i + " is '" + s + "'.");
            out.println("(char)" + i + " is '" + s + "'.");
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
    }

Apart from the backspace, tab, linefeed and carriage return, the IDE console showed a space. In the file the control characted were there. So it is a matter of displaying a non-functional control character to a space.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • If your console shows a space for backspace, it's broken. Also, ^G should beep. – Ingo Jul 12 '13 at 13:09
  • 1
    @Ingo thanks - forgotten. Pssst, I wanted to keep silent on the BEL, ^G. If web designers _hear_ from it... ;) Though BEL is also displayed as space. – Joop Eggen Jul 12 '13 at 13:22
0

The Stings are not the same, even if they look the same when printed to the console.

Printing to the console adds additional processing to the string. (converting the bytes to pixels on your screen). In your case it converts the \0 character to an space.

Take a look at the link posted by DrYap, ideone.com/wWEv2v. His output does not look the same as yours.

Colin D
  • 5,641
  • 1
  • 23
  • 35
0

What you are seeing is the limitations of your console app. Binary 0 looks the same as a space character on the display. If you want to see the difference, you would have to look at a memory dump of the strings.

edtheprogrammerguy
  • 5,957
  • 6
  • 28
  • 47
0

How can the following produce the same output, despite the strings are not equal?

System.out.println("x\bA");
System.out.println("y\bA");
System.out.println("A");
Ingo
  • 36,037
  • 5
  • 53
  • 100