getBytes() in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

The getBytes() function is an inbuilt method of Java that converts a string into a sequence of bytes. The getBytes() method takes the string which is to be converted and returns the byte array.

Syntax of getBytes() in Java

Let's understand the syntax of getBytes() in java. The getBytes() in java uses the string that is to be converted into the array of bytes. getBytes() function takes optional parameters as charset or string.

Syntax:

Parameters of getBytes() in Java

Let's understand the parameters of getBytes() in java. getBytes() function takes an optional parameter that can be charset or a string according to which string has to be encoded while conversion into bytes.

  • charset: There are different CharSet available in java:
    • UTF-8: 8-bit UCS Transformation Format
    • UTF-16: 16-bit UCS Transformation Format
    • UTF-16BE: 16-bit UCS Transformation Format, big-endian byte order
    • UTF-16LE: 16-bit UCS Transformation Format, little-endian byte order
    • US-ASCII: 7-bit ASCII
    • ISO-8859-1: ISO Latin Alphabet No. 1
  • String CharsetName: We can also use strings to specify the encoding type to getBytes(), we can pass the above charset values and they'll be processed as string values.

Return Values of getBytes() in Java

Return Type: byte array

The Return type of getBytes() is a byte array made from the given string value.

Exceptions of getBytes() in Java

The UnsupportedEncodingException occurs mainly in the getBytes() method due to the wrong encoding charset value passed as the string. Let's understand this using an example.

Output:

Example of getBytes() in Java

Lets take a look at a short example on how the getBytes function works, we will simply apply the getBytes() function with no parameter so the string will be encoded with the default charset value.

Output: The string is simply converted to its corresponding ASCII encoding values:

What is getBytes() in Java?

Let's suppose you have to convert the given string to its corresponding bytearray, then getbytes() function comes to play and converts the entire string value into a sequence of bytes and returns the byte array. getbytes() method also takes some optional parameter for charset so that we can convert the string to particular charset.

More Examples of getBytes() in Java

Example 1: getBytes() Without Any Parameters

Lets take a look at an example where we will simply apply the getBytes() function with no parameter so the string will be encoded with default charset value.

Output: The string is simply converted to its corresponding ASCII encoding values:

Example 2: getBytes() With CharSet Parameter

Let's take a look at an example where we will be passing the above discussed CharSet values. We will import java.nio.charset.Charset to use CharSet in our java program.

Output: The string is simply converted to its corresponding ASCII encoding values with corresponding Charset Values:

Example 3: getBytes() With String Parameter

Let's take a look at an example where we will be passing the above-discussed CharSet values as String values. we have to wrap the code inside the try-catch block while using the getBytes() with the string parameter.

Output: The string is simply converted to its corresponding ASCII encoding values with corresponding Charset Values, The American Standard Code for Information Interchange (ASCII) is a character encoding standard for electronic communication.

Conclusion

  • The getBytes() function is an inbuilt method of Java.
  • The getBytes() function converts a string into a sequence of bytes and returns a byte array.
  • getBytes() function takes optional parameter as charset or string.
  • The getBytes() function throws UnsupportedEncodingException due to the wrong encoding charset value passed.