Java String getBytes()24 Mar 2025 | 3 min read The Java String class getBytes() method does the encoding of string into the sequence of bytes and keeps it in an array of bytes. SignatureThere are three variants of getBytes() method. The signature or syntax of string getBytes() method is given below: Parameterscharset / charsetName - The name of a charset the method supports. ReturnsSequence of bytes. Exception ThrowsUnsupportedEncodingException: It is thrown when the mentioned charset is not supported by the method. Internal implementationString class getBytes() Method ExampleThe parameterless getBytes() method encodes the string using the default charset of the platform, which is UTF - 8. The following two examples show the same. ExampleCompile and RunOutput: 65 66 67 68 69 70 71 Java String class getBytes() Method Example 2The method returns a byte array that again can be passed to the String constructor to get String. ExampleCompile and RunOutput: 65 66 67 68 69 70 71 ABCDEFG Java String class getBytes() Method Example 3The following example shows the encoding into a different charset. Output: The input String is : Welcome to TpointTech. After converted into UTF-16 the String is : -2-10870101010809901110109010103201160111032074097011809708401120111010501100116046 After converted into UTF-16BE the String is : 0870101010809901110109010103201160111032074097011809708401120111010501100116046 After converted into ISO-8859-1 the String is : 871011089911110910132116111327497118978411211110511011646 After converted into UTF-16LE the String is : 8701010108099011101090101032011601110320740970118097084011201110105011001160460 Java String class getBytes() Method Example 4The following example shows when the charset is not supported by the getBytes() method, UnsupportedEncodingException is thrown. ExampleCompile and RunOutput: /StringGetBytesExample4.java:11: error: unreported exception UnsupportedEncodingException; must be caught or declared to be thrown byte[] byteArr = str.getBytes("UTF-17"); ^ 1 error Next TopicJava String getChars() |
Java String.join() Method The Java String class join() method returns a string joined with a given delimiter. In the method, the delimiter is copied for each element. The join() method is included in the Java string since JDK 1.8. There are two types of join() methods...
5 min read
Java The Java String class lastIndexOf() method returns the last index of the given character value or substring. If it is not found, it returns -1. The index counter starts from zero. Signature There are four types of lastIndexOf() method in Java. The signature of the methods...
3 min read
» « Java Java, being a versatile and widely-used programming language, provides a rich set of functionalities for handling strings. One powerful feature that developers often leverage is the substring method. The method allows us to extract a portion of a string, providing flexibility in manipulating and...
5 min read
Java The java string toUpperCase() method returns the string in uppercase letter. In other words, it converts all characters of the string into upper case letter. The toUpperCase() method works same as toUpperCase(Locale.getDefault()) method. It internally uses the default locale. Internal implementation public String toUpperCase(Locale locale)...
7 min read
Java String The Java String class method compares the two given strings on the basis of the content of the string irrespective of the case (lower and upper) of the string. It is just like the equals() method but doesn't check the case sensitivity....
3 min read
Java The charAt() method is a fundamental tool in Java for accessing individual characters within a string. In this section, we will delve into the intricacies of the charAt() method, exploring its syntax, functionality, and common use cases. The Java String class charAt() method returns...
4 min read
» « Java The Java String class format() method returns the formatted string by given locale, format and arguments. If we do not specify the locale in String.format() method, it uses the default locale by calling Locale.getDefault() method. The format() method of the Java Sting class is the same...
8 min read
Java Method The Java String class intern() method returns the interned string. It returns the canonical representation of string. It can be used to return string from memory if it is created by a new keyword. It creates an exact copy of the heap string object...
4 min read
Java The java string toLowerCase() method returns the string in lowercase letter. In other words, it converts all characters of the string into lower case letter. The toLowerCase() method works same as toLowerCase(Locale.getDefault()) method. It internally uses the default locale. Internal implementation public String toLowerCase(Locale locale) { ...
8 min read
» « Java The java string split() method splits this string against given regular expression and returns a char array. Internal implementation public String[] split(String regex, int limit) { /* fastpath if the regex is a ...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India