title: Java String.getBytes( charsetName ) vs String.getBytes ( Charset object )
body: <p>I need to encode a String to byte array using UTF-8 encoding. I am using Google guava, it has Charsets class already define Charset instance for UTF-8 encoding. I have 2 ways to do:</p>
  
  <ol>
  <li><p>String.getBytes( charsetName )</p>
  
  <pre><code>try {        
      byte[] bytes = my_input.getBytes ( "UTF-8" );
  } catch ( UnsupportedEncodingException ex) {
  
  }
  </code></pre></li>
  <li><p>String.getBytes( Charset object )</p>
  
  <pre><code>// Charsets.UTF_8 is an instance of Charset    
  
  byte[] bytes = my_input.getBytes ( Charsets.UTF_8 );
  </code></pre></li>
  </ol>
  
  <p>My question is which one I should use? They return the same result. For way 2 - I don't have to put try/catch! I take a look at the Java source code and I see that way 1 and way 2 are implemented differently.</p>
  
  <p>Anyone has any ideas?</p>