JavaNewbie_M107
JavaNewbie_M107

Reputation: 2037

What is the null literal?

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?

Upvotes: 4

Views: 1312

Answers (7)

Ingo
Ingo

Reputation: 36339

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");

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109557

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.

Upvotes: 1

Stephen C
Stephen C

Reputation: 718836

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.

Upvotes: 11

edtheprogrammerguy
edtheprogrammerguy

Reputation: 6039

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.

Upvotes: 0

Colin D
Colin D

Reputation: 5661

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.

Upvotes: 0

m0skit0
m0skit0

Reputation: 25873

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.

Upvotes: 1

Andr&#233; Stannek
Andr&#233; Stannek

Reputation: 7863

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.

Upvotes: 2

Related Questions