Java String length()

This method returns the length of this string. The length is equal to the number of 16-bit Unicode characters in the string.

Internal implementation
public int length() { return value.length; }
Syntax

Here is the syntax of this method −

public int length()
Specified by

CharSequence interface

Parameters

Here is the detail of parameters −

NA

Return Value

This method returns the the length of the sequence of characters represented by this object.

Java String length() method example
public class Test { public static void main(String args[]) { String str1="cprogramcoding"; String str2="java"; System.out.println("string length is: "+str1.length()); System.out.println("string length is: "+str2.length()); } }


Output:
string length is: 14 string length is: 4
Java String length() Method Example
public class Test { public static void main(String[] args) { String s = "cprogramcoding"; if(s.length()>0) { System.out.println("String is not empty and length is: "+s.length()); } s = ""; if(s.length()==0) { System.out.println("String is empty now: "+s.length()); } } }


Output:
String is not empty and length is: 14 String is empty now: 0
import java.io.*; public class Test { public static void main(String args[]) { String Str1 = new String("Welcome to cprogramcoding.com"); String Str2 = new String("Tutorials" ); System.out.print("String Length :" ); System.out.println(Str1.length()); System.out.print("String Length :" ); System.out.println(Str2.length()); } }


Output:
C:\new>javac Test.java C:\new>java Test String Length :29 String Length :9



Instagram