简体   繁体   中英

Java data types

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

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM