What is an empty literal?

I wrote 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) must be clearly false , since they are completely different String s. However, the result surprised me.

 This is x - . This is y - . false 

If these two String NOT equal, then how can they produce the same result?

+4
source share
7 answers

If these two lines are NOT equal, then how can they produce the same result?

They just look like the same result ... on the console!

If you redirected the console output to a file and looked at the hex dump tool, you probably 1 will find that they are different.

1 - We cannot be sure of this. It is also possible that the platform-encoded character encoding process maps the NUL to the SP by default. But a hex dump will clarify this.


By the way, null is what ordinary people mean “null literal” in Java. You have a String literal ... whose only character is the Unicode 0x0000 code point.

+11
source

Having the same output (optically) does not mean that the string consists of the same characters. For instance. the tab looks the same as four spaces. There are many characters in UTF-8 that look exactly the same, but are not.

This can also be applied only to the console view. Even if the null character should look different, your console might seem erroneous.

+2
source

The actual output of your Java program is

 This is x - This is y - . false 

EDIT: Please note that this can be a very operating system and console, because \ 0 is a line terminator in many technologies.

+1
source

I did this for 32 escape 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); } 

In addition to return, tab, line feed, and carriage return, the IDE console showed a space. A control was marked in the file. Thus, it is a matter of displaying a non-functional control character in space.

+1
source

The seams do not match, even if they look the same when printing on the console.

Printing to the console adds extra processing to the line. (converting bytes to pixels on the screen). In your case, it converts the \ 0 character to space.

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

0
source

What you see are the limitations of your console application. Binary 0 looks the same as the space character on the display. If you want to see the difference, you will have to look at the string dump.

0
source

How can the following data produce the same output even though the rows are not equal?

 System.out.println("x\bA"); System.out.println("y\bA"); System.out.println("A"); 
0
source

Source: https://habr.com/ru/post/1491121/


All Articles