String Length

Su Suriya Ravichandran Updated 14 Nov 2026
6 min read ·Lesson 92 of 306

length() returns the number of characters in a string, or more precisely, the number of UTF-16 code units (char values) it contains. For everyday text in most languages, that is the same as the number of characters you see. For emoji, some symbols, and text that uses combining marks, the visible character count can differ. This lesson covers length(), the indexes it defines, how to use it safely, and how to measure text correctly when the difference matters.

Using length()

String course = "Java Basics";
System.out.println(course.length());      // 11  (the space counts)
System.out.println("".length());          // 0
System.out.println("  ".length());        // 2   (whitespace counts)

length() is a method on String, written with parentheses. This differs from arrays (array.length, a field) and collections (list.size()).

TypeCount expression
String, StringBuilders.length()
Arrayarr.length
List, Set, Mapc.size()

Calling length() on null throws NullPointerException, so check for null first when a value may be missing.

Indexes and length()

Characters are numbered from 0 to length() - 1:

 J  a  v  a     B  a  s  i  c  s
 0  1  2  3  4  5  6  7  8  9  10      length() = 11
String s = "Java Basics";
char first = s.charAt(0);                  // 'J'
char last = s.charAt(s.length() - 1);      // 's'
s.charAt(s.length());                      // StringIndexOutOfBoundsException

Index-based methods use length() as their natural boundary:

s.substring(5);                     // "Basics": from index 5 to the end
s.substring(0, 4);                  // "Java": end index is exclusive
s.substring(s.length() - 3);        // "ics": the last 3 characters

substring(begin, end) returns end - begin characters. end may equal length() but not exceed it.

Common Uses of length()

Validating input

static String validatePassword(String pw) {
    if (pw == null || pw.length() < 10) {
        return "Password must be at least 10 characters";
    }
    if (pw.length() > 128) {
        return "Password is too long";
    }
    return "OK";
}

Looping over characters

String text = "Hello, World";
int upper = 0;
for (int i = 0; i < text.length(); i++) {
    if (Character.isUpperCase(text.charAt(i))) upper++;
}
System.out.println(upper);   // 2

Padding and alignment

String id = "42";
String padded = "0".repeat(Math.max(0, 6 - id.length())) + id;   // "000042"

String.format("%06d", 42) produces the same result for numbers.

Empty and blank checks

isEmpty() is equivalent to length() == 0. Whitespace still counts toward the length, so use isBlank() to treat whitespace-only strings as empty, or measure after stripping:

String input = "   Ada   ";
System.out.println(input.length());          // 9
System.out.println(input.strip().length());  // 3

When length() Is Not the Number of Visible Characters

Java strings are stored as UTF-16. Characters in Unicode's Basic Multilingual Plane (Latin, Greek, Cyrillic, Arabic, Devanagari, Tamil, Chinese, Japanese, and most other scripts) use one char each. Characters outside it, including most emoji and some rare scripts and symbols, use two char values, called a surrogate pair.

There are three different things you might want to count:

MeasureMethodCounts
UTF-16 code unitss.length()char values; what indexes are based on
Unicode code pointss.codePointCount(0, s.length())Individual Unicode characters
Grapheme clustersBreakIterator.getCharacterInstance()What a reader perceives as one character
String launch = "Launch 🚀";
System.out.println(launch.length());                           // 9: the rocket uses 2 chars
System.out.println(launch.codePointCount(0, launch.length())); // 8

Grapheme clusters go further. A single visible symbol can consist of several code points: a base letter plus combining marks (common in Tamil and other Indic scripts), an emoji plus a skin-tone modifier, or a family emoji joined with invisible joiner characters. The practical example below compares all three measures.

Breaking surrogate pairs

Index-based operations work on char positions, so they can cut a surrogate pair in half, producing an invalid character that displays as ? or a replacement box:

String post = "Shipping the new Java course today 🚀🚀🚀";
String cut = post.substring(0, 36);       // ends with half of the first rocket

To cut safely, convert a number of code points into a char offset with offsetByCodePoints:

int end = post.offsetByCodePoints(0, 35);  // char index after the first 35 code points
String safe = post.substring(0, end);

For most business text (names, addresses, product descriptions in a single script), length() is entirely adequate. Use code points or grapheme clusters when limits are shown to users ("280 characters remaining"), when truncating user-generated content, or when reversing or splitting text that may contain emoji.

Length in Bytes

The number of characters is different again from the number of bytes a string occupies when encoded for a file, database column, or network message:

String cafe = "Café";
System.out.println(cafe.length());                                         // 4
System.out.println(cafe.getBytes(StandardCharsets.UTF_8).length);          // 5: é takes 2 bytes
System.out.println("வணக்கம்".getBytes(StandardCharsets.UTF_8).length);     // 21

