Flash cards
Review the key moves
1/4
Core idea
What is the main idea behind Java String getBytes() Method?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
1Quick choice
Which statement best captures the main point of this lesson?
2Fill blank
Complete the missing token from the example code.
___ myStr = "Hello";3Order
Put the learning moves in the order that makes the concept easiest to apply.
The getBytes() method converts a string into an array of bytes.
Definition and Usage
Java String getBytes() Method
❮ String Methods
Example
String myStr = "Hello";
byte[] result = myStr.getBytes();
System.out.println(result[0]);Definition and Usage
The getBytes() method converts a string into an array of bytes.
The encoding of the bytes depends on the charset argument.
If the charset argument is not provided then the bytes will be encoded using the system's default character set.
One of the following
public byte[] getBytes(Charset
charset )public byte[] getBytes()Parameter Values
| Parameter | Description |
|---|---|
| charset | Optional. A Charset object specifying which character set should be used when encoding the string as bytes. |
Technical Details
| Returns: | A byte array representing the bytes of the string with a specific encoding. |
|---|---|
| Java version: | Any. charset parameter added in version 1.6. |