Java 是否区分大小写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2128459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is Java case-sensitive?
提问by giri
I read somewhere that Java is case-sensitive. I have been unable to confirm this. Is it? If so, why?
我在某处读到 Java 区分大小写。我一直无法证实这一点。是吗?如果是这样,为什么?
回答by Paul Wagland
Yes, it is case-sensitive. It is this way because of its heritage from C. To keep the language more familiar to what people were used to "in the day", they left it as case-sensitive. There is an added advantage, since Java identifiers can be almost any Unicodecharacter. You don't have to worry about whether or not an individual character can be uppercased or not.
是的,它区分大小写。之所以这样是因为它从 C 继承而来。为了使该语言更熟悉人们在“白天”所习惯的内容,他们将其保留为区分大小写。还有一个额外的优势,因为 Java 标识符几乎可以是任何Unicode字符。您不必担心单个字符是否可以大写。
For example, should ?
be equivalent to ss
?
例如,应该?
等价于ss
?
回答by Tobu
Identifiers are case-sensitive because collation is a difficult, locale-dependent problem.
标识符区分大小写,因为整理是一个困难的、依赖于语言环境的问题。
回答by Alex Baranosky
Yes. Java is case-sensitive because most programming languages are!
是的。Java 区分大小写,因为大多数编程语言都是!
回答by Kaleb Brasee
Java is case-sensitive because it uses a C-style syntax. Case sensitivity is useful because it lets you infer what a name means based on it's case.
Java 区分大小写,因为它使用 C 风格的语法。区分大小写很有用,因为它可以让您根据大小写推断名称的含义。
For example, the Java standard for class names is uppercasing the first letter of each word (Integer
, PrintStream
, etc). The standard for variable and method names is lowercase the first letter of the first word, and uppercasing all other first letters (number
, println()
, checkError()
, etc). And the standard for constants is all uppercase with underscores (SOME_CONSTANT
).
例如,对于类名的Java标准uppercasing每个单词(的第一个字母Integer
,PrintStream
等等)。变量和方法名的标准是小写的第一个单词的第一个字母,uppercasing所有其他第一个字母(number
,println()
,checkError()
等)。常量的标准是全部大写并带有下划线 ( SOME_CONSTANT
)。
While you're not required to follow these rules in your code (it'll compile even if you break these rules), Java's built-in code follows them, and all major Java libraries do too. And your code really should follow these rules too, because it helps other developers infer meaning when they see the capitalization.
虽然您不需要在代码中遵循这些规则(即使您违反这些规则,它也会编译),但 Java 的内置代码遵循这些规则,所有主要的 Java 库也遵循这些规则。而且您的代码也确实应该遵循这些规则,因为它可以帮助其他开发人员在看到大写时推断其含义。
回答by BalusC
This was more a technical decision. Java is intented to be platform independent. Public Java classes are stored with the package/classname in the filesystem. In non-Windows platforms the filenames are case sensitive. It would fail over there if you didn't use an exact case to load/run the class. This need for case sensitivity is extended to other identifiers in Java (which in end yields room for other (but non-technical) advantages like naming conventions).
这更像是一个技术决定。Java 旨在独立于平台。公共 Java 类与文件系统中的包/类名一起存储。在非 Windows 平台中,文件名区分大小写。如果您没有使用确切的大小写来加载/运行类,它就会在那里失败。这种区分大小写的需求扩展到 Java 中的其他标识符(这最终为命名约定等其他(但非技术)优势留出了空间)。
回答by Mark R
The Java compiler and interpreter are case-sensitive, so you must capitalize consistently.
Java 编译器和解释器区分大小写,因此您必须一致地大写。
HelloWorldApp is not the same as Helloworldapp.
HelloWorldApp 与 Helloworldapp 不同。
This case sensitivity reflects Java's heritage as an outgrowth of C and C++.
这种区分大小写反映了 Java 作为 C 和 C++ 的产物的传统。
回答by Michael Borgwardt
I read somewhere that Java is case-sensitive. I have been unable to confirm this.
我在某处读到 Java 区分大小写。我一直无法证实这一点。
Java source code is case sensitive, if you mean that. i.e. Double
is not the same type as double
, and you can have two different and separate variables myData
and mydata
.
Java 源代码区分大小写,如果你是这个意思的话。ieDouble
与 的类型double
不同,您可以有两个不同且独立的变量myData
和mydata
。
Is it? If so, why?
是吗?如果是这样,为什么?
Case sensitivity is the norm in most programming languages and environments, because lower and upper case letters are represented differently at the lowest levels. To a computer, "a" and "A" are two completely different things, and it takes extra work to make it act as if they were the same.
区分大小写是大多数编程语言和环境的规范,因为在最低级别上小写和大写字母的表示方式不同。对于计算机来说,“a”和“A”是两个完全不同的东西,需要额外的工作才能让它表现得好像它们是一样的。
Furthermore, some languages have very tricky special rules for casing, e.g. the German letter ? has no uppercase version and is typically uppercased to "SS" - so should "wei?" and "WEISS" be considered syntactially identical? Even worse is Turkish: they have two separate letters i with and without a dot, and each has its own uppercase version. So in Turkey, "IMAGE" is notthe uppercase version of "image"! And this is not irrelevant at all, especially for Java, since you can actually use all these letters as identifiers in your Java programs if you want.
此外,某些语言对大小写有非常棘手的特殊规则,例如德语字母 ? 没有大写版本,通常大写为“SS” - 那么“wei”应该吗?和“WEISS”在语法上是否相同?更糟糕的是土耳其语:它们有两个单独的字母 i 带点和不带点,每个都有自己的大写版本。所以在土耳其,“IMAGE”不是“image”的大写版本!这一点也不是无关紧要的,尤其是对于 Java,因为如果需要,您实际上可以在 Java 程序中使用所有这些字母作为标识符。
In the light of all this, it's very understandable that programming language designers would choose the simple solution of having the syntax be case sensitive.
鉴于所有这些,编程语言设计者会选择让语法区分大小写的简单解决方案,这是可以理解的。
回答by Norvik
Whether a programming language should be case sensitive or not tends to divide opinion amongst programmers. Regardless of the debate it's important to remember that Java is case sensitive. It simply means that the case of the letters in your Java programs matter. For example, suppose you decide to create three variables called "endLoop", "Endloop", and "EnDlOoP". Even though in English we see the variable names as saying the same thing, Java does not. It will treat them all differently.
编程语言是否应该区分大小写往往会在程序员之间产生分歧。不管争论如何,重要的是要记住 Java 区分大小写。它只是意味着 Java 程序中字母的大小写很重要。例如,假设您决定创建三个变量,分别称为“endLoop”、“Endloop”和“EnDlOoP”。尽管在英语中我们看到变量名说的是同一件事,但 Java 没有。它将以不同的方式对待他们。