Quote:
Originally Posted by HelloWorld I personally still don't get of why do you ask of "When to use one variables over another?"  Because I'd say that their functionalities are different. Well, there are some of them those are similar, though, they have different sizes. So, it's either on how do you want to use the variable for, OR if you need more size for whatever information to be stored in a variable  |
In Java, yes, that is guaranteed. A quick comparison of what is comparable on my machine (format: Java = C/C++) -
boolean = _Bool/bool
byte = char
char = wchar_t
short = short int
int = long int (note that on my machine, a C/C++ int = a C/C++ long int, but a C/C++ int = a C/C++ short int on some other machines)
long = long long (C99 only, not valid with C++98)
As Bench wrote, C++ only guarantees that a char is at least 1 byte in size and each integral data type is at least the same size. The same is true for the real/floating-point types. Honestly, a char in C++ could be the same size as a long int, meaning that your long int would only be able to hold -128 to 127 (signed) or 0 to 255 (unsigned). Also, a char is often an int with a restricted range. As such, int is often preferred because using char causes conversion to take place at runtime, which can affect performance somewhat, depending on the efficiency of the instructions used for the conversion and the machine's abilities itself.
Quote:
Originally Posted by TeraTask I'm afraid I take a far more simplistic approach. In whatever language I'm working with, I go to the documentation and print out the minimum and maximum values. Then, when choosing a type, I choose the type that uses the smallest number of bytes and covers the entire range of possible values. |
Yeah, that's nice to do, but in some languages, that can cause performance issues, as I said previously. The only reason they exist is for convenience IMHO. That way, a programmer doesn't need to "reinvent the wheel" by doing range checking herself/himself. In some languages however, no performance issues are noticed. For example, one can calculate 65536^256 in Python rather quickly to EXACT PRECISION. Often a program in certain languages such as Common Lisp, Perl and Python are amazingly fast compared to the equivalent program in the simpler, more machine-based C or C++ program. Of course, they were designed to be that way.