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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Logging Best Practices Revisited [Video]
  • Transactional Outbox Patterns Step by Step With Spring and Kotlin
  • Operator Overloading in Java
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices

Trending

  • Logging Best Practices Revisited [Video]
  • Transactional Outbox Patterns Step by Step With Spring and Kotlin
  • Operator Overloading in Java
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices
  1. DZone
  2. Data Engineering
  3. Data
  4. Java String length confusion

Java String length confusion

Jonatan Ivanov user avatar by
Jonatan Ivanov
·
Apr. 21, 14 · Interview
Like (7)
Save
Tweet
Share
16.18K 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.

Trending

  • Logging Best Practices Revisited [Video]
  • Transactional Outbox Patterns Step by Step With Spring and Kotlin
  • Operator Overloading in Java
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: