

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a String is empty ("") or null in Java
To check if a string is null or empty in Java, use the == operator.
Let’s say we have the following strings.
String myStr1 = "Jack Sparrow"; String myStr2 = "";
Let us check both the strings now whether they are null or empty. Result will be a boolean.
res = (myStr1 == null || myStr1.length() == 0); res = (myStr2 == null || myStr2.length() == 0);
Example
public class Demo { public static void main(String[] args) { String myStr1 = "Jack Sparrow"; String myStr2 = ""; boolean res; res = (myStr1 == null || myStr1.length() == 0); System.out.println("String 1 is null or empty? "+res); res = (myStr2 == null || myStr2.length() == 0); System.out.println("String 2 is null or empty? "+res); } }
Output
String 1 is null or empty? False String 2 is null or empty? True
- Related Questions & Answers
- Check if a String is whitespace, empty ("") or null in Java
- Check if a String is not empty ("") and not null in Java
- Java Program to Check if a String is Empty or Null
- Java Program to check if a string is empty or not
- What is a "null coalescing" operator in JavaScript?
- Java regex program to match parenthesis "(" or, ")".
- How to check if field is null or empty in MySQL?
- What is the difference between String s1 = "Hello" and String s1= new String("Hello") in java?
- How do I check if a column is empty or null in MySQL?
- Check whether a field is empty or null in MySQL?
- Java program to remove all numbers in a string except "1" and "2"?
- How to check if String is empty in Java?
- Java string concat() method vs "+" operator
- What is an "Account in Trust" or "Trust Account" and how is it managed?
- How to test String is null or empty?
Advertisements