Jacky's blog
首页
  • 学习笔记

    • web
    • android
    • iOS
    • vue
  • 分类
  • 标签
  • 归档
收藏
  • tool
  • algo
  • python
  • java
  • server
  • growth
  • frida
  • blog
  • SP
  • more
GitHub (opens new window)

Jack Yang

编程; 随笔
首页
  • 学习笔记

    • web
    • android
    • iOS
    • vue
  • 分类
  • 标签
  • 归档
收藏
  • tool
  • algo
  • python
  • java
  • server
  • growth
  • frida
  • blog
  • SP
  • more
GitHub (opens new window)
  • shell

  • tool

  • 网络

  • algo

  • compute_base

  • blog

  • growth

  • java

    • java base
    • Java 面试高频问题指南
    • other

      • String#format
        • 包装类型
          • 需要判空
          • 通用方式
      • 泛型擦除
    • throwable
    • thread
    • jvm
    • weakreference
    • UnSafe
    • collections
    • Class
    • classloader
  • C&C++

  • ai

  • secure

  • cms

  • english

  • 生活

  • 金融学

  • more

  • other
  • java
  • other
Jacky
2024-10-27
目录

String#format

more: link (opens new window)

  • %d: long, int
  • %s: String, boolean, object
  • %f: float
    • %.1f: Formats a floating-point number with 1 decimal places. 这里不能加 0, 如 0.xxx
    • %.2f: Formats a floating-point number with 2 decimal places

# 包装类型

# 需要判空

Integer count = null;
System.out.printf("Count: %d%n", count != null ? count : 0);
1
2

# 通用方式

Integer count = null;
Double ratio = 2.345;
System.out.printf("Count: %s, Ratio: %s%n", String.valueOf(count), String.valueOf(ratio));
// Output: "Count: null, Ratio: 2.345"
1
2
3
4
优点: 这处理方式既能避免空指针异常,还保持了输出的通用性,非常适合于不确定类型或可能为 null 的变量
#Java
上次更新: 2025/10/09, 23:53:03
Java 面试高频问题指南
泛型擦除

← Java 面试高频问题指南 泛型擦除→

最近更新
01
npx 使用指南
10-12
02
cursor
09-28
03
inspect
07-20
更多文章>
Theme by Vdoing | Copyright © 2019-2025 Jacky | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式