为什么Java是“字符串”?类型以大写字母书写,而“int”则为不是吗?

发布于 2024-09-28 11:36:07 字数 84 浏览 3 评论 0原文

我很好奇。为什么我必须输入大写字母 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

温柔嚣张 2024-10-05 11:36:07

因为int是原始类型,而不是类,因此它不能直接与String进行比较。对应的类类型为 Integer ,根据类命名约定拼写。

类似的基元类型和类类型对是

  • byte字节
  • Long
  • floatFloat
  • double 与 < a href="http://download.oracle.com/javase/6/docs/api/java/lang/Double.html" rel="noreferrer">
  • booleanBoolean
  • char角色

Because int is 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.

Similar pairs of primitive and class types are

不一样的天空 2024-10-05 11:36:07

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.

老子叫无熙 2024-10-05 11:36:07

int 是一种原始数据类型,String 派生自 Object 并且是一个类。

int is a primitive data type, String derives from Object and is a class.

み零 2024-10-05 11:36:07

我也会加入这个聚会:这都是惯例

谢天谢地:(

class billybobstype {
    ...
}

这都是惯例。没有理由“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:

class billybobstype {
    ...
}

(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".)

痴骨ら 2024-10-05 11:36:07

因为String是一个类(即对象)而int

看不到Java 命名约定 了解更多信息。

Because String is a class (ie an object) and int is not

see Java naming conventions for more infos.

赢得她心 2024-10-05 11:36:07

因为 int 是原始类型,而 String 是对象类型

because int is a primitive type whereas String is an object type

悲凉≈ 2024-10-05 11:36:07

按照惯例,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.)

小帐篷 2024-10-05 11:36:07

基本的低级类型(例如 byteinteger)以小写形式命名,高级对象/类以大写形式命名(CamelCase)。这就是 Java 的设计方式。

Basic low-level types like byte or integer are named in lowercase, and high-level objects/classes are named uppercase (CamelCase). That's just the way Java was designed.

等待圉鍢 2024-10-05 11:36:07

这只是最初的 Java 设计者强加给我们的东西:-)

It's just something that original Java designers imposed on us :-)

溺渁∝ 2024-10-05 11:36:07

我简直不敢相信这个问题的一些答案!答案与惯例无关,如果你认为这就是答案,你需要做更多的研究。 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文