Isempty java что это

от admin

Java — String isEmpty() Method

The Java String isEmpty() method is used to check whether the current string is empty or not. The method returns a boolean value which is true if and only if the string is empty; false otherwise. The isEmpty() method does not accept any parameter.

Note: If the string is, initialized as null, the isEmpty() method throws a NullPointerException. Because Null is a placeholder that generally means, «no data is available about this”, and it is not the same as an empty string, the compiler cannot handle it and throws a NullPointerException.

Syntax

Following is the syntax of the Java String isEmpty() method −

Parameters

It does not accept any parameter.

Return Value

This method returns true if length() is 0, else false.

Example

If the given string is empty, the isEmpty() method returns true.

In the following program, we are creating a string literal with an empty value. Then, using the isEmpty() method, we are, trying to check whether the current string is an empty string or not.

Output

On executing the above program, it will produce the following result −

Example

If the given string is non-empty, this method returns false.

In the following example, we are instantiating the string class with the value “TutorialsPoint”. Using the isEmpty() method, we are trying to check whether this string is empty or not.

Output

Following is the output of the above program −

Example

Using the conditional statement to check whether the current string is empty or not.

In this program, we create an object of the string class with the value “Hello”. Then, using the isEmpty() method and conditional statement, we are trying to check whether the current string is empty or not.

Output

The above program, produces the following output −

Example

If the given string is null, this method throws the NullPointerException.

In the following program, we are creating a string literal with the null value. Using the isEmpty() method, we are trying to check whether the given string is empty or not. Since the given string is null, this method throws an exception.

Читать:
Почему после обновления леново ноутбук не работает

Output

After executing the above program, it will produce the following output −

Isempty java что это

The isEmpty() method of List interface in java is used to check if a list is empty or not. It returns true if the list contains no elements otherwise it returns false if the list contains any element.

Syntax:

Parameter: It does not accepts any parameter.

Returns: It returns True if the list has no elements else it returns false. The return type is of datatype boolean.

Error and Exceptions: This method has no error or exceptions.

Метод isEmpty() в Java

Метод isEmpty() класса java.util.ArrayList в Java возвращает true, если этот список не содержит элементов.

Пример

Средняя оценка 4.9 / 5. Количество голосов: 10

Спасибо, помогите другим — напишите комментарий, добавьте информации к статье.

Или поделись статьей

Видим, что вы не нашли ответ на свой вопрос.

Помогите улучшить статью.

Напишите комментарий, что можно добавить к статье, какой информации не хватает.

В чём отличие isBlank vs isEmpty?

isBlank(String str) checks if a String is whitespace, empty ( "" ) or null.

isEmpty(String str) checks if a String is empty ( "" ) or null.

Т.е. разница в проверке пробела:

isBlank = isEmpty + допускает наличие пробелов

  • isEmpty — проверяет, является ли строка пустой («») или значение null .
  • isBlank — проверяет, есть-ли в строке пробел, пустая строка («») или значение null .

Vladimir Glinskikh's user avatar

isEmpty — возвращает true только если строка абсолютно пустая («») или имеет значение null;

isBlank — возвращает true только если в строке есть только пробел(-ы) (и нет других символов) или строка пустая («») или имеет значение null.

прикладное значение: допустим мы хотим чтобы пользователь ввёл свое имя, чтобы поприветствовать его.

Пользователь вводит пробел(-ы) вместо имени, тогда получится «Привет, пробел(-ы)».

Чтобы не здороваться с пробелом можно проверить введенное имя через isBlank() и в случае если пользователь ввел только пробел(-ы) попросить заново ввести имя.

Похожие статьи