java int is 32 bits and long is 64 bits, so when you need to represent integers greater than 2 ^ 31, your long friend. For a typical example of using long, see System.currentTimeMillis ()
A byte is 8 bits and the smallest addressable object on most modern hardware, so it is necessary when reading binary data from a file.
The double size is twice the size of the float, so you usually use a double rather than a float unless you have size or speed limits and the float has enough capacity.
Short is two bytes, 16 bits. In my opinion, this is the least needed data type, and I really have not seen this in real code, but, again, it can be useful for reading binary file formats or for performing low-level network protocols. For example, ip port numbers are 16 bits.
Char is a single character, which is 16 bits. This is the same size as short, but short is signed (from -32768 to 32767), and char is unsigned (from 0 to 65535). (This means that the ip port number is probably more correctly represented as char than short, but this seems to be outside the intended area for characters ...)
For a truly authoritative source of this data, the java language specification .
Rolf Rander Jan 06 '09 at 9:37 2009-01-06 09:37
source share