What is the purpose of long, double, byte, char in Java?

Click Upvote picture Click Upvote · Jan 6, 2009 · Viewed 88.6k times · Source

So I'm learning java, and I have a question. It seems that the types int, boolean and string will be good for just about everything I'll ever need in terms of variables, except perhaps float could be used when decimal numbers are needed in a number.

My question is, are the other types such as long, double, byte, char etc ever used in normal, everyday programming? What are some practical things these could be used for? What do they exist for?

Answer

Neil Coffey picture Neil Coffey · Jan 6, 2009

With the possible exception of "short", which arguably is a bit of a waste of space-- sometimes literally, they're all horses for courses:

  • Use an int 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;
  • Use a double when you need fractional numbers and you've no reason to use anything else;
  • Use a char when you want to represent a character (or possibly rare cases where you need two-byte unsigned arithmetic);
  • Use a byte if either you specifically need to manipulate a signed byte (rare!), or when you need to move around a block of bytes;
  • Use a boolean when you need a simple "yes/no" flag;
  • Use a long 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);
  • Use a float for those rare cases where you either (a) are storing a huge number of them and the memory saving is worthwhile, or (b) are performing a massive number of calculations, 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! [*]
  • Use a short if you really need 2-byte signed arithmetic. There aren't so many cases...

[*] For example, in Hotspot on Pentium architectures, float and double operations generally take exactly the same time, except for division.

Don't get too bogged down in the memory usage of these types unless you really understand it. For example:

  • every object size is rounded to 16 bytes 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;
  • when passing parameters to a method, every type takes up 4 or 8 bytes on the stack: you won't save anything by changing a method parameter from, say, an int to a short! (I've seen people do this...)

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...!

Note that String isn't a primitive type, so it doesn't really belong in this list.