In UTF-8, ASCII characters take 1 byte, most European accented letters 2, most Indic and CJK characters 3, and emoji 4. When a database column or protocol field has a byte limit, measure the encoded bytes, not length().

Practical Example: Measuring Text for a Post Composer

A social media composer needs to show a character count and truncate long posts without breaking emoji.

import java.nio.charset.StandardCharsets;
import java.text.BreakIterator;

public class Len {
    static int graphemes(String s) {
        BreakIterator it = BreakIterator.getCharacterInstance();
        it.setText(s);
        int count = 0;
        while (it.next() != BreakIterator.DONE) count++;
        return count;
    }

    static String truncate(String s, int maxCodePoints) {
        if (s.codePointCount(0, s.length()) <= maxCodePoints) return s;
        int end = s.offsetByCodePoints(0, maxCodePoints - 1);
        return s.substring(0, end) + "…";
    }

    public static void main(String[] args) {
        String[] samples = {"Java", "Café", "வணக்கம்", "Launch 🚀", "👍🏽"};
        for (String s : samples) {
            System.out.printf("%-12s chars=%d codePoints=%d utf8Bytes=%d graphemes=%d%n", "\"" + s + "\"", s.length(),
                    s.codePointCount(0, s.length()),
                    s.getBytes(StandardCharsets.UTF_8).length, graphemes(s));
        }
        String tweet = "Shipping the new Java course today 🚀🚀🚀";
        System.out.println(truncate(tweet, 36));
        System.out.println(tweet.length() + " " + tweet.indexOf("🚀"));
        String cut = tweet.substring(0, 36);
        System.out.println(Character.isHighSurrogate(cut.charAt(cut.length() - 1)));
    }
}

Output (on a terminal configured for UTF-8, which may require running with -Dstdout.encoding=UTF-8 on some systems):

"Java"       chars=4 codePoints=4 utf8Bytes=4 graphemes=4
"Café"       chars=4 codePoints=4 utf8Bytes=5 graphemes=4
"வணக்கம்"    chars=7 codePoints=7 utf8Bytes=21 graphemes=5
"Launch 🚀"  chars=9 codePoints=8 utf8Bytes=11 graphemes=8
"👍🏽"       chars=4 codePoints=2 utf8Bytes=8 graphemes=1
Shipping the new Java course today …
41 35
true

The table shows how the measures diverge:

  • ASCII text (Java) has the same value for all four.
  • Café has 4 characters but 5 UTF-8 bytes.
  • The Tamil word has 7 char values and 7 code points but 5 grapheme clusters, because vowel signs and the virama combine with the preceding consonant into a single visible unit.
  • The rocket emoji adds 2 to length() but 1 to the code point count.
  • The thumbs-up with a skin tone is 4 char values, 2 code points, and a single visible symbol.

truncate counts and cuts in code points, so the post is shortened cleanly before the first rocket and an ellipsis is added. The last two lines show why this matters: the first rocket starts at char index 35, so a naive substring(0, 36) ends with a lone high surrogate, which would display as a broken character. (The %-12s padding also counts char values, which is why the columns do not line up perfectly for the emoji rows.)

0 Comments

Reviewed before they appear

No comments yet.

