String

Java String getBytes Example

We all know that every single piece of code consists of bytes. So do the Strings, this Java class type, that can represent text and words inside a Java program. In order to convert bytes into characters, Java needs to know what to represent, in which language, and what each character means in every language in the world.

This is the main reason for the existence of character encoding when converting bytes into Java Strings and vice versa. In our case, we want to convert or encode a Java String into a byte array, so we have to use the Java getBytes() method, from the java.lang.String class.

As you guessed, in this tutorial, we are going to discuss about the getBytes() method.

String getBytes() method:

The getBytes() method of Java String class, generally, converts or encodes a String into a byte array. This method accepts a charset as parameter, however if no charset is specified or can not be recognized, then the default charset of the system will be used. This default charset is actually provided by the system file encoding property, it is cached, and cannot be changed after the JVM starts. Moreover, if the file encoding property does not map to a known charset, then the UTF-8 encoding is specified. And, if the input on the overriding method is a charsetName and its not recognized, a UnsupportedEncodingException will be thrown.

Let’s see this method in more detail. The Java getBytes() from the String class has the following three forms:

  • public byte[] getBytes(): This method encodes a String into a sequence of bytes using JVMs default charset, returning as a result a new byte array.
  • public byte[] getBytes(Charset charset): This method encodes a String into a sequence of bytes using the given charset, returning as a result a new byte array
  • public byte[] getBytes(String charsetName): This method encodes a String into a sequence of bytes using the named charset, returning as a result a new byte array.

Example:

JavaStringGetBytes.java

package com.javacodegeeks.javabasics.string;

import java.io.*;
import java.nio.charset.Charset;

public class JavaStringGetBytes {

    public static void main(String args[]) {

        String Str1 = new String("JavaCodeGeeks");
        byte [] Str2;

        String chset = Charset.defaultCharset().name(); 
        System.out.println("Charset.defaultCharset(): " + chset); //prints the default Java charset

        try {
            Str2 = Str1.getBytes(); //the getBytes method
            System.out.println("Value: " + Str2);

            Str2 = Str1.getBytes("UTF-8"); //gives the method a String (Charset name) as input
            System.out.println("Value: " + Str2);

            Str2 = Str1.getBytes(Charset.defaultCharset()); //gives the method a Charset as input
            System.out.println("Value: " + Str2);

            Str2 = Str1.getBytes("ISO-8859-1"); //gives the method a String (Charset name) as input
            System.out.println("Value: " + Str2); 

            Str2 = Str1.getBytes("UnsupportedValue"); //this is an unsupported Charset name
            System.out.println("Value: " + Str2); 

        } catch (UnsupportedEncodingException e) { //catches the UnsupportedEncodingException when an unsupported charset is given
            System.out.println("Unsupported character set");
        }
    }
}

Output:

Charset.defaultCharset(): UTF-8
Value: [B@1c8825a5
Value: [B@2e5f8245
Value: [B@6197cc
Value: [B@19789a96
Unsupported character set

This was the Java String getBytes example.

Chryssa Aliferi

Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button