From b170ec04bba17e7e5061f8c091580b536b3abe00 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Thu, 9 Jun 2022 01:24:30 -0400 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AF=B9=E8=B1=A1=E6=88=90?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9A=84=20String=20=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E5=AF=B9=E8=B1=A1=E6=9D=A5=E8=BF=9B=E8=A1=8C=E8=AF=B4?= =?UTF-8?q?=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core-java-modules/core-java-strings/README.md | 5 +++-- .../ossez/interview/StringNullCheckUnitTest.java | 15 +++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/core-java-modules/core-java-strings/README.md b/core-java-modules/core-java-strings/README.md index 5daae8394a..419565afa7 100644 --- a/core-java-modules/core-java-strings/README.md +++ b/core-java-modules/core-java-strings/README.md @@ -1,8 +1,8 @@ -## Java Strings +## Java 字符串(Strings) This module contains articles about strings in Java. -### Relevant Articles: +### 相关文章 - [Use char[] Array over a String for Manipulating Passwords in Java?](https://www.baeldung.com/java-storing-passwords) - [Compact Strings in Java 9](https://www.baeldung.com/java-9-compact-string) - [String Not Empty Test Assertions in Java](https://www.baeldung.com/java-assert-string-not-empty) @@ -13,3 +13,4 @@ This module contains articles about strings in Java. - [Java Multi-line String](https://www.baeldung.com/java-multiline-string) - [Guide to Java String Pool](https://www.baeldung.com/java-string-pool) - [Fixing “constant string too long” Build Error](https://www.baeldung.com/java-constant-string-too-long-error) +- [Java 对象如何安全的 toString](https://www.ossez.com/t/java-tostring/14000) diff --git a/core-java-modules/core-java-strings/src/test/java/com/ossez/interview/StringNullCheckUnitTest.java b/core-java-modules/core-java-strings/src/test/java/com/ossez/interview/StringNullCheckUnitTest.java index cfcffb085e..791c1c96dd 100644 --- a/core-java-modules/core-java-strings/src/test/java/com/ossez/interview/StringNullCheckUnitTest.java +++ b/core-java-modules/core-java-strings/src/test/java/com/ossez/interview/StringNullCheckUnitTest.java @@ -9,17 +9,24 @@ import java.util.Objects; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType; +/** + * Java toString null Check + * + *

https://www.ossez.com/t/java-tostring/14000

+ * + * @author YuCheng Hu + */ public class StringNullCheckUnitTest { @Test public void toString_null_safe() { - Object obj = null; + String obj = null; ObjectUtils.toString(obj, StringUtils.EMPTY); - ObjectUtils.toString(obj, ()->StringUtils.EMPTY); + ObjectUtils.toString(obj, () -> StringUtils.EMPTY); assertThat("null").isEqualTo(Objects.toString(obj)); - assertThat("").isEqualTo(Objects.toString(obj,StringUtils.EMPTY)); + assertThat("").isEqualTo(Objects.toString(obj, StringUtils.EMPTY)); - assertThatExceptionOfType(NullPointerException.class).isThrownBy(() ->{ + assertThatExceptionOfType(NullPointerException.class).isThrownBy(() -> { obj.toString(); });