Why is Java “String” type written in capital lette

2019-01-31 21:50发布

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?

10条回答
Explosion°爆炸
2楼-- · 2019-01-31 22:27

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

see Java naming conventions for more infos.

查看更多
淡お忘
3楼-- · 2019-01-31 22:31

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.

查看更多
萌系小妹纸
4楼-- · 2019-01-31 22:34

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

查看更多
再贱就再见
5楼-- · 2019-01-31 22:34

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

查看更多
ら.Afraid
6楼-- · 2019-01-31 22:36

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!

查看更多
迷人小祖宗
7楼-- · 2019-01-31 22:37

By convention, java Objects have capitalized first-letter names (e.g. String), while primitives have lower case names (e.g. int, float, double, etc.)

查看更多
登录 后发表回答