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: ...
随机推荐
- POJ1904 King's Quest(完备匹配可行边:强连通分量)
题目大概就是说给一张二分图以及它的一个完备匹配,现在问X部的各个点可以与Y部那些些点匹配,使得X部其余点都能找到完备匹配. 枚举然后匹配,当然不行,会超时. 这题的解法是,在二分图基础上建一个有向图: ...
- HDU4067 Random Maze(最小费用最大流)
题目大概说,给一张图,删除其中一些单向边,使起点s出度比入度多1,终点t入度比出度多1,其他点出度等于入度.其中删除边的费用是bi,保留边的费用是ai,问完成要求最小的费用是多少. 一开始我想到和混合 ...
- 获取DLL中的方法名称
OpenFileDialog obj = new OpenFileDialog(); if (obj.ShowDialog() == System.Windows.Forms.DialogResu ...
- iOS学习02C语言分支结构
1. BOOL类型 返回值:真:YES 假:NO BOOL数据类型占一个字节的内存空间,占位符为%d. 计算机在识别时,YES就替换成1,NO就替换成0. bool是C语言中的布尔类型,返回值为tr ...
- MyBatis 缓存问题 session
iBatis(MyBatis)开启缓存后,通过外部程序修改或者删除数据库记录,如何让Cache清除?5 当其外部的数据库连接甚至是数据库管理系统,对数据库进行了更改,iBatis(MyBatis)的缓 ...
- POJ3469 & 最小割(最大流)模板
就是一个求最小割. sol: 数据比较大,n有20000,内部相连的边有20w,这么算算就要存八九十万的边,空间显然降不下来...然而打了dinic并不觉得快很多...最快跑到3800+ms 然后跪一 ...
- ACM +-字符串
+-字符串 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 Shiva得到了两个只有加号和减号的字符串,字串长度相同.Shiva一次可以把一个加号和它相邻的减号交换. ...
- 关于NSNotificationCenter消息通信用法
NSNotificationCenter主要用于广播消息到多个监听着,其传统用法 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotification ...
- Codeforces Beta Round #8
A题,小小的模拟题,没看懂题意啊. #include <iostream> #include <cstdio> #include <cmath> #include ...
- 属性字符串的replaceCharactersInRange方法
一,实验: 1> 让 range 的 length 参数为0,以下代码输出属性字符串的结果为12354 NSMutableAttributedString *attrStr = [[NSMuta ...