Java String - getBytes() Method
The java.lang.String.getBytes() method is used to encode this String into a sequence of bytes using platform's default charset, storing the result into a new byte array.
Syntax
public byte[] getBytes()
Parameters
No parameter is required.
Return Value
Returns the resultant byte array.
Exception
NA.
Example:
In the example below, getBytes() method is used to encode the given String into a sequence of bytes using the platform's default charset.
import java.lang.*; public class MyClass { public static void main(String[] args) { String MyString = "HELLO"; //encoding the string into a byte array byte Arr[] = MyString.getBytes(); //printing the content of byte array System.out.print("Default Charset encoding:"); for(byte i: Arr) System.out.print(" " + i); } }
The output of the above code will be:
Default Charset encoding: 72 69 76 76 79
❮ Java.lang - String