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?
The String type is capitalized because it is a class, like Object , not a primitive type like boolean or int (the other types you probably ran across). As a class, the String follows the Naming Convention for Java proposed by Sun.
By convention, Java programs are written entirely in lower case characters with three exceptions. The first letter of class names are capitalized to distinguish class names from member names. The names of constant fields are written entirely capital letters.
Unfortunately, the String class in Java does not provide any method to capitalize string.
All keywords in java are in lowercase.
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
vs Byte
short
vs Short
long
vs Long
float
vs Float
double
vs Double
boolean
vs Boolean
char
vs Character
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With