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

So, I am learning java and I have a question. It seems that the int , boolean and string types will be useful for almost everything that I will ever need in terms of variables, except maybe float can be used when decimal numbers are needed in a number.

My question is whether other types are like long , double , byte , char , etc. ever used in ordinary, everyday programming? What practical things can they use? Why do they exist?

+41
java variables types language-features
Jan 6 '09 at 9:27
source share
7 answers

With the possible exception of β€œshort ones,” which may be a waste of space β€” sometimes literally, they are all horses for courses:

  • Use int when you don't need fractional numbers and you have no reason to use anything else; on most processors / OS configurations, this is the size of the number that the machine can handle most effectively;
  • Use double when you need fractional numbers and you have no reason to use anything else;
  • Use char when you want to represent a character (or perhaps rare occasions when you need double-byte unsigned arithmetic);
  • Use a byte if you just need to manipulate a signed byte (rarely!) Or when you need to move around a block of bytes,
  • Use boolean when you need a simple yes / no flag;
  • Use long for those cases when you need an integer, but where the value can exceed 2 billion (file sizes, time measurements in milliseconds / nanoseconds, in extended use to compress several parts of data into one number);
  • Use float for those rare cases when you either (a) store a huge number of them, or save memory, or (b) do a lot of calculations and can afford to lose accuracy. For most applications, "float" offers very low accuracy, but operations can be up to twice as fast - it’s worth checking it on your processor, however, to find that it really is! [*]
  • Use short if you really need 2-byte arithmetic. There are not many cases ...

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

Do not use too many errors in using these types of memory if you do not understand this. For example:

  • the size of each object is rounded to 16 bytes in Hotspot, so an object with one byte field will occupy exactly the same space as one object with a long or double field;
  • when passing parameters to a method, each type takes 4 or 8 bytes in the stack : you will not save anything by changing the method parameter, for example, using int to a short one! (I saw people doing it ...)

Obviously, there are certain API calls (for example, various calls for non-intensive tasks that for some reason take a float), where you just need to pass it the type that it requests ...!

Note that String is not a primitive type, so it really does not belong to this list.

+100
Jan 06 '09 at 13:57
source share

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 .

+18
Jan 06 '09 at 9:37
source share

You can look here about primitive types in Java.

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

Imagine that you are working with a large structure (arrays, matrices ...), then you better take care of the type that you use to reduce memory usage.

+3
Jan 06 '09 at 9:33
source share

I assume that there are several goals for types of this type:

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

2) They can add a little clarity to the code (for example, if you use char, then anyone who reads 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 to if you declared an array of ints.

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

+3
Jan 6 '09 at 9:35
source share

Primitive data types are required since they are the basis for each complex set.

long, double, byte etc. used if you only need a small integer (or something else) that will not let your heap space go.

I know there is enough RAM in our time, but you should not waste it.

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

0
Jan 06 '09 at 9:32
source share

Integers should be used for numbers in general.
Bilocal is a basic data type used to represent decimal places.
Strings can contain essentially any type of data, but it's easier to use ints and confuse the use of strings, except for text.
Balls are used when you want to keep only one letter, although they are essentially just for clarity.
Shorts, longs and float may not be needed, but if you, for example, create an array of 1.00000 in size, which should contain only numbers less than 1000, then you would like to use shorts, just to save space.

0
Dec 15 '16 at 19:56
source share

This is relative to the data you are dealing with. It makes no sense to use a data type that reserves most of the memory when you are dealing with only a small amount of data. For example, many data types retain memory until they are used. For example, take arrays, they will reserve the default value (say 256 bytes <- an example!), Even if you use only 4 bytes.

See this link for an answer

-one
Jan 6 '09 at 9:32
source share



All Articles