当前位置:网站首页>short i =1; i=i+1与short i=1; i+=1的区别
short i =1; i=i+1与short i=1; i+=1的区别
2022-07-06 11:23:00 【qq_37705525】
short i =1; i=i+1与short i=1; i+=1的区别
很典型的一到JAVA 基础面试题,上次面试遇到的,现在记录一下。
short i =1; i=i+1;
short i=1;i+=1;
这两有什么区别呢 ?
对两个容量不一样的数据类型的变量进行算术运算时,java会自动将小容量的变量进行精度提升,然后再进行运算,得到的结果类型是提升后的大容量的数据类型.如果将该结果赋值给小容量的数据类型的变量,则必须进行强制的类型转换,否则编译程序会报损失精度错.如楼主示例,用i来表示1:
short s1 = 1;
int i = 1;
首先,因为short类型是16位的,而int类型是32位的,在进行
(s1+i) 运算时,自动将s1提升到32位,然后与i相加,得到的结果是32位的,而此时
s1=s1+i; 必然报错,因为如果赋值成功,只是把低16位赋给了s1,这个虽然正是楼主想要的结果,但是编译程序却不能判定你的意图是什么.
执行强转:
s1=(short)(s1+i); 就没问题了.
s1+=i;能编译通过并得到正确结果,而 s1=s1+i; 却报错,是因为它们并不是等价的,s1+=i的方式java会对i进行窄化转换,由编译程序自动执行.
边栏推荐
- Installation and management procedures
- R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
- 一种用于夜间和无袖测量血压手臂可穿戴设备【翻译】
- ACTF 2022圆满落幕,0ops战队二连冠!!
- Mathematics in machine learning -- common probability distribution (XIII): Logistic Distribution
- Use map function and split function to type multiple elements in one line
- RT-Thread 组件 FinSH 使用时遇到的问题
- Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
- The list of people who passed the fifth phase of personal ability certification assessment was published
- English topic assignment (25)
猜你喜欢

MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!

How word displays modification traces

How are you in the first half of the year occupied by the epidemic| Mid 2022 summary

Oracle advanced (IV) table connection explanation

RT-Thread 组件 FinSH 使用时遇到的问题

How to type multiple spaces when editing CSDN articles

业务与应用同步发展:应用现代化的策略建议

About static type, dynamic type, ID, instancetype

Pytorch common loss function

第五期个人能力认证考核通过名单公布
随机推荐
If you have any problems, you can contact me. A rookie ~
人体骨骼点检测:自顶向下(部分理论)
提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
Reptiles have a good time. Are you full? These three bottom lines must not be touched!
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
pytorch常见损失函数
Installation and management procedures
RT-Thread 组件 FinSH 使用时遇到的问题
Method of accessing mobile phone storage location permission under non root condition
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
倒计时2天|腾讯云消息队列数据接入平台(Data Import Platform)直播预告
受益匪浅,安卓面试问题
数学知识——高斯消元(初等行变换解方程组)代码实现
快速幂模板求逆元,逆元的作用以及例题【第20届上海大学程序设计联赛夏季赛】排列计数
C#/VB. Net to add text / image watermarks to PDF documents
Nuc11 cheetah Canyon setting U disk startup
Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
美庐生物IPO被终止:年营收3.85亿 陈林为实控人