Java String: getBytes() Method
getBytes() Method
Contents:
public byte[] getBytes()
The getBytes() method is used to encode a specified String into a sequence of bytes using the named charset, storing the result into a new byte array.
The behavior of this method, when this string cannot be encoded in the given charset, is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.
Java Platform: Java SE 8
Syntax:
getBytes()
Return Value:
The resultant byte array.
Return Value Type:
Example: Java String getBytes() Method
The following example shows the usage of java String() method.
public class Exercise {
public static void main(String[] args)
{
String str = "The quick brown fox jumps over the lazy dog.";
// Copy the contents of the String to a byte array.
byte[] byte_arr = str.getBytes();
// Create a new String using the contents of the byte array.
String new_str = new String(byte_arr);
// Display the contents of the byte array.
System.out.println("\nThe new String is : " +
new_str + "\n");
}
}
Output:
The new String is : The quick brown fox jumps over the lazy dog.
public byte[] getBytes(Charset charset)
Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.
This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement byte array. The CharsetEncoder class should be used when more control over the encoding process is required.
Syntax:
getBytes(Charset charset)
Parameters:
Name | Description | Type |
---|---|---|
charset | The Charset to be used to encode the String. | byte |
Return Value : The resultant byte array.
Return Value Type: byte
Example: Java String getBytes(Charset charset) Method
The following example shows the usage of java String() method.
import java.nio.charset.Charset;
public class Example {
public static void main(String[] args) {
try {
String str = "example.com";
System.out.println();
System.out.println("\nOriginal string = " + str);
// copy the contents of the String to a byte array
byte[] arr = str.getBytes(Charset.forName("ASCII"));
String str1 = new String(arr);
System.out.println("New string = " + str1);
System.out.println();
} catch (Exception e) {
System.out.print(e.toString());
System.out.println();
}
}
}
Output:
Original string = example.com New string = example.com
public byte[] getBytes(String charsetName)
Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
The behavior of this method, when this string cannot be encoded in the given charset, is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.
Syntax:
getBytes(String charsetName)
Parameters:
Name | Description | Type |
---|---|---|
charsetName | The name of a supported charset. | byte |
Return Value: The resultant byte array.
Return Type: byte
Throws:
UnsupportedEncodingException - If the named charset is not supported
Example: Java String getBytes(String charsetName) Method
The following example shows the usage of java String() method.
public class Example {
public static void main(String[] args) {
try {
String str = "example.com";
System.out.println("\nOriginal string = " + str);
// copy the contents of the String to a byte array
byte[] array = str.getBytes("ASCII");
String str1 = new String(array);
System.out.println("New string = " + str1);
System.out.println();
} catch (Exception e) {
System.out.print(e.toString());
System.out.println();
}
}
}
Output:
Original string = example.com New string = example.com
Java Code Editor:
Previous:format Method
Next:getChars Method
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/java-tutorial/string/string_getbytes.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics