Why is Java "String" type written in capital letter while "int" is not?
I am curious. Why do I have to type String myStr
with a capital letter whereas I type int aNumba
with a lower-case 开发者_如何学Cletter?
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
byte
vsByte
short
vsShort
long
vsLong
float
vsFloat
double
vsDouble
boolean
vsBoolean
char
vsCharacter
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
is a primitive data type, String
derives from Object
and is a class.
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".)
Because String
is a class (ie an object) and int
is not
see Java naming conventions for more infos.
It's just something that original Java designers imposed on us :-)
because int is a primitive type whereas String is an object type
By convention, java Objects have capitalized first-letter names (e.g. String), while primitives have lower case names (e.g. int, float, double, etc.)
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.
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!
精彩评论