Java String getBytes() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, getBytes() encodes a string into a byte[]. Prefer an explicit charset such as UTF-8 so the result does not depend on the platform default.

You can call it with no arguments or pass a charset. The charset form is the safer choice for portable code.

Syntax

Call getBytes with an explicit charset. Start with this line.

java
byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
CallWhat it does
text.getBytes(StandardCharsets.UTF_8)Encodes text as UTF-8 bytes.

Import java.nio.charset.StandardCharsets. Prefer UTF-8 for files, APIs, and network data.

Library declarations
java
public byte[] getBytes();
public byte[] getBytes(Charset charset);
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException;

Parameters

ParameterDescription
charsetOptional. The encoding to use, such as StandardCharsets.UTF_8. Strongly recommended.

Return Value

Return ValueDescription
byte[]The encoded bytes for the string.

Java Examples

Example 1 — Encode ASCII with UTF-8

Encode a short string and print each byte value. For everyday ASCII, each letter is one byte in UTF-8.

java
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class GetBytesBasic {
    public static void main(String[] args) {
        String text = "Hi";

        byte[] data = text.getBytes(StandardCharsets.UTF_8);

        System.out.println(Arrays.toString(data));
        System.out.println("byte length = " + data.length);
    }
}

How It Works

  1. Import UTF-8. StandardCharsets.UTF_8 avoids platform defaults.
  2. Call getBytes. "Hi" is encoded into a new byte[].
  3. Read values. 'H' → 72, 'i' → 105 (ASCII codes).
  4. Check length. Two letters → two bytes in UTF-8.

Example 2 — Byte Length vs length()

Emoji show why encoded size is not the same as codePointCount or UTF-16 length().

java
import java.nio.charset.StandardCharsets;

public class GetBytesLength {
    public static void main(String[] args) {
        String text = "Hi😀";

        byte[] utf8 = text.getBytes(StandardCharsets.UTF_8);

        System.out.println("length() = " + text.length());
        System.out.println("UTF-8 bytes = " + utf8.length);
    }
}

How It Works

  1. Map the string. H and i are one UTF-16 unit each; 😀 is a surrogate pair → length() 4.
  2. Encode as UTF-8. Letters stay 1 byte each; the emoji becomes 4 bytes.
  3. Total bytes. 1 + 1 + 4 = 6.
  4. Pick the right metric. Use byte length for buffers/files; use code points for “character” limits.

Example 3 — Encode Then Decode

Round-trip with the same charset. Decoding with a different encoding can corrupt text— see also String.format when you only need a display string, not bytes.

java
import java.nio.charset.StandardCharsets;

public class GetBytesRoundTrip {
    public static void main(String[] args) {
        String original = "Café 😀";

        byte[] data = original.getBytes(StandardCharsets.UTF_8);
        String back = new String(data, StandardCharsets.UTF_8);

        System.out.println(back);
        System.out.println("same = " + original.equals(back));
    }
}

How It Works

  1. Start with Unicode text. Includes accented letters and an emoji.
  2. Encode to UTF-8 bytes. Safe, portable binary form.
  3. Decode with UTF-8. new String(data, UTF_8) rebuilds the text.
  4. Compare. equals is true when encode/decode charsets match.

Frequently Asked Questions

getBytes() encodes a string into a byte array.
The platform default charset. Prefer an explicit charset instead.
UTF-8 is portable and standard for files, APIs, and network data.
A byte[] with the encoded characters.
Calling getBytes on null throws NullPointerException.

Next: getChars()

Copy a range of characters into an existing char array.

getChars() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful