String length Java Example

In this example, we are going to see how you can find out the length of a String in Java. By length string, we mean the number of Unicode characters or Code Units.

1. What is String in Computer Programming

In computer programming, a string is traditionally sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements to be mutated and the length changed, or it may be fixed (after creation). A string is generally considered as a data type and is often implemented as an array data structure of bytes (or words) that stores a sequence of elements, typically characters, using some character encodingString may also denote more general arrays or other sequence (or list) data types and structures.

string length java

2. String length Java Example

String class has a very convenient method of doing so: length().

Here is how you can use it:

package com.javacodegeeks.core.string;

public class StringLengthExample {

    public static void main(String[] args) {

        String s1 = "Java Code Geeks are awesome!";

        int strLength  = s1.length();

        System.out.println("The length of the string s1 : "+strLength);
        System.out.println("The length of this string is : ".length());


This will output:

The length of the string s1 : 28

Take care when using length with words of languages that have multi-character letters length will count the number of characters of the String, not lexicographical length.

There is also another way to count the number of characters in a String, but “unpaired surrogates within the text range count as one code point”:

package com.javacodegeeks.core.string;

public class StringLengthExample {

    public static void main(String[] args) {

        String s1 = "Java Code Geeks are awesome!";
        System.out.println(s1.codePointCount(0, s1.length()));

This will output: 28

This was a String length Java Example.

3. Download the Source Code

You can download the full source code of this example here: String Length Java Example

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button