为什么Java是“字符串”?类型以大写字母书写,而“int”则为不是吗?
我很好奇。为什么我必须输入大写字母 String myStr
而输入小写字母 int aNumba
?
I am curious. Why do I have to type String myStr
with a capital letter whereas I type int aNumba
with a lower-case letter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
因为
int
是原始类型,而不是类,因此它不能直接与String
进行比较。对应的类类型为Integer
,根据类命名约定拼写。类似的基元类型和类类型对是
byte
与字节
短
与短
长
与Long
float
与Float
double
与 < a href="http://download.oracle.com/javase/6/docs/api/java/lang/Double.html" rel="noreferrer">双
boolean
与Boolean
char
与角色
Because
int
is a primitive type, not a class, thus it is not directly comparable toString
. The corresponding class type isInteger
, spelled according to the class naming conventions.Similar pairs of primitive and class types are
byte
vsByte
short
vsShort
long
vsLong
float
vsFloat
double
vsDouble
boolean
vsBoolean
char
vsCharacter
String本身是一个从Object派生的类,而int是一个基元。
您的困惑可能来自于这样一个事实:String 在很多方面的行为都像原语,因为它具有可以应用于它的基本操作,例如 (+) 连接,并且不需要导入它。
连接是因为应用这个添加的操作已经足够基础了,即使它是一个对象类型。
之所以不需要导入,是因为默认导入了
java.lang
包,其中String是其中的成员。String itself is a class derived from Object, while int is a primitive.
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.
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.lang
package is imported, of which String is member.int
是一种原始数据类型,String
派生自Object
并且是一个类。int
is a primitive data type,String
derives fromObject
and is a class.我也会加入这个聚会:这都是惯例。
谢天谢地:(
这都是惯例。没有理由“String”不能是“string”;但是,“int”是一个关键字并且不仅仅是一个类名 - 其中 CamelCase 是约定但不是要求 - 因此它需要编译器修改:-)
编辑:顺便说一句,C#具有“string”关键字(语言语法的一部分),它是“System.String”类的别名。 (C# 也以这种方式实现“int”、“long”等作为别名,但它可以做到这一点是因为它有一个可扩展的“值类型”系统,而 JVM 只考虑/允许一小部分谨慎的“值类型”值类型”。)
I'll join the party too: It's all convention.
And thank-goodness:
(It's all convention. There is no reason why "String" couldn't have been "string"; however, "int" is a keyword and more than just a classname -- of which CamelCase is the convention but not a requirement -- so it would require a compiler modification :-)
Edit: As an aside, C# has the 'string' keyword (part of the language grammar) which is an alias for 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".)
因为
String
是一个类(即对象)而int
是看不到Java 命名约定 了解更多信息。
Because
String
is a class (ie an object) andint
is notsee Java naming conventions for more infos.
因为 int 是原始类型,而 String 是对象类型
because int is a primitive type whereas String is an object type
按照惯例,java 对象的首字母名称大写(例如 String),而基元的名称则小写(例如 int、float、double 等)。
By convention, java Objects have capitalized first-letter names (e.g. String), while primitives have lower case names (e.g. int, float, double, etc.)
基本的低级类型(例如
byte
或integer
)以小写形式命名,高级对象/类以大写形式命名(CamelCase)。这就是 Java 的设计方式。Basic low-level types like
byte
orinteger
are named in lowercase, and high-level objects/classes are named uppercase (CamelCase). That's just the way Java was designed.这只是最初的 Java 设计者强加给我们的东西:-)
It's just something that original Java designers imposed on us :-)
我简直不敢相信这个问题的一些答案!答案与惯例无关,如果你认为这就是答案,你需要做更多的研究。 int 是一个原始类型,而 String 是一个类(参见 Peter 的回答)。
原始与复杂的一个重要警告是自动装箱:
http:// /download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html
以下是原语的 Java 教程:
http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
string 类也是一种特殊情况:
http://www.javabeginner.com/learn-java/java-string- class
读懂人!
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).
An important caveat of primitive versus complex is autoboxing:
http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html
Here's the Java tutorial for primitives:
http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
The string class is a special case as well:
http://www.javabeginner.com/learn-java/java-string-class
Read up people!