简体   繁体   中英

Objective-C and Java Primitive Data Types

I need to convert a piece of code from Objective-C to Java, but I have a problem understanding the Primitive Types in Objective-C. So I had their data types in my Objective-C code :

UInt64, Uint32, UInt8 ,

which are unsigned integers (as I understand from internet). So my question is, can I use Java primitive types like byte (8bit) - instead of UInt8 , int (32bit) - instead of UInt32 , and long (64bit) - instead of UInt64 .

Unfortunately, it isn't a straight translation and without knowing more about your program, its hard to suggest what the "right" approach is.

  • If your UInt8 values really range from 0-255, you may have to use Java signed int to be able to hold the entire range.

  • If you are dealing with byte streams or memory layouts and really need to use just a single byte of memory, than you could try byte , but you may have to test and handle cases to handle when the high-bit is set (value > 127). Ditto with the other unsigned types.

Ideally, if your code just kind of "defaulted" to the unsigned types, but really the signed versions would have worked fine too (ie the ranges of your values never equal or exceed 2^7, 2^15, or 2^31 respectively), then you may be fine with the "straight" translation to byte , int , and long .

Yes, those are the correctly sized data types to use in Java. Make sure you take into account that Java does not have unsigned types and the trick is to use the next largest size. 64 bit unsigned arithmetic requires special consideration.

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