Why Does My Java Code Convert an Integer to an Unprintable Character?
Converting Integer to Character in Java: A Comparison
Understanding character and integer conversions in Java can be tricky. Let's examine the following code snippets:
int a = 1; char b = (char) a; System.out.println(b);
Expectedly, this code should print the character '1'. However, it produces an empty output. To understand this, we need to delve deeper into the type conversion process.
The assignment char b = (char) a converts the integer a (which is the number 1) to a character. This conversion actually stores the character with the Unicode code point corresponding to the integer value. For example, the Unicode code point for '1' is 49, whereas the Unicode code point for the start-of-heading character is 1. Since the integer a is 1, it gets converted to the start-of-heading character, which is not printable.
In contrast, the following code snippet:
int a = '1'; char b = (char) a; System.out.println(b);
Produces the expected output '1'. This is because the assignment int a = '1' initializes a with the ASCII value of '1' (which is 49) rather than the integer value 1.
To convert an integer to a character as intended in the first snippet, you can use the following approaches:
-
Use the Character.forDigit() method: This converts a digit between 0 and 9 to its corresponding character.
int a = 1; char b = Character.forDigit(a, 10); System.out.println(b); // Prints '1'
Copy after login -
Add 48 to the integer and cast: This approach relies on the fact that the Unicode code points for digits are 48 higher than the digit values.
int a = 1; char b = (char) (a + 48); System.out.println(b); // Prints '1'
Copy after login -
Use the Character.toChars() method: This converts a Unicode code point to its corresponding character array.
int a = 1; char[] b = Character.toChars(a); System.out.println(b[0]); // Prints '1'
Copy after login
The above is the detailed content of Why Does My Java Code Convert an Integer to an Unprintable Character?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
