BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java – String getBytes() Method example

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

The getBytes() method encodes a given String into a sequence of bytes and returns an array of bytes. The method can be used in below two ways:

public byte[] getBytes(String charsetName): It encodes the String into sequence of bytes using the specified charset and return the array of those bytes. It throws UnsupportedEncodingException – If the specified charset is not supported.
public byte[] getBytes(): It encodes the String using default charset method.

Example: getBytes() method

import java.io.*;
public class GetBytesExample{
   public static void main(String args[]){
       String str = new String("Hello");
       byte[] array1 = str.getBytes();
       System.out.print("Default Charset encoding:");
       for(byte b: array1){
           System.out.print(b);
       }
       System.out.print("\nUTF-16 Charset encoding:");
       try{
             byte [] array2 = str.getBytes("UTF-16");
             for(byte b1: array2){
                System.out.print(b1);
             }
             byte [] array3 = str.getBytes("UTF-16BE");
             System.out.print("\nUTF-16BE Charset encoding:");
             for(byte b2: array3){
                System.out.print(b2);
             }
        }catch(UnsupportedEncodingException ex){
             System.out.println("Unsupported character set"+ex);
        }
   }	
}

Output:

Default Charset encoding:72101108108111
UTF-16 Charset encoding:-2-10720101010801080111
UTF-16BE Charset encoding:0720101010801080111

In the above example we have done encoding using charset UTF -16 and UTF - 16BE, there are many other standard charset like:

  • US-ASCII: Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
  • ISO-8859-1: ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
  • UTF-8: Eight-bit UCS Transformation Format
  • UTF-16BE: Sixteen-bit UCS Transformation Format, big-endian byte order
  • UTF-16LE: Sixteen-bit UCS Transformation Format, little-endian byte order
  • UTF-16: Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark.

Reference:

http://docs.oracle.com/javase/7/docs/api/java/nio/charset/Charset.html

Top Related Articles:

  1. System.out.println() in Java explained with examples
  2. Java String contains() method
  3. Java Integer bitCount() Method
  4. Java 9 – Private methods in Interfaces (with examples)
  5. What is break Keyword in Java

Tags: Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap