+ 1
What is the range of jave variable?
is it same as c/c++ or different and does it follow also the cyclic pattern as of c
7 Réponses
+ 11
Primitive types summary (Java)
+-----------+-------+--------+--------+
| types | size | min | max. |
| | (bits) | value | value |
+-----------+-------+--------+--------+
| byte | 8 | -128 | 127 |
+-----------+-------+--------+--------+
| char | 16 | 0 | 2^16 |
| | | | - 1 |
+-----------+-------+--------+--------+
| short | 16 | -2^15 | 2^15 |
| | | | - 1 |
+-----------+-------+--------+--------+
| int | 32 | -2^31 | 2^31 |
| | | | - 1 |
+-----------+-------+--------+--------+
| long | 64 | -2^63 | 2^63 |
| | | | - 1 |
+-----------+-------+--------+--------+
| float | 32 | 2^ | (2 - 2^ |
| | | -148 | -23) * |
| | | | 2^127 |
+-----------+-------+--------+--------+
| double | 64 | 2^ | (2 - 2^ |
| | | -1074 | -52) * |
| | | | 2^1023|
+------------+------+--------+--------+
| boolean | 1 | -- | -- |
+------------+------+--------+--------+
| void | -- | -- | -- |
+------------+------+--------+--------+
+ 10
If you mean wrapping around and overflow, then yes, they do.
In C/C++ we have signed/unsigned type modifier for integral types (e.g. signed char or unsigned int). As you know, every type has a specific range defined by the platform in which you are programming (e.g. in a desktop machine, the short type normally has 16 bits while in a sort of microcontroller designed for a specific purpose, it might have 32 bits ). When you perform a bunch of arithmetic operations and then assign the result to a type which doesn't have the capacity to handle that, two scenarios may happen:
Scenario 1: for unsigned variables wrap around/overflow
// ...
#define BIN_8(x) bitset<8>(x).to_string()
int main() {
// get the platform size of unsigned char type
cout << sizeof(unsigned char) * CHAR_BIT << " bits\n";
// declare and initialize an unsigned char type with its maximum possible range
// In a 32-bit Win machine unsigned char mostly has a range of [0, 255]
unsigned char uc = numeric_limits<unsigned char>::max(); // 255
cout << "Max of uc is " << +uc << " --BIN-> " << BIN_8(uc) << endl;
uc++; // 255 + 1 = 0 which is a WRAP AROUND (a proper anology would be the way a clock works or an odometer) and create a CYCLIC PATTERN.
cout << "Max of uc + 1 is " << +uc << " --BIN-> " << BIN_8(uc) << endl;
}
Scenario 2: for signed variables overflow (and not wrap around)
// ...
// declare and initialize a signed char type with its maximum possible range
// In a 32-bit Win machine signed char mostly has a range of [-128, 127]
signed char sc = numeric_limits<signed char>::max(); // 127
sc++; // 127 + 1 = -128 which is an overflow
//...
But, in Java, there's no such thing called "unsigned". So, you almost always will face with arithmetic OVERFLOW which is considered an UNDEFINED behavior which is driven by the compiler.
Live Link to above snippet [http://cpp.sh/4gff6]
More info [https://www.sololearn.com/Discuss/701288/c-bread-butter-part-2-1]
+ 6
What do you mean by cyclic pattern?
+ 1
after incrementing a variable having its upper bound value gives its lower bound value then it follow a cyclic pattern
so does java variable also follow this
+ 1
in addition to @babak's answer for java's datatypes:
although they have different sizes like 8bit for byte and so on, they all get processed internally using 32bit integers (64bit double for float) by the jvm. so there is pretty much no reason to use them to save storage.
0
is it also follow cyclic pattern?