getBytes() in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

The getBytes() function is an inbuilt method of Java that converts a string into a sequence of bytes. The getBytes() method takes the string which is to be converted and returns the byte array.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Syntax of getBytes() in Java

Let's understand the syntax of getBytes() in java. The getBytes() in java uses the string that is to be converted into the array of bytes. getBytes() function takes optional parameters as charset or string.

Syntax:

Parameters of getBytes() in Java

Let's understand the parameters of getBytes() in java. getBytes() function takes an optional parameter that can be charset or a string according to which string has to be encoded while conversion into bytes.

  • charset: There are different CharSet available in java:
    • UTF-8: 8-bit UCS Transformation Format
    • UTF-16: 16-bit UCS Transformation Format
    • UTF-16BE: 16-bit UCS Transformation Format, big-endian byte order
    • UTF-16LE: 16-bit UCS Transformation Format, little-endian byte order
    • US-ASCII: 7-bit ASCII
    • ISO-8859-1: ISO Latin Alphabet No. 1
  • String CharsetName: We can also use strings to specify the encoding type to getBytes(), we can pass the above charset values and they'll be processed as string values.

Return Values of getBytes() in Java

Return Type: byte array

The Return type of getBytes() is a byte array made from the given string value.

Sharpen Your Fundamentals with Free Learning

Exceptions of getBytes() in Java

The UnsupportedEncodingException occurs mainly in the getBytes() method due to the wrong encoding charset value passed as the string. Let's understand this using an example.

Output:

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Example of getBytes() in Java

Lets take a look at a short example on how the getBytes function works, we will simply apply the getBytes() function with no parameter so the string will be encoded with the default charset value.

Output: The string is simply converted to its corresponding ASCII encoding values:

What is getBytes() in Java?

Let's suppose you have to convert the given string to its corresponding bytearray, then getbytes() function comes to play and converts the entire string value into a sequence of bytes and returns the byte array. getbytes() method also takes some optional parameter for charset so that we can convert the string to particular charset.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

More Examples of getBytes() in Java

Example 1: getBytes() Without Any Parameters

Lets take a look at an example where we will simply apply the getBytes() function with no parameter so the string will be encoded with default charset value.

Output: The string is simply converted to its corresponding ASCII encoding values:

Example 2: getBytes() With CharSet Parameter

Let's take a look at an example where we will be passing the above discussed CharSet values. We will import java.nio.charset.Charset to use CharSet in our java program.

Output: The string is simply converted to its corresponding ASCII encoding values with corresponding Charset Values:

Example 3: getBytes() With String Parameter

Let's take a look at an example where we will be passing the above-discussed CharSet values as String values. we have to wrap the code inside the try-catch block while using the getBytes() with the string parameter.

Output: The string is simply converted to its corresponding ASCII encoding values with corresponding Charset Values, The American Standard Code for Information Interchange (ASCII) is a character encoding standard for electronic communication.

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 parameter as charset or string.
  • The getBytes() function throws UnsupportedEncodingException due to the wrong encoding charset value passed.