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 using Long
and Double
?
I don't understand that part.
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 using Long
and Double
?
I don't understand that part.
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 bitsint
= 4 Bytes, signed = 1 sign bit and 31 magnitude bitslong
= 8 Bytes, signed = 1 sign bit and 63 magnitude bitsfloat
= 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 bitschar
= 2 Bytes, unsigned = 16 magnitude bitsboolean
= 1 Byte, unsigned = 8 bitsboolean
in an array of boolean
s = 1 nibble = 4 bitsNote that these are all lower-case. This means that they are primitives, and 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!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!
If you know the bounds of the data you're trying to hold then it makes sense to use the smallest possible primitive data type that can hold it. This is going to always be more memory efficient than allocating more memory than you need.
Welcome to Java programing. First of all Hit Insert key on keyboard to toggle out of block cursor.
The data types should not be thought of as amount of storage it consumes, but rather as the behavior it defines for variables and expressions of that type - Java comple reference
So by selecting a right data type you specify its behavior or bounds. Note: short, byte may not be memory efficient always as internaly they would most likey be converted to int during computation.