In Java, there are instances where the developer runs short of memory. In such a case scenario, encoding comes into effect to transform data from one format to another to save memory. This can be done via the String “getBytes()” method.
This write-up demonstrates the implementation of the String “getBytes()” method.
How to Use the Java String “getBytes()” Method?
The “getBytes()” method in Java encodes a string into a bytes sequence via the named character set and stores the output in a byte array.
Syntax
string.getBytes()
string.getBytes(Charset chrst)
string.getBytes(String chrstname) throws UnsupportedEncodingException
In these syntaxes, “string” corresponds to an object of the String class, and “chrst/chrstname” refers to the charset’s name supported by the method.
Example 1: Utilizing the Java String “getBytes()” Method Without any Parameter
In this example, no parameter will be specified in the “getBytes()” method and so it encodes the string via the platform’s default charset:
import java.util.Arrays;
public class Getbytes {
public static void main(String[] args) {
String givenString = "Linuxhint";
byte[] btArray = givenString.getBytes();
System.out.println("Array -> "+Arrays.toString(btArray));
}}
In this snippet of code:
- Import the specified package to work with the “Arrays” class.
- In the next step, initialize the string to be evaluated.
- Now, apply the “string.getBytes()” method without any parameter to convert the string into the byte array “btArray” via the platform’s default charset.
- Lastly, utilize the “Arrays.toString()” method to return the byte array as a string.
Output

From this output, it can be verified that the byte array is retrieved based on the defined string.
Example 2: Utilizing the Java String “getBytes()” Method With “CharSet” Parameter
The “forName()” method of the “java.nio.charset” gives a charset object with respect to the named charset.
Syntax
Charset forName(String chrstname)
The method retrieves a charset object against the named charset i.e., “chrstname”.
This example applies this method with the “getBytes()” method utilizing the “CharSet” parameter. Following are the most commonly used “CharSets” in Java:
- UTF-8: 8-bit UCS Transformation Format.
- UTF-16: 16-bit UCS Transformation Format.
Now, move to the code implementation in this scenario utilizing the above “CharSets”:
import java.util.Arrays;
import java.nio.charset.Charset;
public class Getbytes {
public static void main(String[] args) {
String givenString = "Linuxhint";
byte[] btArray = givenString.getBytes(Charset.forName("UTF-8"));
System.out.println("Array in UTF-8 Encoding -> "+Arrays.toString(btArray));
byte[] btArray2 = givenString.getBytes(Charset.forName("UTF-16"));
System.out.println("Array in UTF-16 Encoding -> "+Arrays.toString(btArray2));
}}
In this block of code:
- Import the additional “java.nio.charset.Charset” package to utilize “CharSet”.
- Now, define the string and apply the combined “CharSet.forName()” and “getBytes()” methods to return the array based on the specified charsets “UTF-8” and “UTF-16”, respectively.
Output

Example 3: Utilizing the Java String “getBytes()” Method With “String” Parameter
In this specific demonstration, the discussed method will be applied with the “String” parameter to include the encoding type directly as a string:
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class Getbytes {
public static void main(String[] args) throws UnsupportedEncodingException {
String givenString = "Linuxhint";
byte[] byteArray = givenString.getBytes("UTF-8");
System.out.println("Array in UTF-8 Encoding -> "+Arrays.toString(byteArray));
byteArray = givenString.getBytes("UTF-16");
System.out.println("Array in UTF-16 Encoding -> "+Arrays.toString(byteArray));
}}
In this code:
- Include the “UnsupportedEncodingException” to be declared and thrown.
- Now, specify the encoding types “UTF-8” and “UTF-16” as the “getBytes()” method directly and return the corresponding array as a string via the “toString()” method in both scenarios.
Output

However, specifying an inaccurate encoding type returns the “UnsupportedEncodingException” exception, demonstrated as follows:

Conclusion
The “getBytes()” method in Java encodes a string in a sequence of bytes via the named character set and stores the output in a byte array. This method can be applied directly, with the “ChatSet” parameter, or with the “String” parameter. However, specifying an inaccurate encoding type throws an exception. This blog explained the working of the Java “getBytes()” method.