Home >>Java String Methods >Java String getBytes() method
Java method getBytes() in java string returns the string's byte array. In other words, it returns byte sequence.
public byte[] getBytes() { return StringCoding.encode(value, 0, value.length); }
public byte[] getBytes() public byte[] getBytes(Charset charset) public byte[] getBytes(String charsetName)throws UnsupportedEncodingException
public class StringGetBytesExample
{
public static void main(String args[])
{
String n1="UVWXYZ";
byte[] barr=n1.getBytes();
for(int i=0;i<barr.length;i++)
{
System.out.println(barr[i]);
}
}
}
public class StringGetBytesExample2
{
public static void main(String[] args)
{
String n1 = "UVWXYZ";
byte[] barr = n1.getBytes();
for(int i=0;i<barr.length;i++)
{
System.out.println(barr[i]);
}
String n2 = new String(barr);
System.out.println(n2);
}
}