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?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Because
String
is a class (ie an object) andint
is notsee Java naming conventions for more infos.
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.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
int
is a primitive data type,String
derives fromObject
and is a 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!
By convention, java Objects have capitalized first-letter names (e.g. String), while primitives have lower case names (e.g. int, float, double, etc.)