views:

256

answers:

10

Hi,

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?

+23  A: 

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

Péter Török
Are there reasons to use a class x over its corresponding primitive?
rFactor
@rFactor Yes. Imagine you'd like an List of int. Can't do it with standard Java API -- as primitives aren't subtypes of Object and are thus not eligible for generics, but you can get ArrayList<Integer>. Java (1.5+?) does some autoboxing to make it easier to use.
pst
+2  A: 

int is a primitive data type, String derives from Object and is a class.

bermuda
A: 

because int is a primitive type whereas String is an object type

DaVinci
+1  A: 

Because String is a class (ie an object) and int is not

see Java naming conventions for more infos.

RC
A: 

By convention, java Objects have capitalized first-letter names (e.g. String), while primitives have lower case names (e.g. int, float, double, etc.)

Chris Knight
A: 

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.

AndiDog
+2  A: 

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".)

pst
A: 

It's just something that original Java designers imposed on us :-)

Peter Štibraný
+3  A: 

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.

Codemwnci
And they say Java "is easy" ;-)
pst
to be fair to Java, if you had to import java.lang.String or had to do concatenation like new String("myString").append("concat").append("concat"), it would have been far more tedious. And by making it an object, it opens up the ability to perform method calls on it, like charAt, indexOf etc, things we take for granted, but probably couldn't do with out. I have to agree that the Java developers got this one right.
Codemwnci
A: 

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!

hisdrewness
Nobody's saying anything to contradict that int is primitive and String isn't. The capitalization, which is the subject of this question _is_ due to a convention, though. It's certainly legal to create a class called `string` in Java, but the convention is that class names are title cased, and primitives are not.
Yuliy
The point, and why yours and the others' comments are invalid, is because int is not a class, so you're comparing apples to oranges (hence primitives vs. classes). The capitalization convention ONLY APPLIES TO CLASSES, not primitives, therefore it's a moot point that does not apply.
hisdrewness