Introduction
In Java, the String is a Class and not a data type like 'int & char'. Thus, the String class represents String characters, and all string objects in Java are instances of this type. The string class is the most useful in Java programming and Selenium WebDriver.
Java String class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc. In this blog, we will learn about the String getBytes() Method in Java with the help of a few examples.
getBytes() Method
The getBytes() method of the Java String class encodes a string into a sequence of bytes and stores it in an array of bytes.
Syntax
The getBytes() function has three variations. The string getBytes() method's signature or syntax is as follows:
string.getBytes()
string.getBytes(Charset charset)
string.getBytes(String charsetName)
Note: String is an object of the String class in this case, and the function getBytes() returns a byte array.
Parameters
charset / charsetName - The name of a charset supported by the method.
Return
Byte sequence is returned as a value
Exception Throws
UnsupportedEncodingException: This exception is thrown when the function does not support the specified charset.
Internal implementation
public byte[] getBytes() {
return StringCoding.encode(value, 0, value.length);
}




