What is the purpose of long, double, byte, char in Java?

后端 未结 7 1423
误落风尘
误落风尘 2020-12-22 19:24

So I\'m learning java, and I have a question. It seems that the types int, boolean and string will be good for just about everything I

7条回答
  •  难免孤独
    2020-12-22 20:03

    I guess there are several purposes to types of that kind:

    1) They enforce restrictions on the size (and sign) of variables that can be stored in them.

    2) They can add a bit of clarity to code (e.g. if you use a char, then anyone reading the code knows what you plan to store in it).

    3) They can save memory. if you have a large array of numbers, all of which will be unsigned and below 256, you can declare it as an array of bytes, saving some memory compared with if you declared an array of ints.

    4) You need long if the numbers you need to store are larger than 2^32 and a double for very large floating point numbers.

提交回复
热议问题