Java String length() Method

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

The String class is a non-primitive class that allows us to access many inbuilt methods with the help of the String class object. The String class stores immutable characters. The String class uses the inbuilt length() method, which is implemented from the CharSequence interface to find string length in Java, i.e. the total number of characters present in the string.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Signature

Return Type

Integer—This method returns the string length in Java, which is the total number of characters present in the String object.

Sharpen Your Fundamentals with Free Learning

Internal Implementation

The string length in Java refers to the number of characters present in the string. It can be obtained using the length() method of the String class. The length of a string represents the total count of characters, including spaces and special characters. It is important to note that the length of a string is different from its capacity, which refers to the maximum number of characters that can be stored in the underlying data structure.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Java String length() Method Examples

Example 1 - Finding the String Length in Java

Output:


Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example 2 - Finding the Length of a Character Array

Let's understand how to convert a char array to String format.

Output:

Explanation: When we pass the char array in the constructor of the String class, it converts all characters of the char array to a String format and returns it. That's why in the variable 'word', we get the String representation of the characters of the char array.


Example 3 - Finding the Length of a String Which Has Whitespaces

Output:

Explanation: The Output is 14 because the length() method includes the whitespace as well as other characters. The representation of the above String in char array is {'S', 'c', 'a', 'l', 'e', 'r', ' ', 't', 'o', 'p', 'i', 'c', 's',' '} . Whitespaces are also present in the char array.

Conclusion

  • The String class uses the length() method to get the string length in java.
  • The public access modifier prefixes the length() method.
  • The length() method also includes the whitespace while calculating the length of the string.