Java String length example

String's length method is used to calculate length of String. It returns number of character in the String.

Method Signature:

public int length()

String length Example:

package org.arpit.java2blog;

public class StringLengthExample {
  * @Author: Arpit Mandliya
 public static void main(String[] args) {

  // 1st
  String str1 = "Hello world from";
  // 2nd
  String str2 = "Core java programming tutorials";
  // 3rd
  String str4="";
  System.out.println("str1's length : "+str1.length());
  System.out.println("str2's length : "+str2.length());
  System.out.println("str3's length : "+str3.length());
  System.out.println("str4's length : "+str4.length());

When you run above program, you will get below output:
str1's length : 30
str2's length : 31
str3's length : 26
str4's length : 0

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012