为什么\0在java中的不同系统中打印不同的输出 [英] Why does \0 print different output in different system in java

查看:56
本文介绍了为什么\0在java中的不同系统中打印不同的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在不同的系统中打印了不同的输出.

The below code printed different output in different system.

String s = "hello?vsrd".replace('?', '\0');
System.out.println(s);

当我在我的系统(Linux Ubuntu、Netbeans 7.1)中尝试时,它打印:

When I tried in my system (Linux Ubuntu, Netbeans 7.1), it printed :

当我在另一个系统(Linux Mint、Netbeans 7.1)中尝试相同的代码时,它打印为

When I tried the same code in another system (Linux Mint, Netbeans 7.1) it printed as

我确实理解 \0 在 Java 中充当字符(通过引用这个答案 显然 s.length() 打印 10).并且,不可打印的字符可能会像这样打印(框).但是,为什么这只适用于某些系统?是jdk版本还是操作系统的区别?我不想要替代代码,但想知道为什么会这样.你可以考虑 String s = "hello\0vsrd";.

I do understand that \0 acts as a character in java (by referring this answer and obviously s.length() prints 10). And, non-printable characters may be printed like this (box). But, why this behaves only on some systems? is it the difference of jdk version or OSs? I don't want an alternative code, but want to know why exactly it happens so. You can just consider String s = "hello\0vsrd";.

推荐答案

\0 是一个不可打印的字符.

\0 is a non-printable character.

此外,在 C 中,字符串传统上以 \0 结尾,因为它们没有明确存储它们的长度.有些程序会在到达该字符时停止打印.

Additionally, in C, strings are traditionally terminated with \0, as they don't explicitly store their lengths. Some programs will simply stop printing when they reach that character.

Netbeans 控制台会打印它,但字体可能没有字形,甚至没有正方形,这可能解释了不同的行为.

The Netbeans console does print it, but the font might not have a glyph, not even the square, which might explain the different behavior.

这篇关于为什么\0在java中的不同系统中打印不同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