Java - Write code to convert string to Byte array using UTF-8 encoding

Requirements

Write code to convert string to Byte array using UTF-8 encoding

Demo

//package com.book2s;
import java.io.UnsupportedEncodingException;

public class Main {
    public static void main(String[] argv) {
        String string = "book2s.com";
        System.out.println(java.util.Arrays.toString(toBytes(string)));
    }//from w  w  w  .  ja va  2 s.c  om

    public static byte[] toBytes(String string) {
        try {
            return string.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException(e);
        }
    }
}