Signed vs Unsigned in Java
🔹 Signed types store both negative and positive values.
Java default: all primitive integer types (byte, short, int, long) are signed, using 2’s
complement representation.
Type Bits Min Value Max Value
int 32 -2³¹ = -2,147,483,648 2³¹–1 = 2,147,483,647
long 64 -2⁶³ 2⁶³–1
🔹 Unsigned types store only non-negative values, but allow a larger positive range.
Java does not have explicit unsigned types for int or long, but starting from Java 8, it introduced
support for unsigned arithmetic via wrapper methods in the Integer and Long classes.
🔷 2. Why Use Unsigned?
Unsigned types:
Provide wider positive range (no need to reserve half of the range for negatives).
Useful when working with binary data, network protocols, file formats, or cryptography,
where negative values are meaningless.
Helps interoperate with C/C++ code using unsigned types.
🔷 3. How to Work with Unsigned int and long in Java?
Use wrapper class static methods in Integer and Long classes.
Method (Integer) Method (Long) Description
toUnsignedString(int) Converts to unsigned string
Long.toUnsignedString(long) (e.g., 4294967295 for -1)
parseUnsignedInt(String) Parses unsigned decimal
Long.parseUnsignedLong(String) number
compareUnsigned(int, int) Long.compareUnsigned(long, Compares two unsigned ints
long)
divideUnsigned(int, int) Long.divideUnsigned(long, long) Unsigned division
remainderUnsigned(int, Long.remainderUnsigned(long, Unsigned remainder
int) long)
🔷 4. Examples
public class UnsignedExample {
public static void main(String[] args) {
int signed = -1;
// Convert signed -1 to unsigned string
String unsignedStr = Integer.toUnsignedString(signed); // 4294967295
System.out.println("Unsigned string: " + unsignedStr);
// Parse unsigned string to int (returns -1, same bits)
int parsed = Integer.parseUnsignedInt("4294967295");
System.out.println("Parsed unsigned int: " + parsed); // -1
// Compare unsigned
System.out.println("Compare unsigned: " + Integer.compareUnsigned(-1, 1)); // > 0
// Divide unsigned
System.out.println("Divide unsigned: " + Integer.divideUnsigned(-1, 2)); // 2147483647
// Remainder unsigned
System.out.println("Remainder unsigned: " + Integer.remainderUnsigned(-1, 3)); // 0
char – The Only Unsigned Primitive in Java
✅ Summary:
char is a 16-bit unsigned integer type in Java.
Represents Unicode code points from 0 to 65535 (\u0000 to \uFFFF).
Always non-negative.
🧠 Internally:
char c = 65535;
System.out.println((int)c); // 65535
char is numeric at its core but used for characters.
You can safely cast it to int without sign extension.
🔷 2. float and double – Signed Only
📌 Key points:
Both are always signed.
Use IEEE 754 standard format.
Can represent:
o Positive and negative numbers
o Positive/negative zero
o Infinity (+∞, -∞)
o NaN (Not a Number)
⚠️Why No "Unsigned" Floats/Doubles?
Floating-point numbers already support both positive and negative values with a sign bit.
There's no practical use for unsigned floating-point types — the IEEE 754 standard doesn't
define such a format.
If you wanted to "treat" them as raw bits (for example, to extract the sign, exponent,
mantissa), you'd use methods like:
int bits = Float.floatToIntBits(f);
long bits = Double.doubleToLongBits(d);
These can help manipulate float/double at the binary level.