Java String Getbytes
# Java getBytes() Method
[Java String Class](#)
* * *
The `getBytes()` method has two forms:
* **`getBytes(String charsetName)`:** Encodes the string into a sequence of bytes using the specified character set and stores the result into a new byte array.
* **`getBytes()`:** Encodes the string into a sequence of bytes using the platform's default character set and stores the result into a new byte array.
### Syntax
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
or
public byte[] getBytes()
### Parameters
* **charsetName** -- The name of a supported character set.
### Return Value
Returns a byte array.
## Example
import java.io.*; public class Test{public static void main(String args[]){String Str1 = new String("tutorial"); try{byte[]Str2 = Str1.getBytes(); System.out.println("Return valueοΌ" + Str2); Str2 = Str1.getBytes("UTF-8"); System.out.println("Return valueοΌ" + Str2); Str2 = Str1.getBytes("ISO-8859-1"); System.out.println("Return valueοΌ" + Str2); }catch(UnsupportedEncodingException e){System.out.println("Unsupported character set"); }}}
The output of the above program is as follows:
Return valueοΌ[B@7852e922 Return valueοΌ[B@4e25154f Return valueοΌ[B@70dea4e
* * Java String Class](#)
YouTip