Java String isEmpty example

String's isEmpty method checks if length of string is 0 or not. It returns true if length of String is 0 else return false.

Method Signature:

public boolean isEmpty()

String isEmpty Example:

package org.arpit.java2blog;

public class StringIsEmptyExample {

  * @Author : Arpit Mandliya
 public static void main(String[] args)
  String str1="";
  String str2="java2blog";
  String str3=null;
  System.out.println("str1 isEmpty: "+str1.isEmpty() );
  System.out.println("str2 isEmpty: "+str2.isEmpty() );
  // str3.isEmpty will throw NullPointerException
  //System.out.println("str3 isEmpty: "+str3.isEmpty() );

When you run above program, you will get below output:
str1 isEmpty: true
str2 isEmpty: false

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