Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With the possible exception of "short", which arguably is a bit of a waste of space-- sometimes literally, they're all horses for courses:</p> <ul> <li>Use an <strong>int</strong> when you don't need fractional numbers and you've no reason to use anything else; on most processors/OS configurations, this is the size of number that the machine can deal with most efficiently;</li> <li>Use a <strong>double</strong> when you need fractional numbers and you've no reason to use anything else;</li> <li>Use a <strong>char</strong> when you want to represent a character (or possibly rare cases where you need two-byte unsigned arithmetic);</li> <li>Use a <strong>byte</strong> if either you specifically need to manipulate a <em>signed</em> byte (rare!), or when you need to move around a <em>block</em> of bytes;</li> <li>Use a <strong>boolean</strong> when you need a simple "yes/no" flag;</li> <li>Use a <strong>long</strong> for those occasions where you need a whole number, but where the magnitude could exceed 2 billion (file sizes, time measurements in milliseconds/nanoseconds, in advanced uses for compacting several pieces of data into a single number);</li> <li>Use a <strong>float</strong> for those rare cases where you either (a) are storing a <em>huge number</em> of them and the memory saving is worthwhile, or (b) are performing a <em>massive number of calculations</em>, and can afford the loss in accuracy. For most applications, "float" offers very poor precision, but operations can be twice as fast -- it's worth testing this on your processor, though, to find that it's actually the case! [*]</li> <li>Use a <strong>short</strong> if you really need 2-byte signed arithmetic. There aren't so many cases...</li> </ul> <p>[*] For example, in Hotspot on Pentium architectures, f<a href="http://www.javamex.com/tutorials/math/basic_operations.shtml" rel="noreferrer">loat and double operations generally take exactly the same time</a>, except for division.</p> <p>Don't get too bogged down in the memory usage of these types unless you <em>really</em> understand it. For example:</p> <ul> <li><strong>every object size is rounded to 16 bytes</strong> in Hotspot, so an object with a single byte field will take up precisely the same space as a single object with a long or double field;</li> <li>when passing parameters to a method, <strong>every type takes up 4 or 8 bytes on the stack</strong>: you won't save anything by changing a method parameter from, say, an int to a short! (I've seen people do this...)</li> </ul> <p>Obviously, there are certain API calls (e.g. various calls for non-CPU intensive tasks that for some reason take floats) where you just have to pass it the type that it asks for...!</p> <p>Note that String isn't a primitive type, so it doesn't really belong in this list.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload