Java String getBytes() Method Last Updated : 29 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In Java, the getBytes() method of the String class converts a string into an array of bytes. This method is useful for encoding the strings into binary format, which can then be used for file I/O, network transmission, or encryption. It can be used with the default character set or with a specified charset.Example:The below example shows how the getBytes() method converts a string into bytes using the platform's default character set. Java // Java program to demonstrate the getBytes() // with default charset public class GetBytes { public static void main(String[] args) { // Define a string String s = "GeeksforGeeks"; // Convert the string to a byte array byte[] ba = s.getBytes(); for (byte b : ba) { System.out.print(b + " "); } } } Output71 101 101 107 115 102 111 114 71 101 101 107 115 Explanation: In the above example, the getBytes() method converts each character of the string into its corresponding ASCII value in bytes. The for-each loop is used to print each byte in the array.Syntax of getBytes() Methodpublic byte[] getBytes();Return Type: Returns a newly created byte array that contains the encoded sequence of bytes.Example of String getBytes() Method Using Specified Charset in JavaNow let us implement and accept the charset according to which string has to be encoded while conversion into bytes. There are many charset defined in the Charset class. Java // Java Program to Demonstrate // Working of getByte() Method import java.nio.charset.StandardCharsets; public class GetBytes { public static void main(String[] args) { String s = "Geeks for Geeks"; // Convert the string to a byte array // using UTF-8 byte[] ba1 = s.getBytes(StandardCharsets.UTF_8); // Convert the string to a byte array // using UTF-16 byte[] ba2 = s.getBytes(StandardCharsets.UTF_16); System.out.println("Byte Array (UTF-8): " + java.util.Arrays.toString(ba1)); System.out.println("Byte Array (UTF-16): " + java.util.Arrays.toString(ba2)); } } OutputByte Array (UTF-8): [71, 101, 101, 107, 115, 32, 102, 111, 114, 32, 71, 101, 101, 107, 115] Byte Array (UTF-16): [-2, -1, 0, 71, 0, 101, 0, 101, 0, 107, 0, 115, 0, 32, 0, 102, 0, 111, 0, 114, 0, 32, 0...Explanation: In the above example, we have used different charsets to see how characters are encoded into bytes. The UTF-8 encodes characters using variable-length bytes. The UTF-16 uses two bytes for most characters. Comment More infoAdvertise with us Next Article Java String charAt() Method A Astha Tyagi Follow Improve Article Tags : Java Java-Strings Java-Functions Practice Tags : JavaJava-Strings Similar Reads Java String charAt() Method String charAt() method in Java returns the character at the specified index in a string. The Index of the first character in a string is 0, the second character is 1, and so on. The index value should lie between 0 and length() - 1.If the index value is greater than or equal to the string length or 2 min read Boolean toString() Method in Java In Java, the toString() method of the Boolean class is a built-in method to return the Boolean value in string format. The Boolean class is a part of java.lang package. This method is useful when we want the output in the string format in places like text fields, console output, or simple text forma 2 min read Byte toString() method in Java with examples The toString() method of Byte class is a built in method in Java which is used to return a String value. public String toString() Syntax: ByteObject.toString() Return Value: It returns a String object, the value of which is equal to the value of the ByteObject. Below is the implementation of toStrin 2 min read Array getByte() Method in Java The java.lang.reflect.Array.getByte() is an inbuilt method in Java and is used to return the element present at the given index from the specified Array as a Byte.Syntax: Array.getByte(Object []array, int index) Parameters : This method accepts two mandatory parameters: array: The object array whose 3 min read Java String valueOf() Method The valueOf() method of the String class in Java helps to convert various data types like integers, floats, booleans, and objects into their string representations. It makes it simple to work with string manipulation, logging, and displaying data efficiently.Example:To convert a number into text for 3 min read TreeMap get() Method in Java The get() method of the TreeMap class in Java is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. If the key does not exist in the map, the method returns null.Syntax of TreeMap get() MethodtreeMap.get(Object key)Parameter: key: The method takes one paramete 2 min read YearMonth getYear() method in Java The getYear() method of YearMonth class in Java is used to get the value of the Year field from this YearMonth instance with which it is used. It returns the value of the year field as an integer from MIN_YEAR to MAX_YEAR. Syntax: public int getYear() Parameter: This method does not accepts any para 1 min read Object toString() Method in Java Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class, henceforth, it is a child of the Object class. If a class does not extend any other class then it is a direct child class of Object, and if it extends another class, then it is 3 min read Month get() method in Java The get() method is a built-in method of the Month ENUM which is used to get the corresponding integral value of the month-of-year values specified by this Month instance. Syntax: public int get(TemporalField field) Parameters: This method accepts a single parameter which is a temporal object and ca 1 min read IntBuffer get() methods in Java | Set 1 get() The get() method of java.nio.IntBuffer Class is used to reads the int at the given buffer's current position, and then increments the position. Syntax : public abstract int get() Return Value: This method returns the int value at the buffer's current position. Throws: This method throws Buffer 6 min read Like