Java
Java Introduction Java Features Java Syntax Java Comments Java Statements Java Output Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Numbers Java Math Java Booleans Java User Input Java Conditions Java If Else Java Switch Java While Loop Java For Loop Java Break Java Continue Java Arrays Java Methods Java Parameters Java Return Values Java Scope Java Recursion Install Java Install JDK Java IDEs Java JDK Java JRE Java JVM Java Compiler Java File Compile Java Run Java JShell Basics Java Packages Java OOP Java Classes Java Objects Class Methods Constructors Constructor Overload this Keyword static Keyword final Keyword Encapsulation Inheritance Polymorphism Abstraction Access Modifiers super Keyword Method Overload Method Override Instanceof Operator Nested Classes Inner Classes Anonymous Classes Enums Records Array Basics Array Length Loop Arrays Multidimensional Arrays Array Copy Array Sorting Array Search ArrayList LinkedList HashSet TreeSet HashMap TreeMap Queue Interface Deque Interface Stack Class Iterator List Interface Set Interface Map Interface Collections Class Comparable Comparator Generics Basics Generic Classes Generic Methods Wildcards String Basics String Length String Methods String Concatenation String Compare String Search String Replace String Split String Format StringBuilder StringBuffer Text Blocks Regular Expressions Number Types Integer Class Float Class Double Class BigInteger BigDecimal Math Class Random Numbers Number Format Date Basics LocalDate LocalTime LocalDateTime ZonedDateTime Date Formatting Date Parsing Period Class Duration Class Java Instant Class Java Exception Basics Java try...catch Java Multiple Catch Blocks Java finally Block Java throw Keyword Java throws Keyword Java Checked Exceptions Java Unchecked Exceptions Java Custom Exceptions Java try-with-resources Java Exception Chaining Java File Basics Java Create Files Java Read Files Java Write Files Java Delete Files Java File Paths Java File Class Java BufferedReader Java BufferedWriter Java FileInputStream Java FileOutputStream Java Object Streams Java Directory Handling Java NIO Basics Java Files API Java Input Java Scanner Class Java Console Input Java PrintStream Java InputStream Java OutputStream Java Reader Class Java Writer Class Java Buffered Streams Java Data Streams Java Generics Introduction Java Generic Types Java Generic Methods Java Bounded Types Java Wildcards Java Type Erasure Java Generic Interfaces Java Generic Collections Java Lambda Basics Java Lambda Syntax Java Functional Interfaces Java Method References Java Stream API Java Stream Creation Java Stream Filter Java Stream Map Java Stream Reduce Java Stream Sort Java Stream Collect Java Collectors Class Java Optional Class Java Parallel Streams Java Threads: Thread Basics Create a Thread in Java Java Thread Class Java Runnable Interface Java Thread States Java Thread Sleep Java Thread Join Java Thread Priority Java Synchronization Java Thread Safety Java Race Conditions Java Deadlocks Java Executor Service Java Thread Pools Java Callable Interface Java Future Interface Java CompletableFuture Java Concurrent Collections Java Atomic Classes Java Virtual Threads Java Networking Basics Java URL Class Java URI Class Java HttpClient Java HTTP Requests Java HTTP Responses Java Socket Basics Java ServerSocket Java DatagramSocket Java WebSocket Basics Java JDBC Basics Java JDBC Setup Java Database Connection Java JDBC Connection Interface Java JDBC Statement Java PreparedStatement Java ResultSet Java JDBC CRUD Operations Java JDBC Transactions Java JDBC Batch Update Java Connection Pooling with HikariCP Java JPA Basics: Entities, EntityManager and JPQL Java Hibernate Basics: SessionFactory, Session and HQL Package in Java: Basics Explained with Examples Create Package in Java: Step-by-Step Guide with Examples Import Package in Java: The import Statement Explained Access Control in Java: Packages, Classes and Members JAR File in Java: Create, Run and Inspect JAR Files Java Modules: Introduction to the Java Module System module-info in Java: Every Module Directive Explained Java Module Path: How --module-path Finds Your Modules Java Classpath: How -cp and CLASSPATH Find Your Classes Java Annotations: Basics of @ Metadata in Java Java Built-in Annotations: @Override, @Deprecated and More Java Custom Annotations: Create Your Own @interface Java Annotation Targets: @Target and ElementType Explained Java Reflection API: Inspect and Use Classes at Runtime Java Class Metadata: Names, Modifiers, Hierarchy and Generic Java Dynamic Objects: Create Objects Dynamically With Reflec Java Dynamic Proxy: Intercept Method Calls With Proxy and In Java Security Java Secure Coding Java Password Hashing Java Input Validation Java Cryptography Basics Java KeyStore Basics Java TLS Basics Java SecureRandom Java JUnit Basics Java JUnit Test Methods JUnit Assertions JUnit Test Fixtures JUnit Parameterized Tests Mockito in Java Java Mock Objects Java Integration Testing Java Code Coverage Maven in Java Create a Maven Project in Java Maven Dependencies Maven Plugins Gradle in Java Gradle Build Gradle Dependencies Build Executable JAR in Java Build WAR File in Java HTTP Basics in Java Java Servlet Basics Servlet Lifecycle in Java HttpServletRequest in Java HttpServletResponse in Java JSP in Java JSTL in JSP REST API in Java JSON in Java Java Web Application Spring Framework Basics Spring Boot Tutorial Spring MVC Spring Data JPA Spring Security Spring Boot REST API Jakarta EE Basics Java Deployment JAR Deployment Java WAR Deployment Run Java as a Service Docker Java: Dockerize a Java Application Java Environment Variables and Configuration Java Logging Basics Java Production Setup Java JVM Options Java JVM Monitoring Java Hello World App Java Calculator App Java Number Guess Game Java Student Manager Java Library System Java Bank App Java Todo App Java File Manager Java Quiz App Java REST API Project Java Chat Server Java Employee Manager Java E-commerce API
Ask about this post
AI Ask about this post

Ask questions about String Length and get answers drawn from it.

Signed-in readers only.