java.nio.charset.Charset Class in Java Last Updated : 07 Nov, 2023 Comments Improve Suggest changes 3 Likes Like Report In Java, Charset is a mapping technique used in Java to map the 16-bit Unicode sequence and sequences of bytes. It is also used to encode and decode the string data text into different character encoding. It comes under java.nio.charset.Charset package. The charset must begin with a number or letter. Every charset can decode and encode. For constructing a map that contains every charset, support is available in JVM(Java Virtual Machine). Methods of Charset Method Description newDecoder() Creates a new decoder newEncoder() Creates a new encoder getName() Returns the canonical name aliases() Returns an array of aliases isSupported() Tests whether this charset is supported by the current Java virtual machine Classes of CharsetClass Description Charset this is a named mapping between characters and bytes CharsetDecoder this class decodes bytes into characters CharsetEncoder this class encodes character into bytes CoderResult it is a description of the result state of a coder CodingErrorAction it detects coding errors and take error action Standard charsetsStandard Charsets Description US-ASCII 7 bit ASCII characters. Represents the basic English alphabet and some control characters. ISO-8859-1 ISO Latin Alphabet No. 1 that covers the Latin script and some common symbols. UTF-8 8-bit UCS Transformation which consists of most of the characters(from different languages). UTF-16BE 16-bit UCS Transformation Format in this characters are encoded using big-endian byte UTF-16LE 16-bit UCS Transformation Format in this characters are encoded using little-endian byte order. UTF-16 16-bit UCS Transformation Format this is often used for internal text processing. Using a charset Encoding a string into sequence of bytesEncoded String into a sequence of bytes using the given charset, storing the result into a new byte array. public byte[] getBytes(Charset charset);Example:java.nio.charset.Charset charset = java.nio.charset.Charset.forName("ASCII");byte[] byteArray = "Hi".getBytes(charset);Java Charset Example Java public class Main { public static void main(String[] args) { String s= "GFG"; java.nio.charset.Charset charSet = java.nio.charset.Charset.forName("ASCII"); byte[] byteArr= s.getBytes(charSet); System.out.println("byteArr of \"GFG\" with charsetName \"ASCII\" = " + byteArr); for (byte a : byteArr) { System.out.println(a); } } } OutputbyteArr of "GFG" with charsetName "ASCII" = [B@3af49f1c 71 70 71 Create Quiz Comment O opkrchauhan Follow 3 Improve O opkrchauhan Follow 3 Improve Article Tags : Java Geeks Premier League Java-Classes Java-nio-charset package Geeks Premier League 2023 +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like