47

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?

1
  • 9
    Why was this question down-voted, I wonder? It seems like a reasonable thing to ask, and not necessarily an easy thing to google.
    – Ben
    Jan 6, 2009 at 9:36

7 Answers 7

116

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.

4
  • 1
    Note that char only represents a character if all characters you use are inside the Basic Multilingual Plane (BMP) of Unicode. Once you leave the BMP, character handling in Java is very funky. No necessarily something you need to care about if you only handle western languages, but you should be aware of it.
    – sleske
    Dec 28, 2011 at 16:07
  • See e.g. this question & answer for an explanation: stackoverflow.com/questions/2533097/java-unicode-encoding
    – sleske
    Dec 28, 2011 at 16:07
  • The only advantage I can think of to byte and short primitives is that the compiler allows them to be stored directly byte[] and short[] arrays but will not allow an int to be stored without a typecast. Is there any other advantage?
    – supercat
    Sep 3, 2013 at 0:57
  • (or possibly rare cases where you need two-byte unsigned arithmetic) what? Oct 26, 2014 at 17:06
19

A java int is 32 bits, while a long is 64 bits, so when you need to represent integers larger than 2^31, long is your friend. For a typical example of the use of long, see System.currentTimeMillis()

A byte is 8 bits, and the smallest addressable entity on most modern hardware, so it is needed when reading binary data from a file.

A double has twice the size of a float, so you would usually use a double rather than a float, unless you have some restrictions on size or speed and a float has sufficient capacity.

A short is two bytes, 16 bits. In my opinion, this is the least necessary datatype, and I haven't really seen that in actual code, but again, it might be useful for reading binary file formats or doing low level network protocols. For example ip port numbers are 16 bit.

Char represents a single character, which is 16 bits. This is the same size as a short, but a short is signed (-32768 to 32767) while a char is unsigned (0 to 65535). (This means that an ip port number probably is more correctly represented as a char than a short, but this seems to be outside the intended scope for chars...)

For the really authorative source on these details, se the java language specification.

3
  • The whole short being signed and char being unsigned thing really bugs me. Please Mr. Java, just give me back my "unsigned" modifier for ints! :-) Jan 6, 2009 at 13:00
  • Brian: I'd prefer having byte, short and char non-numeric. Better having int and long non-numeric too, with a sensible arbitrary-sized numeric rational integer. Jan 6, 2009 at 14:02
  • One use case for the short type is arrays. If you need to process a lot of numbers in memory, and know that they will all fit in 16 bit, you can save 50 % of your RAM by using short[] instead of int[]. crazysquirrel.com/computing/java/speed_and_memory/…
    – markusk
    Jun 3, 2009 at 21:03
3

You can have a look here about the primitive types in Java.

The main interest between these types are the memory usage. For example, int uses 32bits while byte only uses 8bits.

Imagine that you work on large structure (arrays, matrices...), then you will better take care of the type you are using in order to reduce the memory usage.

3

I guess there are several purposes to types of that kind:

1) They enforce restrictions on the size (and sign) of variables that can be stored in them.

2) They can add a bit of clarity to code (e.g. if you use a char, then anyone reading the code knows what you plan to store in it).

3) They can save memory. if you have a large array of numbers, all of which will be unsigned and below 256, you can declare it as an array of bytes, saving some memory compared with if you declared an array of ints.

4) You need long if the numbers you need to store are larger than 2^32 and a double for very large floating point numbers.

3
  • please fix the point about doubles being needed for numbers greater than 2^32 Jan 6, 2009 at 10:21
  • oops - sorry. Fixed that now.
    – Ben
    Jan 6, 2009 at 12:00
  • double more useful for precise values; the magnitude thing is less important. Point (1) should be "causes overflow errors to be hidden" :). Jan 6, 2009 at 14:05
0

The primitive data types are required because they are the basis of every complex collection.

long, double, byte etc. are used if you need only a small integer (or whatever), that does not waste your heap space.

I know, there's enough of RAM in our times, but you should not waste it.

I need the "small ones" for database and stream operations.

0

Integers should be used for numbers in general.
Doubles are the basic data type used to represent decimals.
Strings can hold essentially any data type, but it is easier to use ints and is confusing to use string except for text.
Chars are used when you only wish to hold one letter, although they are essentially only for clarity.
Shorts, longs, and floats may not be necessary, but if you are, for instance, creating an array of size 1,00000 which only needed to hold numbers less than 1,000, then you would want to use shorts, simply to save space.

-1

It's relative to the data you're dealing with. There's no point using a data type which reserves a large portion of memory when you're only dealing with a small amount of data. For example, a lot of data types reserve memory before they've even been used. Take arrays for example, they'll reserve a default amount (say, 256 bytes <-- an example!) even if you're only using 4 bytes of that.

See this link for your answer

2
  • 1
    I was reading that just before asking this and didn't get it, so downvoted
    – Ali
    Jan 6, 2009 at 9:34
  • Overhead on arrays should be nowhere near that large. More in the order of 16 bytes. Jan 6, 2009 at 14:06

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.