If you need an unsigned 8 bit integer then use byte. It's easy to make it unsigned in arithemtic operations (where actually sign matters) as byteValue & 0xFF

Answer from Evgeniy Dorofeev on Stack Overflow
🌐
Darksleep
darksleep.com › player › JavaAndUnsignedTypes.html
Java and unsigned int, unsigned short, unsigned byte, unsigned long, etc. (Or rather, the lack thereof)
April 7, 2012 - Java's 'char' also can be used as an unsigned short, i.e. it represents numbers from 0 up to 2^16. The only gotchas are, weird things will happen if you try to assign it to a short, and if you try to print it, you'll get the unicode character it represents, instead of the integer value.
🌐
Coderanch
coderanch.com › t › 508884 › java › converting-unsigned-char-JAVA
Problem converting C/C++ unsigned char to JAVA (Java in General forum at Coderanch)
September 1, 2010 - How would i read in JAVA so as to get value 160 ? The followng C++ code Thank you. ... Ravi Kumar wrote:In JAVA, after reading it as char and typecasting into int its value is 8224. There's your #1 mistake. A C char is only 1 byte in size, so you would need to use a Java byte for that.
Discussions

Is Java char signed or unsigned for arithmetic? - Stack Overflow
Java char is a 16 bit data type, but is it signed or unsigned when it comes to performing arithmetic on it? Can you use it as an unsigned 16 bit integer in arithmetic? For example, is the following More on stackoverflow.com
🌐 stackoverflow.com
types - Unsigned short in Java - Stack Overflow
You can use, System.out.format ... will give you an unsigned shift of short. However this adds 1 operation (bitwise &) to each shit. 2013-11-05T15:40:10.04Z+00:00 ... Java has a logical right shift operator, >>>. Also, char will never change its size.... More on stackoverflow.com
🌐 stackoverflow.com
Does Java not have unsigned types?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
10
3
August 26, 2021
c - How can I safely use a Java byte as an unsigned char? - Stack Overflow
I am porting some C code that uses ... into Java. The C code operates under the assumption that int is 32 bits wide and char is 8 bits wide. There are assertions in it that check whether those assumptions are valid. I have already come to terms with the fact that I'll have to use long in place of unsigned ... More on stackoverflow.com
🌐 stackoverflow.com
March 26, 2017
🌐
Programming.Guide
programming.guide › java › unsigned-short.html
Unsigned short in Java | Programming.Guide
The casting is required, since a char is implicitly widened to an int during computations. ... A short is always signed in Java, but nothing prevents you from viewing a short simply as 16 bits and interpret those bits as a value between 0 and 65,535.
🌐
Princeton
cs.princeton.edu › courses › archive › spr18 › cos217 › lectures › 04_DataTypes.pdf pdf
1 Data Types in C Princeton University
Java only · • boolean, byte · C only · • unsigned char, unsigned short, unsigned int, unsigned long · Sizes · • Java: Sizes of all types are specified, and portable · • C: Sizes of all types except char are system-dependent · Type char · • Java: char is 2 bytes (to hold all ...
Find elsewhere
🌐
Java67
java67.com › 2016 › 07 › what-is-difference-between-byte-and-char-java.html
What is the difference between byte and char data types in Java? Example | Java67
The range of byte is between -128 to 127 but the range of char is from 0 to 65535 because a byte is a signed 8-bit data type and char is an unsigned 16-bit data type hence, its maximum value is 2 ^ 16 - 1 which is 65535.
Top answer
1 of 2
5

You can safely use a byte to represent a value between 0 and 255 if you make sure to bitwise-AND its value with 255 (or 0xFF) before using it in computations. This promotes it to an int, and ensures the promoted value is between 0 and 255.

Otherwise, integer promotion would result in an int value between -128 and 127, using sign extension. -127 as a byte (hex 0x81) would become -127 as an int (hex 0xFFFFFF81).

So you can do this:

long a = (((data[0] & 255) << 24) | ((data[1] & 255) << 16) | ((data[2] & 255) << 8) | (data[3] & 255)) & 0xffffffff;

Note that the first & 255 is unnecessary here, since a later step masks off the extra bits anyway (& 0xffffffff). But it's probably simplest to just always include it.

2 of 2
-1

... can I safely use byte as a replacement for unsigned char?

As you've discovered, not really... No.

According to Oracle Java documentation, byte is a signed integer type, and though it has 256 distinct values (due to the explicit range specification "It has a minimum value of -128 and a maximum value of 127 (inclusive)" from the documentation) there are values that an unsigned char from C can store, that a byte from Java can't (and vice-versa).

That explains the problem you've experienced. However, the extent of the problem hasn't been fully demonstrated on your 8-bit-byte implementation.


What other "gotchas" should I be aware of?

Whilst a byte in Java is required to have support for only values between (and including) -128 and 127, Cs unsigned char has maximum value (UCHAR_MAX) that depends upon the number of bits used to represent it (CHAR_BIT; at least 8). So when CHAR_BIT is greater than 8, there will be extra values beyond 255 that unsigned char can store.


In summary, in the world of Java a byte should really be called an octet (a group of eight bits) where-as in C a byte (char, signed char, unsigned char) is a group of at least (possibly more than) eight bits.

No. They are not equivalent. I don't think you'll find an equivalent type in Java, either; they're all rather fixed-width. You could safely use byte in Java as an equivalent for int8_t in C, however (except that int8_t isn't required to exist in C unless CHAR_BIT == 8).


