System.out.println ("Hex fun:" + Long.toHexString (0x100000000L + 0xcafebabe));
I have the code above,
In Java, if the operand data type is different,
Do not do widening conversion.
long a = 10;
int b = 2;
a + b -> b is converted to long type.
Over there
Hexadecimal 0xcafebabe -> 32 bit int, because the left operand is of type logn, so it is extended and converted to sign. Incorrect operation value is displayed. Yeah, this is good.
The problem is that if you take a decimal number,
System.out.println (Integer.toHexString (-889275714)); // cafebabe
System.out.println (Long.toHexString (3405691582L)); // cafebabe -> extended
System.out.println (Integer.parseUnsignedInt ("cafebabe", 16)); // 3405691582 (QWORD)
Integer.decode ("0xcafebabe") causes an error.
I have a NumberFormat exception and I do not know why.
System.out.println (Integer.parseUnsignedInt ("cafebabe", 16)); -> This is how I handle it so I can output 32 bit decimal integer with sign.
As far as I know, Java primitives do not reduce the length of the data type depending on the operating system. The test environment worked on 64-bit Windows.
The JDK is version 8.