Thinking in Java——笔记(4)
Controlling Execution
true and false
- Java doesn’t allow you to use a number as a boolean.
- If you want to use a non-boolean in a boolean test, you must first convert it to a boolean value by using a conditional expression.
if-else
Iteration
- Looping is controlled by while, do-while and for, which are sometimes classified as iteration statements.
do-while
for
- The expression is tested before each iteration, and as soon as it evaluates to false, execution will continue at the line following the for statement. At the end of each loop, the step executes.
- In Java and C++, you can spread your variable declarations throughout the block, defining them at the point that you need them.
The comma operator
- Using the comma operator, you can define multiple variables within a for statement, but they must be of the same type.
- The ability to define variables in a control expression is limited to the for loop.
Foreach syntax
- Any method that returns an array is a candidate for use with foreach.
- Foreach will also work with any object that is Iterable.
- it is far easier to read and says what you are trying to do rather than giving the details of how you are doing it.
Return
break and continue
- break quits the loop without executing the rest of the statements in the loop.
- continue stops the execution of the current iteration and goes back to the beginning of the loop to begin the next iteration.
- Normally, you’d use a break like this only if you didn’t know when the terminating condition was going to occur.
The infamous “goto”
- A goto is a jump at the source-code level.
- The problem is not the use of goto, but the overuse of goto.
- Although goto is a reserved word in Java, it is not used in the language; Java has no goto.
- It’s important to remember that the only reason to use labels in Java is when you have nested loops and you want to break or continue through more than one nested level.
- The break and continue keywords will normally interrupt only the current loop.
- In the cases where breaking out of a loop will also exit the method, you can simply use a return.
switch
- The switch statement selects from among pieces of code based on the value of an integral expression.
- If it finds a match, the corresponding statement executes. If no match occurs, the default statement executes.
- If break is missing, the code for the following case statements executes until a break is encountered.
- Note that the last statement, following the default, doesn’t have a break because the execution just falls through to where the break would have taken it anyway.
- It requires a selector that evaluates to an integral value, such as int or char.
- enums are designed to work nicely with switch.
- Notice how the cases can be “stacked” on top of each other to provide multiple matches for a particular piece of code.
Thinking in Java——笔记(4)的更多相关文章
- Effective Java笔记一 创建和销毁对象
Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...
- java笔记00-目录
--2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:
- java笔记整理
Java 笔记整理 包含内容 Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...
- 转 Java笔记:Java内存模型
Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...
- servlet(6) - servlet总结 - 小易Java笔记
垂阅前必看: 这都是我总结的我觉得是学习servlet应该掌握的,我在学习期间也做了一个博客项目来让所学的知识得以巩固.下面就是博客项目链接.前面的servlet相关的笔记总汇,还有就是我把觉得在学习 ...
- Java笔记 —— 继承
Java笔记 -- 继承 h2{ color: #4ABCDE; } a{ text-decoration: none!important; } a:hover{ color: red !import ...
- Java笔记 —— 方法重载和方法重写
Java笔记 -- 方法重载和方法重写 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red ...
- Java笔记 —— 初始化
Java笔记 -- 初始化 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red !impo ...
- Java笔记 —— this 关键字
Java笔记 -- this 关键字 h2{ color: #4ABCDE; } a{ color: blue; text-decoration: none; } a:hover{ color: re ...
- Java 笔记 —— java 和 javac
Java 笔记 -- java 和 javac h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: ...
随机推荐
- S5中新增的Array方法详细说明
ES5中新增的Array方法详细说明 by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wor ...
- Map.Entry用法示例
一般在HashMap中可以通过key值得到value值,以key作为检索项.Map.Entry<K,V>可以作为条目的检索项.HashMap中有entrySet()方法,返回值是Set&l ...
- Codeforces 629C Famil Door and Brackets(DP)
题目大概说给一个长m的括号序列s,要在其前面和后面添加括号使其变为合法的长度n的括号序列,p+s+q,问有几种方式.(合法的括号序列当且仅当左括号总数等于右括号总数且任何一个前缀左括号数大于等于右括号 ...
- WPF画线问题,几千条以后就有明显的延迟了。
我现在是这么画的,class A { private GeometryGroup _lines; private Path _path; public A() { _path.Data = ...
- jquery ajax 提交信息后等待返回的提示信息
最简单的方法: http://bbs.csdn.net/topics/390584283?page=1 $('#click').click(function){ $('#data').html('&l ...
- 【原】iOS多线程之异步任务+并行队列情况与异步任务+串行队列(主队列)情况
异步任务+并行队列 把异步任务放到并行队列进行执行,异步任务会在不同的线程中执行. /*异步执行+并行队列*/ - (IBAction)clickBasic1:(UIButton *)sender { ...
- vwampserver2.5-apache2.4.9允许外部访问的配置
打开..\wamp\bin\apache\apache2.4.9\conf\httpd.conf配置文件, <Directory "c:/wamp/www/"> ...
- gulp + webpack 构建多页面前端项目
修改增加了demo地址 gulp-webpack-demo 之前在使用gulp和webpack对项目进行构建的时候遇到了一些问题,最终算是搭建了一套比较完整的解决方案,接下来这篇文章以一个实际项目为例 ...
- 20145304 Java第八周学习报告
20145304<Java程序设计>第八周学习总结 教材学习内容总结 NIO NIO使用频道来衔接数据节点,在处理数据时,NIO可以让你设定缓冲区容量,在缓冲区中对感兴趣的数据区块进行标记 ...
- 【wikioi】1014 装箱问题
题目链接 算法:动态规划(01背包) 01背包思想:依次对待某一物体,考虑是否放入容量为V的背包中 用f[V]来表示容量为V的背包的最大价值,则决策是 f[V] = max{f[V], f[V-v[i ...