博客
关于我
Java常见错误合集
阅读量:423 次
发布时间:2019-03-06

本文共 2642 字,大约阅读时间需要 8 分钟。

null转基本类型

在Java中,null是一个特殊值,表示缺失或未赋值。将null转换为基本类型时,需要注意类型的兼容性。以下示例展示了如何将null转换为double类型:

@Data@AllArgsConstructorstatic class OneClass {    private Double value;}@Testpublic void nullToPrimitive() {    OneClass oneClass = new OneClass(1.0);    double value = oneClass.getValue(); // value是1.0    System.out.println(value); // 输出1.0    OneClass oneClass2 = new OneClass();    value = oneClass2.getValue(); // value是null    System.out.println(value); // 输出null}

运行该测试方法会输出:

1.0
null

字符串格式化中的百分号问题

在Java中,使用String.format或format方法格式化字符串时,需要注意格式说明符的使用。以下示例显示了如何正确格式化包含百分比的字符串:

@Testpublic void formatTest() {    String str = "the fee range is %.2f%%";    System.out.println(String.format(str, 1.2));}

运行该测试方法会输出:

the fee range is 1.20%

Google Cache中的null指针问题

在使用Google的Cache库时,需要注意缓存的加载方法。如果返回null值,可能会导致缓存无法正常获取数据。以下是修正后的测试方法:

@Testpublic void nullTest2() {    LoadingCache
> cache = CacheBuilder.newBuilder() .expireAfterWrite(1, TimeUnit.HOURS) .build(new CacheLoader
>() { @Override public Optional
load(String key) throws Exception { return Optional.ofNullable(null); } }); try { Optional
result = cache.get("str"); System.out.println(result.isPresent() ? result.get() : null); } catch (Exception e) { log.error("nullTest2 {}", e.getMessage(), e); }}

运行该测试方法会输出:

null

double相加的精度问题

在Java中,double类型的精度问题可能会导致结果看起来不准确。以下示例展示了如何验证double相加的精度:

@Testpublic void doubleAdd() {    System.out.println(0.3 + 0.3 + 0.3 + 0.1); // 输出0.9999999999999999    System.out.println(Double.compare(1.0, 0.3 + 0.3 + 0.3 + 0.1) == 0); // 输出false    BigDecimal bigDecimal1 = BigDecimal.valueOf(0.3)            .add(BigDecimal.valueOf(0.3))            .add(BigDecimal.valueOf(0.3))            .add(BigDecimal.valueOf(0.1));    BigDecimal bigDecimal2 = BigDecimal.valueOf(1.0);    System.out.println(bigDecimal1.compareTo(bigDecimal2) == 0); // 输出true}

运行该测试方法会输出:

0.9999999999999999
false
true

集合contains方法的类型问题

在Java中,集合的contains方法对类型比较较为严格。以下示例展示了如何测试集合中的元素是否匹配:

@Testpublic void containsTest() {    Set
set = Sets.newHashSet((byte) 1, (byte) 2, (byte) 3); System.out.println(set.contains(1)); // 输出false System.out.println(set.contains((byte) 1)); // 输出true}

运行该测试方法会输出:

false
true

Arrays.asList的列表修改问题

Arrays.asList方法返回的列表是基于原数组的不可变列表。以下示例展示了如何避免修改源数组的问题:

Integer[] integers = {0, 1, 2};List
list = Arrays.asList(integers);System.out.println(list); // 输出[0, 1, 2]integers[0] += 1;System.out.println(list); // 输出[1, 1, 2]

输出结果为:

[0, 1, 2]
[1, 1, 2]

转载地址:http://cfbuz.baihongyu.com/

你可能感兴趣的文章
Python中对象的布尔值
查看>>
python中对集合的操作
查看>>
python中将列表转换为元组的时间复杂度,反之亦然
查看>>
Python中布尔表达式的求值
查看>>
python中很常用的10个内置函数整理(初学必备)
查看>>
python中打印二维列表数据
查看>>
python中打散数组shuffle
查看>>
Python中执行外部命令
查看>>
Python中拼接numpy数组-ChatGPT4o作答
查看>>
Python中提示:no module named 'PIL'
查看>>
Python中提示:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position
查看>>
python中数据类型存储_浅谈Python中的数据类型
查看>>
python中文件最重要的功能是什么-学 Python 都用来干嘛的?
查看>>
python中文版语言_高盛最新调查:Python超过汉语成为未来最重要技能,你准备学哪种编程语言?...
查看>>
Python中文问题SyntaxError
查看>>
Python中方括号和括号括起来的列表有什么区别?
查看>>
python中时间处理_Python中处理时间的几种方法小结
查看>>
Python中枚举串口(包括虚拟端口)的跨平台方法是什么?
查看>>
python中根号下a怎么写_python中根号怎么表示
查看>>
Python中正则表达式的巧妙使用
查看>>