Why are there so many types of number in Java when long and double work every time?

后端 未结 3 1777
遥遥无期
遥遥无期 2021-01-24 07:34

Now I have been trying to learn Java Programming, I want to know why do we use things like Float, short, and int when we could be just be

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-24 07:51

    Great question, especially if you're coming from a language like JavaScript which does not make a distinction between types of numbers.

    Java is a bit more strict than those languages, and everything you write is first compiled to what is called byte code, which is sort of like assembly language, but it can only be read by the Java Virtual Machine (JVM). Because of this, you must specify exactly how many bits you need to represent your data. Even using a more abstract concept like a String, that still becomes code that the JVM can read which says exactly how many bits it represents.

    Here is how it breaks down in Java:

    • byte = 1 Byte, signed = 1 sign bit and 7 magnitude bits (Read more on Wikipedia)
    • short = 2 Bytes, signed = 1 sign bit and 15 magnitude bits
    • int = 4 Bytes, signed = 1 sign bit and 31 magnitude bits
    • long = 8 Bytes, signed = 1 sign bit and 63 magnitude bits
    • float = 4 Bytes, signed = 1 sign bit, 8 exponent bits, 23 mantissa bits (Read more on Wikipedia)
    • double = 8 Bytes, signed = 1 sign bit, 11 exponent bits, 52 mantissa bits
    • char = 2 Bytes, unsigned = 16 magnitude bits
    • boolean = 1 Byte, unsigned = 8 bits
    • boolean in an array of booleans = 1 nibble = 4 bits

    Note that these are all lower-case. This means that they are primitives, and, along with arrays, make up the building blocks of all data in Java.

    There are also these Title-Case classes that Java sometimes uses to wrap primitives (wrapping is when you use a class to represent an object inside the class, like buying a tablet and getting it in the box. You only care about the tablet, but you get the box around it to temporarily represent and hold it). These are as follows:

    • Byte wraps byte
    • Short wraps short
    • Integer wraps int
    • Long wraps long
    • Float wraps float
    • Double wraps double
    • Character wraps char
    • Boolean wraps boolean
    • Number acts as a generic number, and can be a Byte, Short, Integer, Long, Float, Double, or a custom number like BigInteger. You can even use this to make your own numbers!

    Summary

    You can definitely just use double and long for all your numbers! But, when you're dealing with so many numbers (remember: literally everything in Java boils down to numbers), you should use the smallest amount of data possible to accomplish a task, so you don't run out of memory.

    This is a practice that even such big companies as Google use, as we've seen when the view counter for Gangnam Style surpassed the limit of an int, showing that they preferred to use only a 32-bit number for the counter at first instead of a 64-bit one. Now that they need it, though, they updated. This is a practice I suggest you adopt!

提交回复
热议问题