As for pitfalls, there are some in your C code too. Assuming data[0] is an unsigned char, data[0] << 24 is undefined behaviour on any system for which INT_MAX == 32767.

🌐
Quora
quora.com › What-is-the-size-of-a-char-data-type-in-bytes
What is the size of a char data type in bytes? - Quora
Answer: In C# and Java (and I expect almost all other current languages) chars are 16 bits - 2 bytes - as they implement the Unicode UTF-16 character set. You shouldn’t need to know the actual size of a char, all of this just magically occurs.
🌐
All About Circuits
forum.allaboutcircuits.com › home › forums › embedded & programming › programming & languages
Getting and unsigned char's size | All About Circuits
February 20, 2017 - I see that array is just another unsigned char variable and not an actual array. I made it an array by attaching the [] in front of the array. Though I'm still getting an error below. unsigned char[size] is not assignable.
🌐
CodeGym
codegym.cc › java blog › java types › char data type in java
Char Data Type in Java
April 24, 2025 - The char data type in Java is a primitive type that holds a single character, like a letter, digit, or symbol. It's stored in 16 bits (2 bytes), unlike some languages that use 8 bits, giving it room for 65,536 possible values.
🌐
IceCube
user-web.icecube.wisc.edu › ~dglo › c_class › fundamental_types.html
C Class - Fundamental Datatypes
CHAR_BIT 8 /* number of bits in a 'char' */ SCHAR_MIN -127 /* minimum value for 'signed char' */ SCHAR_MAX 127 /* maximum value for 'signed char' */ UCHAR_MAX 255 /* maximum value for 'unsigned char' */ SHRT_MIN -32767 /* minimum value for '(signed) short (int)' */ SHRT_MAX 32767 /* maximum ...
🌐
Reddit
reddit.com › r/learnprogramming › unsigned char
r/learnprogramming on Reddit: unsigned char
August 15, 2022 -
  1. I know that char is neither sign or unsigned because we use characters not numbers.

  2. the size of a char variable is 1 byte in most compilers.

I read recently that the range of values a char variable could take is from -128 to 255. Moreover, if i want the range of values to be only positive i could use unsigned char which will give me the range from 0 to 255.

What i dont get is, why do we need unsigned when we don't store negative numbers in a char variable as a char variable takes only one char as '5' not '-5'.

I dont get why we need to use unsigned char.

Top answer
1 of 2
1
char is odd, to the compiler and processor it's a 1-byte integer, and like other integer types can either be signed or unsigned. The difference, like with all integer types, is that signed integers sacrifice one bit for the "sign" (loosely). Semantically to a C/C++ programmer, a char is useful in c-strings using ASCII encoding - each number corresponds to a character that is often a letter, number, or symbol. Standard io streams (cin, cout, printf, scanf) understand how to process arrays of chars, and C++ compilers understand how to process c-strings you type in to the code, but under the hood it's all integer stuff (the idea of a string doesn't exist to your processor). Char data types are also used to: Store a number that will always be small (e.g. enum values that will always be 0-5) Store a single byte of data in a buffer of unstructured data (e.g. binary data freshly loaded from a file and not yet parsed) In both of those cases, it doesn't matter a lot which one you use as long as you're consistent. I like using unsigned because it feels like a more helpful expression of a byte, but that's just preference. Another note: there's also a "signed char" that you can use if you want to be explicit. I pretty rarely see it in the wild, but it exists and is more explicit/correct if I remember right.
2 of 2
1
It is signed or unsigned. Which it is is dependent on the implementation for historical portability reasons. Other than that, it is exactly like every other integer type, but with a size defined to be exactly 1.
🌐
Java Mex
javamex.com › java_equivalents › unsigned.shtml
Java unsigned operations
When an integer is signed, one of its bits becomes the sign bit, meaning that the maximum magnitude of the number is halved. (So an unsigned 32-bit int can store up to 232-1, whereas its signed counterpart has a maximum positive value of 231-1.) In Java, all integer types are signed (except char).
🌐
Medium
medium.com › @AlexanderObregon › how-javas-char-type-stores-unicode-characters-c8c8e2a910f6
How Java’s char Type Stores Unicode Characters | Medium
March 4, 2025 - The char type is a 16-bit (2-byte) unsigned value designed to store a single UTF-16 code unit in Java. This is important to understand because it means char does not always store an entire Unicode character—only a unit of UTF-16 encoding.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › datatypes.html
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
As mentioned above, this data type ... information, but its "size" isn't something that's precisely defined. char: The char data type is a single 16-bit Unicode character....
🌐
Wikipedia
en.wikipedia.org › wiki › C_data_types
C data types - Wikipedia
3 days ago - Variable b evaluates to false if unsigned char has a size of 8 bits. This is because the value 256 does not fit in the data type, which results in the lower 8 bits of it being used, resulting in a zero value.