.getBytes()

Published Mar 27, 2023
Contribute to Docs

The .getBytes() method encodes a string into an array of bytes that represents the characters in the string.

Syntax

byte[] byteArray = str.getBytes();
  • byteArray is a byte array that stores the byte representation of the string.
  • str is the string being converted to bytes.

Example

The example below demonstrates the use of the .getBytes() method:

// Example.java
import java.util.*;
public class Example {
public static void main(String[] args) {
String myStr = "codeacademy.com";
byte[] myByteArray = str.getBytes();
System.out.println(Arrays.toString(byteArray));
}
}

This outputs the following:

[99, 111, 100, 101, 97, 99, 97, 100, 101, 109, 121, 46, 99, 111, 109]

All contributors

Looking to contribute?

Learn Java on Codecademy