Serial sending unexpected values

I encountered another problem when communicating with Arduino. I want to send raw data. On the Processing side I prepare them as a String and send them to Arduino via Serial. Using

void writeToArduino(String whatToWrite) {
  Arduino.write(whatToWrite);
}

mangles the data for some reasons. For example it always sends 0x3F instead of 0xFF. But other chars are sent correctly. To get correct data I need to do

void writeToArduino(String whatToWrite) {
  for (int i=0; i<whatToWrite.length(); i++) Arduino.write(whatToWrite.charAt(i));
}

Why? String is not “an array of chars?”

A char primitive type in Java is an unsigned value of 16 bits (2 bytes):

So a String of length() 10 is actually 20 bytes for most cases.

A shorter workaround is call method String::getBytes():
docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#getBytes()

void writeToArduino(String whatToWrite) {
  Arduino.write(whatToWrite.getBytes());
}