Java String getBytes() Method with Examples

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

The Java String class is a fundamental component of Java programming, and it offers a plethora of methods to manipulate and work with strings efficiently. Among these methods, the getBytes() method is a versatile tool for encoding strings into bytes using various character encodings. It bridges the world of text and binary data, allowing developers to adapt strings to different encoding standards, making it a crucial asset in applications where character encoding matters.

The getBytes() method boasts three distinct variants, each catering to different needs. The primary form, byte[] getBytes(), employs the platform’s default charset to encode the string.

In contrast, the other two variants, byte[] getBytes(String charsetName) and byte[] getBytes(Charset charset), enable developers to specify the character encoding explicitly. This flexibility is invaluable, especially when interoperability with external systems or services with specific character encoding requirements is crucial.

However, it’s essential to note that the getBytes() method has nuances. While it empowers developers to navigate the intricacies of character encoding, it also introduces the possibility of an UnsupportedEncodingException when a requested charset is not supported, highlighting the importance of robust error handling in code that uses this method.

Internal Implementation

Here is a snippet from the internal implementation of getBytes() in the String class:

public byte[] getBytes(String charsetName) throws UnsupportedEncodingException {
  if (charsetName == null) throw new NullPointerException();
  return StringCoding.encode(charsetName, value, offset, count); 
}

As we can see, the StringCoding class performs the actual encoding using the provided charset. This shows that getBytes() is a wrapper that handles parameters and exceptions while delegating the encoding work to StringCoding.

Examples of Using getBytes() in Java

Let’s look at examples using getBytes() to encode Strings into byte arrays.

Example 1: Using Default UTF-8 Encoding

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String str = "Hello World";
        try {
            byte[] bytes = str.getBytes("UTF-8");

            System.out.println(Arrays.toString(bytes));
        } catch (java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}

Output:
[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]

Example 2: Creating String from Bytes

import java.io.UnsupportedEncodingException;

public class Main {
    public static void main(String[] args) {
        String original = "Hello World";

        try {
            byte[] bytes = original.getBytes("UTF-16");
            String restored = new String(bytes, "UTF-16");

            System.out.println("Original String: " + original);
            System.out.println("Restored String: " + restored);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}

Output:
Hello World
Hello World

Example 3: Encoding into Different Charsets

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String str = "Hello World";

        try {
            byte[] utf8 = str.getBytes("UTF-8");
            byte[] utf16 = str.getBytes("UTF-16");
            byte[] utf16be = str.getBytes("UTF-16BE");
            byte[] iso88591 = str.getBytes("ISO-8859-1");
            byte[] utf16le = str.getBytes("UTF-16LE");

            System.out.println("UTF-8: " + Arrays.toString(utf8));
            System.out.println("UTF-16: " + Arrays.toString(utf16));
            System.out.println("UTF-16BE: " + Arrays.toString(utf16be));
            System.out.println("ISO-8859-1: " + Arrays.toString(iso88591));
            System.out.println("UTF-16LE: " + Arrays.toString(utf16le));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}

Output:
UTF-8: [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
UTF-16: [-2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100]
UTF-16BE: [0, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100]
ISO-8859-1: [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
UTF-16LE: [72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0]

Example 4: Handling UnsupportedEncodingException

public class Main {
    public static void main(String[] args) {
        String str = "Hello World";

        try {
            byte[] bytes = str.getBytes("UTF-17");
        } catch (UnsupportedEncodingException ex) {
            System.out.println("Error: " + ex.getMessage());
        }
    }
}

Output:
Error: UTF-17 is not a supported encoding

Conclusion

The getBytes() method of the Java String class is beneficial for encoding Strings into bytes using different character encodings. As we saw in the examples, it can encode into standard charsets like UTF-8, UTF-16, ISO-8859-1, etc. The byte arrays returned by getBytes() allow Strings to be converted into binary data for storage or transmission. This provides interoperability between systems that use different encodings. GetBytes () is a convenient method for handling character encoding in Java.

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

Leave a Reply

Your email address will not be published. Required fields are marked *