Java: String getBytes() method

In this article, we will discuss couple of variant methods of String to convert/encode the string into equivalent byte array

 

getBytes() method: This String method is used to convert or encode into sequence of bytes using either JVM’s default charset or named charset

Note: There are 4 variants or overloaded getBytes() methods & one of them is deprecated

 

Method Signature:

public byte[] getBytes();

public byte[] getBytes(Charset charset);

public byte[] getBytes(String charsetName) 
                           throws UnsupportedEncodingException;

@deprecated
public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin);

 

Returns:

Returns a sequence of bytes or byte array

 

Throws:

UnsupportedCodingException: If the charset name passed is invalid or not supported

 

Examples on getBytes() method:

Demo Java program to convert string into byte array

StringGetBytesMethod.java

package in.bench.resources.string.methods;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays;

public class StringGetBytesMethod {

	public static void main(String[] args) {

		String str1 = "Universe";

		// default platform-specific charset name
		byte[] bArrray1 = str1.getBytes();

		// static method of charset
		byte[] bArrray2 = str1.getBytes(Charset.forName("UTF-16"));

		// charset name passed as string argument
		byte[] bArrray3 = null;
		try {
			bArrray3 = str1.getBytes("UTF-16BE");
		} catch (UnsupportedEncodingException useex) {
			useex.printStackTrace();
		}

		// Printing to console
		System.out.println("Default charset of platform : \n" 
                                         + Arrays.toString(bArrray1));
		System.out.println("\nStatic method of charset : \n" 
                                         + Arrays.toString(bArrray2));
		System.out.println("\nPassed UTF-16BE charset name : \n" 
                                         + Arrays.toString(bArrray3));
	}
}

 

Output:

Default charset of platform : 
[85, 110, 105, 118, 101, 114, 115, 101]

Static method of charset : 
[-2, -1, 0, 85, 0, 110, 0, 105, 0, 118, 0, 101, 0, 114, 0, 115, 0, 101]

Passed UTF-16BE charset name : 
[0, 85, 0, 110, 0, 105, 0, 118, 0, 101, 0, 114, 0, 115, 0, 101]

 

References:

https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Read Also:

  • Java: String class
  • Java: String Literal and String constant pool concept
  • Java: String is Immutable
  • Java: String comparison
  • Java: String concatenation

 

Happy Coding !!
Happy Learning !!

  • Thanks for this method. I found this as the most easiest methods of String to convert/encode the string into equivalent byte array. While programming I always wonder what the easiest way can be and now I found the one.

    🙂