java 为什么Java“String”类型用大写字母写而“int”不是?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4006302/
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
Why is Java "String" type written in capital letter while "int" is not?
提问by Tower
I am curious. Why do I have to type String myStrwith a capital letter whereas I type int aNumbawith a lower-case letter?
我好奇。为什么我必须String myStr用大写字母输入而我int aNumba用小写字母输入?
回答by Péter T?r?k
Because intis a primitive type, not a class, thus it is not directly comparable to String. The corresponding class type is Integer, spelled according to the class naming conventions.
因为int是原始类型,而不是类,因此不能直接与String. 对应的类类型是Integer,根据类命名约定拼写。
Similar pairs of primitive and class types are
类似的原始类型和类类型对是
回答by Codemwnci
String itself is a class derived from Object, while int is a primitive.
String 本身是一个派生自 Object 的类,而 int 是一个原始类。
Your confusion probably comes from the fact that String behaves in many ways like a primitive, in such that it has basic operations that can be applied to it, like the (+) concatenation, and that it does not need to be imported.
您的困惑可能来自这样一个事实,即 String 在许多方面的行为都像一个基元,因为它具有可以应用于它的基本操作,例如 (+) 连接,并且不需要导入它。
The concatenation is because it is fundamental enough to have this added operation applied, even though it is an object type.
串联是因为它足以应用这个添加的操作,即使它是一个对象类型。
The reason it does not need to be imported, is by default the java.langpackage is imported, of which String is member.
它不需要导入的原因是默认情况下java.lang导入了包,其中 String 是成员。
回答by Codemwnci
I'll join the party too: It's all convention.
我也要参加聚会:都是约定俗成的。
And thank-goodness:
谢天谢地:
class billybobstype {
...
}
(It's all convention. There is no reason why "String" couldn'thave been "string"; however, "int" is a keywordand more than just a classname -- of which CamelCase is the convention but not a requirement-- so it would require a compiler modification :-)
(这都是约定。没有理由为什么“字符串”不能是“字符串”;然而,“int”是一个关键字,而不仅仅是一个类名——其中 CamelCase 是约定但不是必需的——所以它需要一个编译器修改:-)
Edit:As an aside, C# has the 'string' keyword (part of the language grammar) which is an aliasfor the 'System.String' class. (C# also implements 'int', 'long', etc. as aliases this way, but it can do this because it has an extensible "value type" system whereas the the JVM only considers/allows-for a small discreet set of "value types".)
编辑:顺便说一句,C# 有“string”关键字(语言语法的一部分),它是“System.String”类的别名。(C# 也以这种方式实现 'int'、'long' 等作为别名,但它可以这样做,因为它有一个可扩展的“值类型”系统,而 JVM 只考虑/允许 - 一小部分谨慎的“值类型”。)
回答by bermuda
intis a primitive data type, Stringderives from Objectand is a class.
int是一种原始数据类型,String源自Object并且是一个类。
回答by bermuda
Because Stringis a class (ie an object) and intis not
因为String是一个类(即一个对象)而int不是
see Java naming conventionsfor more infos.
有关更多信息,请参阅Java 命名约定。
回答by Chris Knight
By convention, java Objects have capitalized first-letter names (e.g. String), while primitives have lower case names (e.g. int, float, double, etc.)
按照惯例,java 对象的首字母名称大写(例如 String),而原语名称则小写(例如 int、float、double 等)。
回答by AndiDog
Basic low-level types like byteor integerare named in lowercase, and high-level objects/classes are named uppercase (CamelCase). That's just the way Java was designed.
基本的低级类型(如byte或 )integer以小写命名,高级对象/类以大写(CamelCase)命名。这就是 Java 的设计方式。
回答by DaVinci
because int is a primitive type whereas String is an object type
因为 int 是原始类型而 String 是对象类型
回答by Peter ?tibrany
It's just something that original Java designers imposed on us :-)
这只是原始 Java 设计师强加给我们的东西:-)
回答by hisdrewness
I can't believe some of the answers to this question!! The answer does not pertain to convention, and if you think that's the answer you need to do some more studying. int is a primitive type whereas String is a class (see Peter's answer).
我无法相信这个问题的一些答案!!答案与惯例无关,如果您认为这是答案,则需要进行更多研究。int 是一种原始类型,而 String 是一个类(请参阅 Peter 的回答)。
An important caveat of primitive versus complex is autoboxing:
原始与复杂的一个重要警告是自动装箱:
http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html
http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html
Here's the Java tutorial for primitives:
这是原语的Java教程:
http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
The string class is a special case as well:
string 类也是一个特例:
http://www.javabeginner.com/learn-java/java-string-class
http://www.javabeginner.com/learn-java/java-string-class
Read up people!
读书人!

