Why does toHexString have variable length?

0

Why is it that toHexString prints different strings in what appears to be very specific circumstances? Printing a number below 0x80000000 works just fine. Adding 1 to that value and printing it works fine. But assigning that value directly does not print the same thing, instead I have to add an L to the end.

My guess is that it has to do with the fact that numeric literals are of type int by default, but I don't know this happens at 0x80000000 and not when crossing over 0xffffffff for instance.

long a = 0x7FFFFFFF;
System.out.println(java.lang.Long.toHexString(a)); // prints 7fffffff

a++;
System.out.println(java.lang.Long.toHexString(a)); // prints 80000000


long b = 0x80000000;
System.out.println(java.lang.Long.toHexString(b)); // prints ffffffff80000000

b=0x80000000L;
system.out.println(java.lang.Long.toHexString(b)); // prints 80000000

P.S. Why doesn't oracle or tutorialspoint say anything about how methods are implemented? Where can I find the implementation of the standard libraries?

java
type-conversion
hex
asked on Stack Overflow Oct 14, 2018 by Deoxal

1 Answer

1

That has nothing to do with the toHexString method, it's all about int vs long in Java, and binary representation of signed integers.

When you write

long b = 0x7FFFFFFF;

and

long b = 0x80000000;

the literal number on the right is interpreted as an int (32 bits). Now, in the second case that value overflows the (signed) integer positive range, its binary represantion has a 1 in the leftmost position, and hence the number is understood as negative (-2147483648). Afterwards, in the assignment, it's promoted to a (negative) long, so the extra 32 bits are filled with ones. That's why you see those "extra" FFF...

If you don't know about the binary representation of signed integers in Java, read here

answered on Stack Overflow Oct 14, 2018 by leonbloy • edited Oct 14, 2018 by leonbloy

User contributions licensed under CC BY-SA 3.0