Share on Google+Share on Google+

length() Method In Java

In this section, you will learn how to use length() method of the String class.

length() Method In Java


In this section, you will learn how to use length() method of the String class. We are going to using length() method. These method are illustration as follow here:

Description of program:

Here, we will know that how many character of length in String. First of all, we have to define class named "StringLength". Inside of class we have to define method of class in  main class. After that we create one method which returns the integer type values. So we are using to length() method of the String class for the purpose.

length() method : This method return the length of this string as an integer value.

Here is the the code of this program:


class StringLength{
  public static void main(String[] args){
  BufferedReader object=
new BufferedReader (new InputStreamReader(;
  System.out.println("Eneter string value:");
  String s=object.readLine();
  int len=s.length();
  catch(Exception e){}

Output this program:


C:\java_work>java StringLength
Eneter string value:

Download this program:



Posted on: June 5, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: length() Method In Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
January 1, 2012

i have problem with java function,like length() i don't know how much function in java can you tell me about it ?
January 1, 2012

i have problem with function in java, i dont know how much function like length() etc can you tell me about it? i need reference from java,please
February 24, 2012
how i can get the length in this input

import*; /** * Write a description of class ewanq here. * * @author (your name) * @version (a version number or a date) */ public class input1 { public static void main (String [] args)throws Exception { BufferedReader input = new BufferedReader (new InputStreamReader(; clear_string c = new clear_string(); c.clear_string(); int n; String strnum1; System.out.print("Enter value"); strnum1= input.readLine(); n = Integer.parseInt(strnum1); System.out.print( n + " "); while (n!=1) { if (n%2==1) { n=(3*n)+1; System.out.print(n + " "); } else { n =n/2; System.out.print(n + " "); } } } } how can i get the length of this input..thanks for the fast responds
sachin sharma
March 16, 2012

How to run package
Art Bergquist
November 27, 2013
Typo - "Eneter" need to be replaced with "Enter"

Both occurrences "Eneter" need to be replaced with "Enter" above (on