Strings are always encoded by UTF-16 in Java. These are just sequences of char values ββthat are UTF-16 code units. When you specify the encoding in the String(byte[], String) constructor String(byte[], String) , it just says how to decode bytes to text - after that, the encoding is discarded.
If you need to keep the encoding, you need to create your own class to save the Charset and String together. I canβt say that I ever wanted to do this - are you really sure what you need?
(Thus, your βstuckβ code will not work anyway - and it will also be inefficient.)
Jon skeet
source share