Jacky's blog
首页
  • Android
  • Web
  • Server
  • Python
  • iOS
  • Java
  • Vue
  • 计算机基础
  • AI
  • 全部工具
  • CLI 工具
  • 编辑器
  • 开发工具
  • 构建工具
  • 版本控制
  • 调试工具
  • 网络工具
  • 个人成长
  • 博客
  • 分类
  • 标签
  • 归档
收藏
GitHub (opens new window)

Jack Yang

编程; 随笔
首页
  • Android
  • Web
  • Server
  • Python
  • iOS
  • Java
  • Vue
  • 计算机基础
  • AI
  • 全部工具
  • CLI 工具
  • 编辑器
  • 开发工具
  • 构建工具
  • 版本控制
  • 调试工具
  • 网络工具
  • 个人成长
  • 博客
  • 分类
  • 标签
  • 归档
收藏
GitHub (opens new window)
  • java base
  • Java 面试高频问题指南
  • 其他

    • String#format
      • 包装类型
        • 需要判空
        • 通用方式
    • 泛型擦除
  • throwable
  • thread
  • weakreference
  • UnSafe
  • collections
  • Class
  • classloader
  • 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
上次更新: 2026/10/18, 22:14:48
Java 面试高频问题指南
泛型擦除

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

最近更新
01
Codex 使用与会话管理指南
10-17
02
Tailscale 入门:让自己的设备跨网络互联
10-15
03
GitHub CLI(gh)工作流与 CI 排查
10-08
更多文章>
Theme by Vdoing | Copyright © 2019-2026 Jacky | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式