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: ...
随机推荐
- git 学习笔记3--status flow
1.status 通过执行 git status 命令,查看输出的信息来理解文件所处的状态以及可能的动作. 1.1 nothing to commit (working directory clean ...
- OpenCV 线性混合(4)
带滚动条的线性混合示例: #include "stdafx.h" #include<iostream> #include<thread> #incl ...
- document.images、document.forms、doucument.links——>HTMLCollection
由于历史原因,HTMLDocument类定义了一些快捷属性来访问各种各样的节点.例如,images.forms.links等属性指向香味类似只读数组的<img>.<form>. ...
- 每天一个linux命令---导出到文件
导出Linux下的部分日志到文件,使用‘>’符号 例如: [calendar@test190 logs]$ monitor.log|grep getCalendarView > share ...
- Leetcode Add Binary
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- WZJ的blog开通了
WZJ的blog开通了
- PHP面向对象学习六 多态
OOP的模式并不仅仅是把很多函数和功能集合起来,目的而是使用类,继承,多态的方式描述我们生活中的一种情况.从而使得我们的代码更具有“物”的意义.帮助我们减少一些重复性的代码和条件语句的判断. 运算 ...
- linux下搭建svn代码库
1.安装svn客户端 2.创建svn代码库 1.安装svn客户端 1.1.使用命令安装 1)CentOS $ yum install subversion 2)ubuntu sudo apt-get ...
- C#实现动态页面静态化
制作一个aspx页面,专门用来生成各个动态aspx页面的静态页面.如下图所示,仅将内容页和主页面生成静态页面,当然本例只是一个简单的范例,实际情况如很复杂,但原理都是相同的. 生成内容页: 本例中的不 ...
- (转)C#特性学习与使用(为枚举定义Description)
参考:http://blog.csdn.net/nndtdx/article/details/6905802#comments