您可以将其视为不可变的字节数组。差不多就是这样。这是一个你可以在原型中使用的。Protobuf不允许您使用Java数组,因为它们是可变的。ByteStringbyte[]
ByteString存在,因为 不适合表示任意字节序列。 专门用于字符数据。StringString
  protobuf MessageLite Interface 提供了 toByteArray() 和 toByteString() 方法。如果 ByteString 是不可变的 byte[],那么由 ByteString 和 byte[] 表示的消息的字节表示形式是否相同?
差不多吧。如果调用,您将获得与调用 相同的值。比较这两种方法的实现,在:toByteArray()toByteString().toByteArray()AbstractMessageLite
public ByteString toByteString() {
  try {
    final ByteString.CodedBuilder out =
      ByteString.newCodedBuilder(getSerializedSize());
    writeTo(out.getCodedOutput());
    return out.build();
  } catch (IOException e) {
    throw new RuntimeException(
      "Serializing to a ByteString threw an IOException (should " +
      "never happen).", e);
  }
}
public byte[] toByteArray() {
  try {
    final byte[] result = new byte[getSerializedSize()];
    final CodedOutputStream output = CodedOutputStream.newInstance(result);
    writeTo(output);
    output.checkNoSpaceLeft();
    return result;
  } catch (IOException e) {
    throw new RuntimeException(
      "Serializing to a byte array threw an IOException " +
      "(should never happen).", e);
  }
}