In Java, when one declares a string variable the word "String" is capitalized, yet it isn't in any of the other types I've run across (e.g. "int" or "double"). Why is this? Was it just some weird arbitrary decision by the designers?
For variables, the Java naming convention is to always start with a lowercase letter and then capitalize the first letter of every subsequent word. Variables in Java are not allowed to contain white space, so variables made from compound words are to be written with a lower camel case syntax.
Do not capitalize effects or variables unless they appear with multiplication signs. Many authors confuse these terms (factor, variable, and effect), and if you are uncertain about whether the author used them correctly, it is best to query.
Java - String toUpperCase() Method This method has two variants. The first variant converts all of the characters in this String to upper case using the rules of the given Locale. This is equivalent to calling toUpperCase(Locale.
Why does is declaration of a string variable in Java capitalized?
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. In short, that coding style dictates that UpperCamelCase be used for classes ("Class names should be nouns, in mixed case with the first letter of each internal word capitalized") and lowerCamelCase be used for instances and methods.
What's the basic difference between String
and the primitive types?
As an object, a String
has some advantages, like properties and methods that can be called directly to them (like the famous length()
, replace()
and split()
). None of the primitive types have that.
What about wrapper classes?
The other primitive types have equivalent wrapper classes, like Integer
for int
and Boolean
for boolean
. They will allow you additional functions.
Since Java 1.5, the conversion from an int
to an Integer
is made almost seamlessly. This is called autoboxing.
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