Skip to content
This repository has been archived by the owner on Aug 1, 2020. It is now read-only.

Latest commit

 

History

History
22 lines (15 loc) · 471 Bytes

short s1 = 1; s1 = s1 + 1.md

File metadata and controls

22 lines (15 loc) · 471 Bytes
title date tags
short s1 = 1; s1 = s1 + 1;有什么错 ?
2018-09-02 02:29:46 -0700

隐式类型转换

因为字面量 1 是 int 类型,它比 short 类型精度要高,因此不能隐式地将 int 类型下转型为 short 类型。

short s1 = 1;
s1 = s1 + 1; // 会报错

但是使用 += 运算符可以执行隐式类型转换。

s1 += 1;

上面的语句相当于将 s1 + 1 的计算结果进行了向下转型:

s1 = (short) (s1 + 1);