Text Encoding in Java: Ultimate Guide to Character Sets – 2025

Text Encoding in Java

Java is a language for the Internet. Since the people of the Net speak and write in many different human languages, Java must be able to handle many languages as well.

One way in which Java supports internationalization is through the Unicode character set. Unicode is a worldwide standard that supports the scripts of most languages.

Java bases its character and string data on the Unicode 4.0 standard, which uses 16 bits to represent each symbol.

The Java source code can be written in Unicode and stored in various character encodings, including its full 16-bit form and ASCII-encoded Unicode character values.  

Java is a user-friendly language for non-English-speaking programmers. They can use their native language for class, method, and variable names, as well as for the app’s text.

The Java char type and String objects inherently support Unicode values. However, if you are concerned about handling two-byte characters, you can relax.  

The String API renders the character encoding transparent for you. Unicode is also highly compatible with ASCII, the most prevalent character encoding for English.  

Comments in Java

Java supports both C-style block comments delimited by /* and */ and C++-style line comments indicated by //.

* This is a

            multiline

                comment. */

    // This is a single-line comment

    // and so // is this

Block remarks can encompass extensive text segments and possess both an initial and final sequence. However, they cannot be “nested,” which means that the compiler will become perplexed if a block comment is nested within a block comment.

Extra // indicators within a single line have no effect; single-line comments are delimited by the end of a line and consist of only a start sequence.  

Line comments are advantageous for brief remarks within methods; they do not interfere with block comments, allowing you to comment out more prominent sections of code that contain them.

Javadoc Comments

A block comment that starts with /** indicates a special doc comment. Automated documentation generators, like the JavaDoc tool in the JDK, are made to extract doc comments.  

Like a traditional block comment, a doc comment ends with the following: */. Lines that start with @ in the doc comment are read as special instructions that provide the documentation generator with source code information. Although it is optional, it is customary for each line of a doc comment to start with a *.

Javadoc reads the source code and extracts the embedded comments and @ tags to provide HTML documentation for classes. In this instance, the class documentation displays author and version information due to the tags. Hypertext links to the relevant class documentation are created via the @see tags.

The @deprecated tag is especially relevant to the compiler when it examines the doc comments, as it indicates that the method is outdated and should be avoided in new programs.  

Whenever you utilize a deprecated feature in your code, a warning message can be issued since the built class file indicates the function is deprecated.

Javadoc as metadata

In doc comments, Javadoc tags serve as metadata on the source code, adding descriptive details about the code’s structure or contents that aren’t technically part of the application.  

A few other tools have previously expanded the idea of Javadoc-style tags to incorporate various types of metadata about Java programs.  

The new annotations feature in Java 5.0 made a more structured and expandable approach to adding metadata to Java classes, methods, and variables possible. 

What is Text Encoding in Java?

Text encoding in java converts readable text into bytes that can be stored and used by computers. Because computers only understand binary data (0s and 1s), we need a methodical way to turn letters into numbers.

Character Sets vs Encodings – Text encoding in Java

Character Set (Charset): A group of characters linked to unique integers (code points).

Example:

Unicode assigns ‘A’ to code point 65, ‘B’ to 66, etc.

Encoding: The rules for changing code points into bytes

Example:

UTF-8, UTF-16, and ASCII tell you how to store those integers as bytes.

Common Text Encodings in Java

ASCII (American Standard Code for Information Interchange)

It uses 7-bit encoding to support 128 characters. It includes letters, numbers, and basic symbols in English. Only English text is allowed.

ISO-8859-1 (Latin-1)

It has 8-bit encoding supporting 256 characters. It extends ASCII with Western European characters in ISO-8859-1. Each character = exactly 1 byte

UTF-16

UTF-16 variable-length encoding (two or four bytes per character) and Java’s internal string representation are good for languages with many non-ASCII characters.

String Handling in Java

In Java, strings are stored as UTF-16, but when you read from files or the internet, you must handle encoding yourself.

Key Classes for Encoding

String Methods:

java

byte[] bytes = string.getBytes(“UTF-8”);

The above statements convert a string to bytes using a specific encoding.

String string = new String(bytes, “UTF-8”);

The above statement creates a string from bytes using a specific encoding.

Charset Class:

java

Charset utf8 = StandardCharsets.UTF_8;

byte[] bytes = string.getBytes(utf8);

String decoded = new String(bytes, utf8);

InputStreamReader/OutputStreamWriter:

java

// Reading with specific encoding

FileInputStream fis = new FileInputStream(“file.txt”);

InputStreamReader isr = new InputStreamReader(fis, “UTF-8”);

// Writing with specific encoding

FileOutputStream fos = new FileOutputStream(“file.txt”);

OutputStreamWriter osw = new OutputStreamWriter(fos, “UTF-8”);

Scroll to Top
Verified by MonsterInsights