Printing the Original long Value
To print the original long
value in Java, you can use the System.out.println()
method. Here's an example:
long originalValue = 9223372036854775807L;
System.out.println("The original long value is: " + originalValue);
Output:
The original long value is: 9223372036854775807
Alternatively, you can use the System.out.format()
method to print the long
value with a specific format:
long originalValue = 9223372036854775807L;
System.out.format("The original long value is: %d%n", originalValue);
Output:
The original long value is: 9223372036854775807
The %d
format specifier is used to represent a decimal integer value.
Printing long
Values in Different Bases
You can also print long
values in different number bases, such as binary, octal, or hexadecimal. Here's an example:
long originalValue = 9223372036854775807L;
System.out.println("Binary representation: " + Long.toBinaryString(originalValue));
System.out.println("Octal representation: " + Long.toOctalString(originalValue));
System.out.println("Hexadecimal representation: " + Long.toHexString(originalValue));
Output:
Binary representation: 111111111111111111111111111111111111111111111111111111111111111
Octal representation: 1777777777777777777777
Hexadecimal representation: 7fffffffffffffff
The Long.toBinaryString()
, Long.toOctalString()
, and Long.toHexString()
methods are used to convert the long
value to its binary, octal, and hexadecimal representations, respectively.
In the next section, we'll explore how to print the reversed long
value in Java.