Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 letter?

like image 1000
Tower Avatar asked Oct 23 '10 22:10

Tower


People also ask

Why does string start with capital in Java?

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.

Why do we use capital letters in Java?

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.

Should string be capitalized in Java?

Unfortunately, the String class in Java does not provide any method to capitalize string.

Can a keyword of Java begin with capital letter?

All keywords in java are in lowercase.


2 Answers

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
like image 138
Péter Török Avatar answered Sep 22 '22 13:09

Péter Török


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.

like image 39
Codemwnci Avatar answered Sep 18 '22 13:09

Codemwnci