A string needs no encoding. It is simply a sequence of Unicode characters.

You need to encode when you want to turn a String into a sequence of bytes. The charset the you choose (UTF-8, cp1255, etc.) determines the Character->Byte mapping. Note that a character is not necessarily translated into a single byte. In most charsets, most Unicode characters are translated to at least two bytes.

Encoding of a String is carried out by:

String s1 = "some text";
byte[] bytes = s1.getBytes("UTF-8"); // Charset to encode into

You need to decode when you have а sequence of bytes and you want to turn them into a String. When yоu dо that you need to specify, again, the charset with which the bytеs were originally encoded (otherwise you'll end up with garblеd tеxt).

Decoding:

String s2 = new String(bytes, "UTF-8"); // Charset with which bytes were encoded 

If you want to understand this better, a great text is "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)"

Answer from Itay Maman on Stack Overflow
Top answer
1 of 3
50

A string needs no encoding. It is simply a sequence of Unicode characters.

You need to encode when you want to turn a String into a sequence of bytes. The charset the you choose (UTF-8, cp1255, etc.) determines the Character->Byte mapping. Note that a character is not necessarily translated into a single byte. In most charsets, most Unicode characters are translated to at least two bytes.

Encoding of a String is carried out by:

String s1 = "some text";
byte[] bytes = s1.getBytes("UTF-8"); // Charset to encode into

You need to decode when you have а sequence of bytes and you want to turn them into a String. When yоu dо that you need to specify, again, the charset with which the bytеs were originally encoded (otherwise you'll end up with garblеd tеxt).

Decoding:

String s2 = new String(bytes, "UTF-8"); // Charset with which bytes were encoded 

If you want to understand this better, a great text is "The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)"

2 of 3
10

the core functions are getBytes(String charset) and new String(byte[] data). you can use these functions to do UTF-8 decoding.

UTF-8 decoding actually is a string to string conversion, the intermediate buffer is a byte array. since the target is an UTF-8 string, so the only parameter for new String() is the byte array, which calling is equal to new String(bytes, "UTF-8")

Then the key is the parameter for input encoded string to get internal byte array, which you should know beforehand. If you don't, guess the most possible one, "ISO-8859-1" is a good guess for English user.

The decoding sentence should be

String decoded = new String(encoded.getBytes("ISO-8859-1"));
🌐
SSOJet
ssojet.com › character-encoding-decoding › utf-8-in-java
UTF-8 in Java | Encoding Standards for Programming Languages
When handling text files in Java, explicitly specifying UTF-8 encoding is crucial to ensure correct character representation. You can achieve this by wrapping your file streams with InputStreamReader for reading and OutputStreamWriter for writing, both configured with StandardCharsets.UTF_8.
🌐
Baeldung
baeldung.com › home › java › java string › encode a string to utf-8 in java
Encode a String to UTF-8 in Java | Baeldung
April 15, 2026 - String rawString = "Entwickeln Sie mit Vergnügen"; byte[] bytes = rawString.getBytes(StandardCharsets.UTF_8); String utf8EncodedString = new String(bytes, StandardCharsets.UTF_8); assertEquals(rawString, utf8EncodedString); Alternatively, we can use the StandardCharsets class introduced in Java 7 to encode the String. First, we’ll encode the String into bytes, and second, we’ll decode it into a UTF-8 String:
🌐
Mkyong
mkyong.com › home › java › how to read a utf-8 file in java
How to read a UTF-8 file in Java - Mkyong.com
August 14, 2020 - In Java, the InputStreamReader accepts a charset to decode the byte streams into character streams. We can pass a StandardCharsets.UTF_8 into the InputStreamReader constructor to read data from a UTF-8 file.
🌐
Rosetta Code
rosettacode.org › wiki › UTF-8_encode_and_decode
UTF-8 encode and decode - Rosetta Code
1 month ago - The goal of this task is to write a encoder that takes a unicode code-point (an integer representing a unicode character) and returns a sequence of 1–4 bytes representing that character in the UTF-8 encoding. Then you have to write the corresponding decoder that takes a sequence of 1–4 UTF-8 encoded bytes and return the corresponding unicode character.
🌐
Delft Stack
delftstack.com › home › howto › java › java utf 8
How to Encode String in UTF-8 in Java | Delft Stack
February 2, 2024 - The string is currently in the form of a ByteBuffer, so we call the decode() method of StandardCharsets.UTF_8 that takes the ByteBuffer object as an argument, and at last, we convert the result to a string using toString(). import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; public class JavaExample { public static void main(String[] args) { String japaneseString = "これはテキストです"; ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(japaneseString); String encodedString = StandardCharsets.UTF_8.decode(byteBuffer).toString(); System.out.println(encodedString); } } Output: これはテキストです ·
🌐
GitHub
github.com › xetorthio › fastu
GitHub - xetorthio/fastu: Fast UTF-8 encoder/decoder library for Java · GitHub
Fast UTF-8 encoder/decoder library for Java. Contribute to xetorthio/fastu development by creating an account on GitHub.
Starred by 11 users
Forked by 2 users
Languages   Java
🌐
Stack Abuse
stackabuse.com › encode-a-string-to-utf-8-in-java
Encode a String to UTF-8 in Java
March 2, 2023 - In this tutorial, we'll take a look at how to encode a String to UTF-8 in Java - using StandardCharsets, getBytes() with ByteBuffer and Apache Commons with examples.
Find elsewhere
🌐
Google Groups
groups.google.com › g › protobuf › c › DeUhifWNCkY
Java UTF-8 encoding/decoding: possible performance improvements
I've done some quick and dirty ... to protobufs. The "easy" way to do UTF-8 conversions is the way CodedInputStream/ CodedOutputStream does it: using String.getBytes() and new String()....
🌐
Java2s
java2s.com › example › android › java.lang › decode-string-in-utf8.html
decode string in UTF-8 - Android java.lang
import android.graphics.Paint; import android.text.TextPaint; import android.text.TextUtils; import android.widget.TextView; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main{ public static String decode(String s) { if (s == null) { return ""; }// ww w . jav a2 s .c o m try { return URLDecoder.decode(s, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e.getMessage(), e); } } } Previous ·
🌐
JA-VA Code
java-performance.info › home › unraveling charset encoding and decoding in java
Java UTF-8 Encode: Unraveling Charset Encoding
October 6, 2023 - Here’s a brief overview of how to use these classes for encoding and decoding: In this code, we first obtain a Charset instance for UTF-8 and then create a CharsetEncoder to encode our text.
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
MojoAuth
mojoauth.com › character-encoding-decoding › utf-8-encoding--java
UTF-8 Encoding : Java | Encoding Solutions Across Programming Languages
In this example, the byte array representing a UTF-8 encoded string is decoded back into a human-readable format using the new String() constructor with StandardCharsets.UTF_8.
🌐
Javainuse
javainuse.com › onlineutf
Online UTF-8 Encoder And Decoder Tool
Hex Encoder/Decoder Convert text ... JSON -> Java POJO Generate getters/setters from JSON Text Size (Bytes) Measure string length with/without spaces JSON Size (Bytes) Check JSON payload footprint XML Size (Bytes) Measure XML size quickly UTF-8 Encoder/Decoder Round-trip text ...
🌐
TutorialsPoint
tutorialspoint.com › convert-string-to-utf-8-bytes-in-java
Convert Unicode to UTF-8 in Java
June 26, 2020 - ... String str1 is assigned \u0000 which is the lowest value in Unicode. String str2 is assigned \uFFFF which is the highest value in Unicode. To convert them into UTF-8, we use the getBytes(“UTF-8”) method.
🌐
IBM
ibm.com › support › pages › utf-8-encodingdecoding-problem-jdk-8
UTF-8 encoding/decoding problem in JDK 8
June 16, 2018 - A new global property db2.jcc.alternateUTF8Encoding is introduced in JCC. This property can have value 1 and 0 ,the default value is 0 . If this property is set to 1 under IBM JDK 1.8 (db2.jcc.alternateUTF8Encoding=1) for UTF8 encoded data ,JCC uses UTF8J to decode .
🌐
Coderanch
coderanch.com › t › 709942 › java › decode-object-UTF
decode(object) with UTF-8 (Java in General forum at Coderanch)
But now, I would like that this file when I will be open this with this save message, should be open with Unicode (UTF-8). I create file like this: File eml = new File("abc.doc"); And now.... should be line of code with this: when I will be open file that my file will be open from Unicode (UTF-8).
Top answer
1 of 4
9

When dealing with Strings, always remember: byte != char. So in your first example, you have the char c3, not the byte c3 which is a huge difference: The byte would be part of the UTF-8 sequence but the char already is Unicode. So when you convert that to UTF-8, the Unicode character c3 must become the byte sequence c3 83.

So the question is: How did you get the String? There must be a bug in that code which doesn't properly handle UTF-8 encoded byte sequences.

The reason why ISO-8859-1 usually works is that this encoding doesn't modify any char with a code point < 256 (i.e. anything between 0 and 255), so UTF-8 encoded byte sequences won't be modified.

Your last example is also wrong: The char e9 is é in ISO-8859-1 and Unicode. In UTF-8, it's not valid since it's not a byte and since it's the byte c3 prefix is missing. That said, it correctly represents the Unicode string you seek.

2 of 4
1

If you start with the Java String where "d\u00C3\u00A9jeuner".equals(stmt) then the data is already corrupt at this stage.

A Java char is not a C char. A char in Java is 16bits wide and implicitly contains UTF-16 encoded data. Trying to store any other encoded data in a Java char/String type is asking for trouble. Character data in any other encoding should be as byte data.

If you are reading the parameter using the servlet API, then it is likely that the HTTP request contains inconsistent or insufficient encoding information. Check the calling code and the HTTP headers. It is likely that the client is encoding the data as UTF-8, but the servlet is decoding it as ISO-8859-1.

🌐
Oracle
docs.oracle.com › javase › tutorial › i18n › text › string.html
Byte Encodings and Strings (The Java™ Tutorials > Internationalization > Working with Text)
The getBytes method returns an array of bytes in UTF-8 format. To create a String object from an array of non-Unicode bytes, invoke the String constructor with the encoding parameter. The code that makes these calls is enclosed in a try block, in case the specified encoding is unsupported: try { byte[] utf8Bytes = original.getBytes("UTF8"); byte[] defaultBytes = original.getBytes(); String roundTrip = new String(utf8Bytes, "UTF8"); System.out.println("roundTrip = " + roundTrip); System.out.println(); printBytes(utf8Bytes, "utf8Bytes"); System.out.println(); printBytes(defaultBytes, "defaultBytes"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }