CharsetEncoder charset() method in Java with Examples Last Updated : 29 Mar, 2019 Comments Improve Suggest changes Like Article Like Report The charset() method is a built-in method of the java.nio.charset.CharsetEncoder returns the charset that created this encoder. Syntax: public final Charset charset() Parameters: The function does not accepts any parameter. Return Value: The function returns this encoder's charset. Below is the implementation of the above function: Program 1: Java // Java program to implement // the above function import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; public class Main { public static void main(String[] args) throws Exception { // Gets the encoder CharsetEncoder encoder = Charset.forName("UTF16").newEncoder(); // Prints CodingErrorAction System.out.println(encoder.charset()); } } Output: UTF-16 Program 2: Java // Java program to implement // the above function import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; public class Main { public static void main(String[] args) throws Exception { // Gets the encoder CharsetEncoder encoder = Charset.forName("UTF8").newEncoder(); // Prints CodingErrorAction System.out.println(encoder.charset()); } } Output: UTF-8 Reference: https://docs.oracle.com/javase/10/docs/api/java/nio/charset/CharsetEncoder.html#charset() Comment More infoAdvertise with us Next Article CharsetEncoder charset() method in Java with Examples G gopaldave Follow Improve Article Tags : Java Java-Functions Java-NIO package Java-CharsetEncoder Practice Tags : Java Similar Reads CharsetDecoder charset() in Java with examples CharsetDecoder.charset() is an in-built method in Java of CharsetDecoder class that returns the charset that created this decoder. Syntax: public final Charset charset() Parameter: The function does not accepts any parameter. Return value: The function returns the decoder's charset. Program below de 1 min read Charset compareTo() method in Java with Examples The compareTo() method is a built-in method of the java.nio.charset compares two charsets with each other. A comparison is done by their canonical names, without regard to case. Syntax: public final int compareTo?(Charset second) Parameters: The function accepts a single mandatory parameter second w 1 min read CharsetDecoder averageCharsPerByte() method in Java with Examples The averageCharsPerByte() method is a built-in method of the java.nio.charset.CharsetDecoder class which returns the average number of characters that will be produced for each byte of input. This heuristic value may be used to estimate the size of the output buffer required for a given input sequen 2 min read CharsetEncoder reset() method in Java with Examples The reset() method is a built-in method of the java.nio.charset.CharsetEncoder resets this encoder, and clears all the internal states if there are any. It also resets charset-independent state and also invokes the implReset method in order to perform any charset-specific reset actions. Syntax: publ 2 min read Charset defaultCharset() method in Java with Examples The defaultCharset() method is a built-in method of the java.nio.charset which returns the charset object for the default charset. The default charset is basically determined by the Java virtual machine and it basically depends on the charset which is in the underlying operating system of the machin 1 min read Like