+ 5

I don't get the difference between long and short. When do I use long and when do I use short?

20th Sep 2016, 12:44 PM
Moritz Fotografie
Moritz Fotografie - avatar
4 Answers
+ 5
If the value that you are looking to use is larger (greater than 32,768 or less than -32,767) than what a short can hold, you will not be able to store it without loss of data. In this case, you would want to use the long value. If the value is between 32,767 and -32,768, then it will fit in a short. You are allowed to use either a short or a long int to store your value. Because both can store your value, you probably should use the short, as it will fit into a smaller region of memory. Using a long where a short will work is rarely the right option.
20th Sep 2016, 10:32 PM
Luke Kanuchok
Luke Kanuchok - avatar
+ 4
Short is 2 bytes long (can take values from -32,768 to 32,767). Long is 4 bytes long (can take values from -2,147,483,648 to 2,147,483,647).
20th Sep 2016, 3:08 PM
Zen
Zen - avatar
+ 2
it ultimately comes down to whether you will need to worry about the difference used in memory. Since modern computers are much more powerful, I honestly wouldn't stress over it unless you plan on declaring hundreds of thousands of variables.
20th Dec 2016, 3:53 AM
Andrew Michel
Andrew Michel - avatar
+ 1
Thx. How do I know, how long it could be?
20th Sep 2016, 3:14 PM
Moritz Fotografie
Moritz Fotografie - avatar