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

1
  • EDIT: I know a integer variable is declared by int... not integer Commented Sep 12, 2015 at 15:04

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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....
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 stackoverflow.com/questions/8790809/… and the Java tutorials.
@RobertTossly This might helpful [how-can-a-string-be-initialized-using "";](:stackoverflow.com/questions/17489250/…)
I will be looking into these now.
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.

Comments

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.

Comments

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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.