DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples
  • Proper Java Exception Handling
  • Generics in Java and Their Implementation

Trending

  • Create Your Own AI-Powered Virtual Tutor: An Easy Tutorial
  • Rethinking Recruitment: A Journey Through Hiring Practices
  • Java Virtual Threads and Scaling
  • Debugging Core Dump Files on Linux - A Detailed Guide
  1. DZone
  2. Data Engineering
  3. Data
  4. Java String length confusion

Java String length confusion

By 
Jonatan Ivanov user avatar
Jonatan Ivanov
·
Apr. 21, 14 · Interview
Likes (7)
Comment
Save
Tweet
Share
17.6K Views

Join the DZone community and get the full member experience.

Join For Free

Facts and Terminology

As you probably know, Java uses UTF-16 to represent Strings. In order to understand the confusion about String.length(), you need to be familiar with some Encoding/Unicode terms.

Code Point: A unique integer value which represents a character in the code space.

Code Unit: A bit sequence used to encode characters (Code Points). One or more Code Units may be required to represent a Code Point.

UTF-16

Unicode Code Points are logically divided into 17 planes. The first plane, the Basic Multilingual Plane (BMP) contains the “classic” characters (from U+0000 to U+FFFF). The other planes contain the supplementary characters (from U+10000 to U+10FFFF).

Characters (Code Points) from the first plane are encoded in one 16-bit Code Unit with the same value. Supplementary characters (Code Points) are encoded in two Code Units (encoding-specific, see Wiki for the explanation).

Example

Character: A
Unicode Code Point: U+0041
UTF-16 Code Unit(s): 0041

Character: Mathematical double-struck capital A
Unicode Code Point: U+1D538
UTF-16 Code Unit(s): D835 DD38

As you can see here, there are characters which are encoded in two Code Units.

String.length()

Let’s take a look at the Javadoc of the length() method:

public int length()
Returns the length of this string. The length is equal to the number of Unicode code units in the string.

So if you have one supplementary character which consists of two code units, the length of that single character is two.

// Mathematical double-struck capital A
String str = "\uD835\uDD38";
System.out.println(str);
System.out.println(str.length()); //prints 2

Which is correct according to the documentation, but maybe it’s not expected.

~Solution

You need to count the code points not the code units:

String str = "\uD835\uDD38";
System.out.println(str);
System.out.println(str.codePointCount(0, str.length()));

See: codePointCount(int beginIndex, int endIndex)

References/Sources

  • The Java Language Specification
  • Unicode Glossary: Code Point
  • Wiki: Code Point
  • Unicode Glossary: Code Unit
  • Wiki: Code Unit
  • Wiki: Unicode
  • Wiki: UTF-16
  • Supplementary Characters in the Java Platform
  • Wiki: Unicode Planes
Java (programming language) UTF-16 Data Types Strings

Opinions expressed by DZone contributors are their own.

Related

  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples
  • Proper Java Exception Handling
  • Generics in Java and Their Implementation

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!