0

Will the byte value of a string change, if system is changed from 32 bit to 64 bit?

Suppose I have a string and I want the byte value of that String. I want to know whether the byte values in Windows 32-bit and Windows 64-bit system remains the same?

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • The internal representation is transparent to you in Java. You only access the strings through the API, and the byte representation is not relevant to you. But basically, the JVM defines the representation, not the machine on which it runs. – RealSkeptic Feb 12 '19 at 12:50
  • See [my answer to a related question](https://stackoverflow.com/a/39957184/4125191). – RealSkeptic Feb 12 '19 at 12:52
  • The JVM is by definition always 32-bit, regardless of what kind of system it's running on. The JVM implementer is responsible for making sure that the behavior is the same. – chrylis -cautiouslyoptimistic- Feb 12 '19 at 13:08
  • @chrylis: I'm not quite sure what you mean by your first sentence. JVMs can certainly run in 64bit mode and use 64bit pointers internally, for example. – Joachim Sauer Feb 12 '19 at 13:41
  • @JoachimSauer JVM implementations can run on 128-bit (Itanium) or 16-bit; the JVM's internal model is 32-bit. – chrylis -cautiouslyoptimistic- Feb 12 '19 at 17:21
  • @chrylis: what exactly do you mean by "internal mode"? I know that OpenJDK on 64bit machines with big enough stacks uses 64 bit pointers. So it's not "32-bit" from that perspective. Do you mean purely for the values of `int`? – Joachim Sauer Feb 12 '19 at 17:22
  • @JoachimSauer Not just `int`; that's the value of the logical word size, which is reflected in the available opcodes. This doesn't vary regardless of the native word size of the underlying machine and has implications for atomicity (the native machine is usually better than the JVM requires, but not always, as in embedded applications). – chrylis -cautiouslyoptimistic- Feb 12 '19 at 17:25

1 Answers1

2

I have to assume that by the "byte value of a String" you mean the byte values generated by encoding a String with a given encoding.

If so, then switching from a 32 bit architecture to a 64 bit architecture will not change the result.

However, if you just use String.getBytes() without any arguments to get the byte[] then the return value will depend on the system default encoding. While this is unlikely to be different between the 32bit and the 64bit version of the same OS with the same settings, the system default encoding can vary based on many other parameters (such as the UI language configured in your OS), so it's always a good idea to avoid this issue by always specifying a specific encoding like this:

 byte[] bytes = myString.getBytes(StandardCharsets.UTF_8);

This call will always produce the same byte[] if called on identical myString values, no matter what OS, architecture or locale setting you happen to be running it on.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614