Java String getBytes() Function: Complete Guide

The Java String getBytes Method encodes the String to the sequence of bytes using the user-specified charset, and it will store the result in the byte array.

Java String getBytes()

Java String getBytes() is a built-in method that helps us encode a given string into a byte code sequence and return an array of bytes. The getBytes() function encodes the StrStringto a sequence of bytes using the specified charset and returns the array of those bytes. In addition, it throws UnsupportedEncodingException – If the specified charset is not supported.

The getBytes() function can be implemented in two ways:

public byte[] getBytes(String charsetName)

The method encodes a string into a sequence of bytes using some specified charset and returns the array of those bytes. If the specified charset is not supported, it throws an exception called UnsupportedEncodingException.

See the following program of String getBytes().


class GetBytes {
    public static void main(String args[]) {

        String str = "Welcome to Appdividend";
        System.out.println("The String before conversion is : ");
        // converting the string into byte
        // using getBytes ( converts into ASCII values )
        byte[] arr = str.getBytes();

        // Displaying converted string after conversion
        System.out.println("The String after conversion is : ");
        for (int i = 0; i < arr.length; i++) {

See the following output.

Java String getBytes()

public byte[] getBytes()

It encodes the String to the default charset method. There is some default charset defined in Java; some of them are:

  1. US-ASCII: Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
  2. ISO-8859-1: ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
  3. UTF-8: Eight-bit UCS Transformation Format
  4. UTF-16BE: Sixteen-bit UCS Transformation Format, big-endian byte order
  5. UTF-16LE: Sixteen-bit UCS Transformation Format, little-endian byte order
  6. UTF-16: Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark.

See the following programing example of the getBytes() function.


class GetBytes {
   public static void main(String args[]) {
      String str = new String("Welcome to Appdividend");
      byte[] array1 = str.getBytes();

      // Printing default charset encoding
      System.out.print("Default Charset encoding:");
      for (byte b : array1) {
      // printing UTF-16 Charset encoding
      System.out.print("\nUTF-16 Charset encoding:");
      try {
         byte[] array2 = str.getBytes("UTF-16");
         for (byte b1 : array2) {
         // converting UTF-16BE encoding
         byte[] array3 = str.getBytes("UTF-16BE");
         System.out.print("\nUTF-16BE Charset encoding:");
         for (byte b2 : array3) {
         // throwing exception
      } catch (UnsupportedEncodingException ex) {
         System.out.println("Unsupported character set" + ex);

See the output.


That’s it for this tutorial.

Recommended Posts

How To Convert String Characters To Lowercase

Java String toCharArray()

Java String indexOf()

Java String compareTo()

How to find string length In Java

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.