Java getBytes() method

    getBytes() method

    This method will allow you to provide the byte array of the string or you can say this method will return the bytes sequence.

    Method implementation-

    public byte[] getBytes() {  
    
            return StringCoding.encode(value, 0, value.length);  
    
        }  

    Syntax-

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

    Example-

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

    Output-

    Java GetBytes method