简体   繁体   中英

Why DataInput interface in Java doesn't have readUnsignedInt method

Java does not support unsigned integer types and the easiest way to represent an unsigned integer in Java is using a larger integer type.
DataInput interface does provide methods for reading unsigned integer types - readUnsignedByte , which returns short and readUnsignedShort , which returns int .

Why doesn't this interface have a readUnsignedInt method that would return long ? Is there any particular reason?

Clarification: I'm aware of Java 8 support for unsigned arithmetic. The question is not how to deal with this problem, but why the DataInput interface doesn't have such a method.

I don't know why, but it's no longer a problem because there is now support for unsigned ints. You can do

long a = Integer.toUnsignedLong(r.readInt());

DataInput/DataOutput has a method for each of the primitives and String.

unsigned int is not a primitive but you can do the following since it was added.

long a = dis.readInt() & 0xFFFF_FFFFL;

dos.writeInt((int) a);

Why this interface doesn't have a readUnsignedInt method that would return long?

I assume the original designers didn't think you need one or they would have added an unsigned int type.

Is there any particular reason?

There is so many things you could add (and in my library I have added because I think they are useful) but the designers seemed to believe less is more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM