一点思考|工作十几年了,竟从未用过do-while!

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最近在看 Java 的基础知识,其中有部分是关于循环的,在 Java 中,循环的语法总共分为 3 种: "},{"type":"codeinline","content":[{"type":"text","text":"for"}]},{"type":"text","text":"、 "},{"type":"codeinline","content":[{"type":"text","text":"while"}]},{"type":"text","text":"、 "},{"type":"codeinline","content":[{"type":"text","text":"do-while"}]},{"type":"text","text":",如下图所示:"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/40/4014dd8484d3a1de98d6b44262f2adcb.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但我惊奇的发现,在之前的职业生涯中(11 年),竟从未用过 "},{"type":"codeinline","content":[{"type":"text","text":"do-while"}]},{"type":"text","text":"(尴尬),于是问了群里的小伙伴,发现也是鲜有人用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/09/09a77f277926ebff723aea942a9d0c49.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"do-while 语法分析"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们先来了解一下 do-while 的语法:"}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"do {\n // statements\n} while (expression);"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意:"},{"type":"text","marks":[{"type":"strong"}],"text":"最后一个冒号不能省略,否则会提示编译出错"},{"type":"text","text":"。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"它的执行流程如下图所示:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/dc/dc56034fd1533d359090da86787755ad.png","alt":null,"title":"","style":[{"key":"width","value":"25%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那它究竟在什么地方用呢?"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"do-while 使用场景"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在我多次的搜索和请教下,终于找到了两个相对满意的使用场景,接下来一起来看。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"使用场景一:抢票"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对于抢票业务来说,无论三七二十一,先抢了再说,然后在判断是否抢票成功,如果抢票成功则退出循环,否则继续执行抢票,实现的伪代码如下所示:"}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"do {\n // 抢票代码...\n} while (没抢到票);"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"思路提供者:贾总"}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"使用场景二:进制转换"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"经过大量搜索发现,在 JDK 的源码中,也存在少量使用 "},{"type":"codeinline","content":[{"type":"text","text":"do-while"}]},{"type":"text","text":" 的场景,比如 "},{"type":"codeinline","content":[{"type":"text","text":"Integer"}]},{"type":"text","text":" 中进制转换,相关源码如下:"}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {\n int charPos = len;\n int radix = 1 << shift;\n int mask = radix - 1;\n do {\n buf[offset + --charPos] = Integer.digits[val & mask];\n val >>>= shift;\n } while (val != 0 && charPos > 0);\n\n return charPos;\n}"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"比如十进制转二进制就会执行此方法,在进制转换的业务中,无论任何情况,一定会至少执行一次进制转换的,因此这种业务场景就非常适合 "},{"type":"codeinline","content":[{"type":"text","text":"do-while"}]},{"type":"text","text":"。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"总结"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"孔子说:温故而知新。当我们学完很多知识之后,回过头来再琢磨这些知识,发现很有趣,这就是知识的一大乐趣吧。本文我们介绍了两种 "},{"type":"codeinline","content":[{"type":"text","text":"do-while"}]},{"type":"text","text":" 的使用场景,抢票和进制转换,你还知道哪些 "},{"type":"codeinline","content":[{"type":"text","text":"do-while"}]},{"type":"text","text":" 的使用场景吗?欢迎评论区告诉我~"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章