Java String getBytes() Method
A string is an object representing a number of character values for Java. The Java string object is made up of a separate character value for each letter in the string. The char class represents the characters in Java. An array of char values can be written by users, which means the same thing as a string.
Example : String s=”HelloWorld”;
String getBytes() in Java
The Java String class getBytes() method does the encoding of string into the sequence of bytes and keeps it in an array of bytes.
Signature
There are three variants of the getBytes() method. The signature or syntax of string getBytes() method is given below:
- public byte[] getBytes()
- public byte[] getBytes(Charset charset)
- public byte[] getBytes(String charsetName)throws UnsupportedEncodingException
Parameters
charset / charsetName – The name of a charset the method supports.
Returns
Sequence of bytes
Exception Throws
UnsupportedEncodingException:
It is thrown when the mentioned charset is not supported by the method.
Internal implementation
public byte[] getBytes()
{
return StringCoding.encode(value, 0, value.length);
}
Java String class getBytes() Method Example
public class StringGetBytesExample
{
public static void main(String[] args)
{
String s1 = "ABCDEFG";
byte[] barr = s1.getBytes();
for(int i=0;i<barr.length;i++)
{
System.out.println(barr[i]);
}
}
}
Output:
65
66
67
68
69
70
71
String.getByte() Method in Java
In Java, getBytes() encodes a string into a sequence of bytes using the named character set and storing the result into a new byte array. This function can be implemented in two ways. Both ways are discussed below as follows:
- getBytes()
- getBytes(Charset charset)
Let us discuss and implement the first use case, which is as follows:
Java String getBytes()
This function takes no arguments and uses the default charset to encode the string into bytes. getbytes() function in Java is used to convert a string into a sequence of bytes and returns an array of bytes.
Syntax
public byte[] getBytes()
Java String getBytes(Charset charset)
Now let us implement and accept the charset according to which string has to be encoded while conversion into bytes. There are many charset defined and are discussed below.
Syntax
public byte[] getBytes(Charset charset);
- 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.
Advantages of String
- Text Processing: Strings are used to represent text in programming languages. They can be used to manipulate and process text in various ways, such as searching, replacing, parsing, and formatting.
- Data Representation: Strings can be used to represent other data types, such as numbers, dates, and times. For example, you can use a string to represent a date in the format “YYYY-MM-DD”, or a time in the format “HH:MM:SS”.
Disadvantages of String:
- Memory Consumption: Strings can consume a lot of memory, especially when working with large strings or many strings. This can be a problem in memory-constrained environments, such as embedded systems or mobile devices.
- Immutability: In many programming languages, strings are immutable, meaning that they cannot be changed once they are created. This can be a disadvantage when working with large or complex strings that require frequent modifications, as it can lead to inefficiencies and memory overhead.
- Performance Overhead: String operations can be slower than operations on other data types, especially when working with large or complex strings. This is because string operations often involve copying and reallocating memory, which can be time-consuming.
Conclusion
The getBytes() function is an inbuilt method of Java.
The getBytes() function converts a string into a sequence of bytes and returns a byte array.
getBytes() function takes optional parameters as charset or string.
The getBytes() function throws UnsupportedEncodingException due to the wrong encoding charset value passed.
