
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - String getBytes() Method
Description
The Java String getBytes() method is, used to encode(convert) this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array. In Java, an array is an object which contains elements of similar data types. A byte is the smallest addressable unit of memory.
Note: An int data type size is 4 bytes, a char data type size is 1 byte, and so on.
The getBytes() method accepts charset and string as a parameter that holds the value of charset and charset name. It throws an exception if the charset or charset name is invalid.
This method has three polymorphic variants with different parameters, as shown in the syntax below.
Syntax
Following is the syntax of the Java String getBytes() method −
public byte[] getBytes() // first syntax public byte[] getBytes(Charset charset) // second syntax public byte[] getBytes(String charsetName) // third syntax
Parameters
- charset − This is a named mapping between sequences of sixteen-bit Unicode code units and sequences of bytes.
- charsetName − This is the string.
Return Value
This method encodes this String into a sequence of bytes using the given values and returns the result in the form of a byte array.
Getting Bytes of a String Example
If we do not pass the default charset to the method, it encodes this String into a sequence of bytes by taking the default charset itself.
In the following program, we are instantiating the string class with the value "Hello World". Then, using the getBytes() method, we are trying to convert the current string into the sequence of bytes and store it in the byte array.
package com.tutorialspoint; import java.util.Arrays; public class GetByte { public static void main(String[] args) { //instantiate the string class String str = new String("Hello World"); System.out.println("The given string is: " + str); // create a byte array byte[] byte_arr; // using getBytes() method byte_arr = str.getBytes(); // print the byte array value System.out.println("The byte array is: " + Arrays.toString(byte_arr)); } }
Output
On executing the above program, it will produce the following result −
The given string is: Hello World The byte array is: [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
Getting Bytes of a String with UTF-8 Encoding Example
If we pass the charset to the getBytes() method, this method encodes this String into a sequence of bytes using the given charset.
In the following example, we are creating an object of the string class with the value "Java". Using the getBytes() method, we are trying to encode the current string into a byte array using the charset "UTF-8".
package com.tutorialspoint; import java.nio.charset.Charset; import java.util.Arrays; public class GetByte { public static void main(String[] args) { try { //create an object of the string class String str = new String("Java"); System.out.println("The given string is: " + str); // copy the contents of the String to a byte array System.out.println("The charset is: " + Charset.forName("UTF-8")); byte[] arr = str.getBytes(Charset.forName("UTF-8")); System.out.println("The byte array is: "); for(int i = 0; i<arr.length; i++) { System.out.print(arr[i] + " "); } } catch (Exception e) { System.out.print(e.toString()); } } }
Output
Following is the output of the above program −
The given string is: Java The charset is: UTF-8 The byte array is: 74 97 118 97
Getting Bytes of a String Using UTF-16 Encoding Example
If we pass the string charsetName as a parameter to the method, it encodes this String into a sequence of bytes using the given charset name.
In the following program, we are creating a string literal with the value "TutorialsPoint". Using the getBytes() method, we are trying to encode the current string into a byte array using the charset name "UTF-16".
package com.tutorialspoint; import java.nio.charset.Charset; public class GetByte { public static void main(String[] args) { try { // create a string literal String str= "TutoriaslPoint"; System.out.println("The given string is: " + str); System.out.println("The charset name is: " + Charset.forName("UTF-16")); // byte array with charset byte bval[] = str.getBytes("UTF-8"); // prints the byte array System.out.print("The byte array elements are: "); for (int i = 0; i < bval.length; i++) { System.out.print(bval[i] + " "); } } catch (Exception e) { e.printStackTrace(); System.out.println("The exception is: " + e); } } }
Output
The above program, produces the following output −
The given string is: TutoriaslPoint The charset name is: UTF-16 The byte array elements are: 84 117 116 111 114 105 97 115 108 80 111 105 110 116
Getting Exception while getting Bytes of a String Using Invalid Encoding Example
If the given charset or charset name is invalid, the getBytes() method throws an UnsupportedEncodingException.
In the following program, we are instantiating a string class with the value "Tutorix". Then, using the getBytes() method, we are trying to convert the current string into the sequence of the byte using the charset name "UTF-50". Since the charset name is invalid, the method throws an exception.
package com.tutorialspoint; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.util.Arrays; public class GetByte { public static void main(String[] args) { try { //instantiate the string class String str = new String("Tutorix"); System.out.println("The given string is: " + str); System.out.println("The charset name is: " + Charset.forName("UTF-50")); // copy the contents of the String to a byte array byte[] arr = str.getBytes("UTF-50"); System.out.println("The byte array is: " + Arrays.toString(arr)); } catch (UnsupportedEncodingException e) { System.out.print(e.toString()); } } }
Output
After executing the above program, it will produce the following output −
The given string is: Tutorix Exception in thread "main" java.nio.charset.UnsupportedCharsetException: UTF-50 at java.base/java.nio.charset.Charset.forName(Charset.java:528) at com.tutorialspoint.String.GetByte.main(GetByte.java:12)