0

I am inquiring about data types in Java.

public class Example {

    public static void main(String[] args) {

        string x = "String variable";

        int y = 4;

        System.out.println(x);
        System.out.println(y);      
    }
}

Why do I receive an error when I declare a string variable as all lowercase? I can declare integers and other data types all lowercase, but when I declare a string variable I must capitalize the "s" in "string"?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Robert Tossly
  • 613
  • 1
  • 6
  • 14

5 Answers5

4

int is a primitive type. String is class. By convention, Java class names start with an uppercase. Primitive types are all lowercase (there are only a few primitive types defined in Java). It's just the way types are named.

M A
  • 71,713
  • 13
  • 134
  • 174
  • Why is one a class though? Are they not both data types? I don't understand how a string variable must be a "class" and not a data type.... – Robert Tossly Sep 12 '15 at 15:05
  • Both `int` and `String` are data types. But one is a _primitive_ data type, and the other is a _reference_ data type (e.g. a class or interface). See http://stackoverflow.com/questions/8790809/whats-the-difference-between-primitive-and-reference-types and the Java tutorials. – M A Sep 12 '15 at 15:11
  • @RobertTossly This might helpful [how-can-a-string-be-initialized-using "";](:http://stackoverflow.com/questions/17489250/how-can-a-string-be-initialized-using/17489410#17489410) – Suresh Atta Sep 12 '15 at 15:14
  • I will be looking into these now. – Robert Tossly Sep 12 '15 at 15:15
2

Because Java is case sensitive and the class is String, not string.

Try Int y = 4;, that won't work either.

Primitive data types like int all start with lowercase, String isn't a primitive data type and thus is capitalized.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

integer in java is not a keyword nor a class name.

string neither.

Integer is a class name (wrapper class for integer variable).

int is a keyword (indicates an integer primitive data type).

String is a class name.

Luigi Cortese
  • 10,841
  • 6
  • 37
  • 48
0

Hi: a primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The String class is not technically a primitive data type, it is included Java API (You can read a little more about it, always useful!). So, the biggest difference between int and String, is only that one is a Java Class, and the other is not.

this is a list of all the primitives in Java (all in lower case):

Numeric primitives: short, int, long, float & double. Textual primitives: byte and char. Boolean and null primitives: boolean and null.

For the Java Api Classes, you can check this other post ->

Most-interest-classes

Finally, you can see another difference, I remember this one:

If you want a list of ints, you MUST type the list like this:

List<Integer> myInts = new ArrayList<Integer>();
myInts.add(1);
myInts.add(2);.......

I hope it helps you.

Community
  • 1
  • 1
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
0

Because it is a predefined class, and it is a common programming style to make the first letter of a class in uppercase only. And all primitive datatypes (like short,char,int,byte,float,double,long,boolean) programming style is to make first letter as small. It is default in java

Gaël J
  • 11,274
  • 4
  • 17
  • 32