Here i demonstrate how do convert a String to a Byte variable with String’s getBytes() function.

public class TestByte
{    
	public static void main(String[] argv) {
 
		    String example = "This is an example";
		    byte[] bytes = example.getBytes();
 
		    System.out.println("Text : " + example);
		    System.out.println("Text [Byte Format] : " + bytes);
 
 
	}
}

Output

Text : This is an example
Text [Byte Format] : [B@187aeca