它确实会无限循环 – 你说的理由很接近.这是因为a不能表示任何不满足> = 0的数字 – char是无符号的.算术下溢在Java中是明确定义的,并且没有指示.请参阅规范的以下相关部分.

The integer operators do not indicate overflow or underflow in any way.

这意味着除了比较值之外没有溢出/下溢的迹象……例如,如果a< = - a,则表示发生了下溢.

> §15.15.2

Before the subtraction, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the difference is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored. The value of the prefix decrement expression is the value of the variable after the new value is stored.

因此,我们可以看到这里有两个主要步骤:二进制数字促销,然后是缩小的初始转换.

> §5.6.2

Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules:

If either operand is of type double, the other is converted to double.

Otherwise, if either operand is of type float, the other is converted to float.

Otherwise, if either operand is of type long, the other is converted to long.

Otherwise, both operands are converted to type int.

我们可以看到递减表达式使用被视为int的方式,从而执行扩展转换.这允许它表示值-1.

> §5.1.3

A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range.

A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.

仅保留n个最低阶位意味着仅保留int表达式a-1的最低16位.由于-1为0b11111111 11111111 11111111 11111111,因此仅保存较低的0b11111111 11111111.由于char是无符号的,所有这些位都对结果有贡献,给出65535.

注意到这里有什么?从本质上讲,这意味着Java整数算法是模块化的;在这种情况下,模数是2 ^ 16或65536,因为char是16位数据类型. -1(mod 65536)≡65535,因此减量将回绕.

java 循环char拼接_java – 为什么带有char的循环因为它的索引无限循环?相关推荐

  1. java 设计char类型_JAVA中的char类型

    1.JAVA中,char占2字节,16位.可在存放汉字 2.char赋值 char a='a';  //任意单个字符,加单引号. char a='中';//任意单个中文字,加单引号. char a=1 ...

  2. java expression 强制出现_Java中带有强制括号对的单行循环

    for 定义如下. BasicForStatement: for ( ForInitopt ; Expressionopt ; ForUpdateopt ) Statement ForStatemen ...

  3. java 循环list 对象_java计算list集合中重复对象的次数及for循环内外创建对象

    我的需求:计算字符串集合中"数值"出现的次数,然后在for循环中计算"数值"及出现的次数 解决方案: 1.使用collections类中的frequency(集 ...

  4. java相同数组拼接_java遍历数组并重新拼接数组里的元素

    public ArrayList getinferfacelist(ArrayList taskNameLists) { String interfaceName=""; Stri ...

  5. java截取字符串拼接_java截取字符串并拼接

    一.substirng public static void main(String[] args) { String sendContent = "请查收:www.baidu.com&qu ...

  6. java 图片流 拼接_java 实现图片拼接

    importjavax.imageio.ImageIO;importjava.awt.image.BufferedImage;importjava.io.File;importjava.util.Ar ...

  7. java url json字符串_JAVA中带有JSON字符串的HTTP POST请求

    我必须使用已经生成的JSON字符串发出http Post请求.我尝试了两种不同的方法: 1.HttpURLConnection 2.HttpClient 但是我从两个人那里得到了相同的"不需 ...

  8. html无限循环音乐,十首最好听的英文歌曲 经典动听 让你无限循环的歌曲

    一首动听的歌曲能够让我们的心境得到享受,那么在现如今这么丰富多彩的音乐市场上,有哪些因为歌曲是最值得收藏的呢?今天就由小编来为大家列出十首最好听的英文歌曲,给您做个参考. 十首最好听的英文歌曲: 1. ...

  9. 1.5.1 无限循环

    文章目录 1.无限循环 概念: for格式: while格式: do...while格式: 无限循环的注意事项: 2.条件控制语句 break: continue: 3. Random 使用步骤: 4 ...

最新文章

  1. phpstorm常用设置
  2. 为什么前后端分离了,你比从前更痛苦?
  3. 订单可视化(智能制造、流程再造、企业信息化) 第三篇 订单可视化定义及目标...
  4. 算法--2016搜狐面试:搜狐员工放假了,都玩什么?
  5. 记录您的里程和社区运行情况:Java EE 7真实体验
  6. spring—第一个spring程序
  7. 经典神经网络 | 从Inception v1到Inception v4全解析
  8. blob类型_MySQL:创建表时如何选择合适的字段类型
  9. Exception的妙用
  10. 微信小程序 地图功能的实现 map
  11. 66. Landing Page
  12. 计算机c盘小了,电脑C盘空间太小怎么办|电脑中使用分区助手扩大C盘空间的方法...
  13. 软件测试教学实训平台
  14. vb.net 拓展教程目录
  15. 使用Python操控手机微信app(最新教程)
  16. 网课答案php源代码,知到网课答案WEB程序设计(PHP)2020章节测试答案
  17. MySQL 精选 60 道笔试题
  18. js中字符串截取函数及其方法
  19. Xmind8 (update8)破解教程
  20. 创维E900V21E机顶盒刷机避坑

热门文章

  1. maven添加jra包
  2. 虚拟大师 安装Linux系统,VMOS Pro虚拟大师完美版,手机一键安装虚拟手机!
  3. LeetCode题解(0992):K个不同呢的子数组(Python)
  4. 仿魅族日历,乐视日历,小米日历等效果
  5. OPPO A37 A37m怎么刷机 OPPO A37 A37m的刷机教程 OPPO A37 A37m完美解除账号锁
  6. 2021年度微信公众号文集
  7. Python 63个内置函数超级详解!这63个必会!
  8. python模块介绍-gevent介绍:基于协程的网络库
  9. Android display 显示流程
  10. LoRaWan规范介绍