博客
关于我
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/

你可能感兴趣的文章
oauth2登录认证之SpringSecurity源码分析
查看>>
OAuth2:项目演示-模拟微信授权登录京东
查看>>
OA系统多少钱?OA办公系统中的价格选型
查看>>
OA系统选型:选择好的工作流引擎
查看>>
OA让企业业务流程管理科学有“据”
查看>>
OA项目之我的会议(会议排座&送审)
查看>>
OA项目之我的会议(查询)
查看>>
Object c将一个double值转换为时间格式
查看>>
object detection之Win10配置
查看>>
object detection训练自己数据
查看>>
object detection错误Message type "object_detection.protos.SsdFeatureExtractor" has no field named "bat
查看>>
object detection错误之Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
查看>>
object detection错误之no module named nets
查看>>
Object of type 'ndarray' is not JSON serializable
查看>>
Object Oriented Programming in JavaScript
查看>>
object references an unsaved transient instance - save the transient instance before flushing
查看>>
Object.keys()的详解和用法
查看>>
objectForKey与valueForKey在NSDictionary中的差异
查看>>
OBJECTIVE C (XCODE) 绘图功能简介(转载)
查看>>
Objective-C ---JSON 解析 和 KVC
查看>>