Follow on Facebook

Like and Share on Facebook

Sunday, January 25, 2015

Java String getBytes()


Java String getBytes()

The java string getBytes() method returns the byte array of the string. In other words, it returns sequence of bytes.

Signature

There are 3 variant of getBytes() method. The signature or syntax of string getBytes() method is given below:

public byte[] getBytes()  
public byte[] getBytes(Charset charset)  
public byte[] getBytes(String charsetName)throws UnsupportedEncodingException  

Returns

sequence of bytes.

Java String getBytes() method example

public class StringGetBytesExample{  
public static void main(String args[]){  
String s1="ABCDEFG";  
byte[] barr=s1.getBytes();  
for(int i=0;i<barr.length;i++){  
System.out.println(barr[i]);  
}  
}}  

Expected Output:
65
66
68
67
69
71
70

No comments:

Post a Comment