- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java String getBytes() method example.
The getBytes() method encodes the current String into a sequence of bytes using the given charset, storing the result into a new byte array.
Example
import java.lang.*; public class StringDemo { public static void main(String[] args) { try { String str1 = "admin"; System.out.println("string1 = " + str1); //copy the contents of the String to a byte array byte[] arr = str1.getBytes(); String str2 = new String(arr); //print the contents of the byte array System.out.println("new string = " + str2); } catch(Exception e) { System.out.print(e.toString()); } } }
Output
string1 = admin new string = admin
- Related Articles
- Java String compareTo() Method example.
- Java String substring() Method example.
- Java String endsWith() method example.
- Java String equals() method example.
- Java String equalsIgnoreCase() method example.
- Java String format() method example.
- Java String getChars() method example.
- Java String indexOf() method example.
- Java String intern() method example.
- Java String isEmpty() method example.
- Java String lastIndexOf() method example.
- Java String length() method example.
- Java String replace() method example.
- Java String split() method example.
- Java String startsWith() method example.

Advertisements