为什么Java八进制转义只能达255? [英] Why do Java octal escapes only go up to 255?

查看:161
本文介绍了为什么Java八进制转义只能达255?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java语言规范指出,字符串内的转义是正常C,如 \\\
\t ,但它们还将 \0 中的八进制转义指定为 \377 。具体来说,JLS说:

The Java language specification states that the escapes inside strings are the "normal" C ones like \n and \t, but they also specify octal escapes from \0 to \377. Specifically, the JLS states:

OctalEscape:
    \ OctalDigit
    \ OctalDigit OctalDigit
    \ ZeroToThree OctalDigit OctalDigit

OctalDigit: one of
    0 1 2 3 4 5 6 7

ZeroToThree: one of
    0 1 2 3

尽管它在Java字符的范围内(因为Java字符不是字节),但是非法。

meaning that something like \4715 is illegal, despite it being within the range of a Java character (since Java characters are not bytes).

为什么Java有这个任意限制?您如何为255以外的字符指定八进制代码?

Why does Java have this arbitrary restriction? How are you meant to specify octal codes for characters beyond 255?

推荐答案

这可能是纯粹的历史原因,Java支持八进制转义序列。这些转义序列起源于C(或者也许在C的前辈B和BCPL)中,像PDP-7这样的计算机统治了地球,并且在组装或直接在机器代码中进行了大量编程,而八进制是首选的数字编写指令代码的基础,没有Unicode,只是ASCII,所以三个八进制数字足以表示整个字符集。

It is probably for purely historical reasons that Java supports octal escape sequences at all. These escape sequences originated in C (or maybe in C's predecessors B and BCPL), in the days when computers like the PDP-7 ruled the Earth, and much programming was done in assembly or directly in machine code, and octal was the preferred number base for writing instruction codes, and there was no Unicode, just ASCII, so three octal digits were sufficient to represent the entire character set.

在Unicode和Java出现的时候一直以来,八进制几乎都是以十六进制的形式作为首选数字基数,而十进制不会做。所以Java具有十六进制数字的 \u 转义序列。八进制转义序列可能被支持,只是为了使C程序员感到舒适,并且可以轻松地将C程序中的字符串常量复制到Java程序中。

By the time Unicode and Java came along, octal had pretty much given way to hexadecimal as the preferred number base when decimal just wouldn't do. So Java has its \u escape sequence that takes hexadecimal digits. The octal escape sequence was probably supported just to make C programmers comfortable, and to make it easy to copy'n'paste string constants from C programs into Java programs.

检查列出这些链接以获取历史琐事:

Check out these links for historical trivia:

http:// en。维基百科,自由的百科全书wikipedia.org/wiki/PDP-11_architecture#Memory_management

这篇关于为什么Java八进制转义只能达255?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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