Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Question: What is CharSequence.isEmpty() in Java 15?
Answer:

Also new is the default method isEmpty() in the CharSequence interface. The method simply checks whether the character sequence's length is 0:

default boolean isEmpty() {  return this.length() == 0;}

This method is thus automatically available in the Segment, StringBuffer, and StringBuilder classes.

String and CharBuffer, which also implement CharSequence, each have their optimized implementation of isEmpty(). With String, for example, the call to length() is unnecessarily expensive because, since Java 9 (JEP 254 "Compact Strings"), the string's encoding must also be taken into account when calculating its length.

Is it helpful? Yes No

Most helpful rated by users:

©2025 WithoutBook