您的位置:首页 > 编程语言 > Java开发

20155302 2016-2017-2 《Java程序设计》第七周学习总结

2017-04-08 17:13 302 查看

20155302 2016-2017-2 《Java程序设计》第七周学习总结

教材学习内容总结

Lambda表达式的优点:更加紧凑的代码、修改方法的能力、更好地支持多核处理

“Lambda 表达式”(lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象(lambda abstraction),是一个匿名函数,即没有函数名的函数。Lambda表达式可以表示闭包(注意和数学传统意义上的不同)。

函数式接口:Functional Interface.

定义的一个接口,接口里面必须 有且只有一个抽象方法 ,这样的接口就成为函数式接口。

在可以使用lambda表达式的地方,方法声明时必须包含一个函数式的接口。

(JAVA8的接口可以有多个default方法)

任何函数式接口都可以使用lambda表达式替换。

例如:ActionListener、Comparator、Runnable

lambda表达式只能出现在目标类型为函数式接口的上下文中。

基本语法:

(parameters) -> expression 或 (parameters) ->{ statements; }

即: 参数 -> 带返回值的表达式/无返回值的陈述

优点:

1.极大的简化代码。去除了很多无用的Java代码,使得代码更为简洁明了。

2.比匿名内部类更加高效(不确定)。编译器会生成专门的lambda方法,可以使用javap -p查看编译过后的代码

缺点:

1.可读性差。在代码简洁的情况下,另一方面又让大多程序员很难读懂。因为很少程序员接触使用它。

(不过这个缺点不是本身缺点,而且源于程序员较少使用)

Java中6个时间类:

java.util.Date java.sql.Date java.sql.Time java.sql.Timestamp java.text.SimpleDateFormat java.util.Calendar

java.util.Date、java.util.Calendar、java.sql.Timestamp具有的时间日期组件(而且他们具有无参构造方法),java.sql.Date和java.sql.Time只有时间或日期组件。

Date 对象表示时间的默认顺序是星期、月、日、小时、分、秒、年。若需要修改时间显示的格式可以使用“SimpleDateFormat(String pattern)”方法。

抽象类 Calendar 提供了一组方法,允许把以毫秒为单位的时间转换成一些有用的时间组成部分。Calendar 不能直接创建对象,但可以使用静态方法 getInstance() 获得代表当前日期的日历对象

教材学习中的问题和解决过程

问题1:Java中Lambda有什么用?

问题1解决方案:Java中的lambda,主要是为了支持函数式编程,只有在Java 8中才引入了lambda

代码调试中的问题和解决过程

问题1:


编译器将会显示以下错误信息:

在以下方法或属性之间的调用不明确:

“ConsoleApplication1.Test.Print(ConsoleApplication1.Test.AddHandler

问题1解决方案:必须明确数据类型给编译器,如下:



这样我们的代码就能编译通过了。

代码托管





上周考试错题总结

下面哪条命令可以把 f1.txt 复制为 f2.txt ?

A .

cp f1.txt f2.txt

B .

copy f1.txt f2.txt

C .

cat f1.txt > f2.tx

D .

cp f1.txt | f2.tx

E .

copy f1.txt | f2.tx

正确答案: A C

如果有以下代码段:

Thread thread = new Thread(new ________________() {

public void run() {...}

});

空白部分指定哪些类型可以通过编译?

A .

Runnable

B .

Thread

C .

Future

D .

Executor

正确答案: A B

调用线程的interrupt()方法 ,会抛出()异常对象?

A .

IOException

B .

IllegalStateException

C .

RuntimeException

D .

InterruptedException

E .

SecurityException

正确答案: D E

现有

class Calc {

public static void main(String [] args) {

try {

int x = Integer.parselnt ("42a") ;


//insert code here


System.out.print ("oops");


}

}

}

下面哪行分别插入到第五行,会导致输 "oops" ?

A .

catch (IllegalArgumentException e) {

B .

} catch (IllegalStateException c) {

C .

} catch (NumbelFormatException n) {

D .

} catch (ClassCastException c) {

正确答案: A C

Given an instance of a Stream, s, and a Collection, c, which are valid ways of creating a parallel stream? (Choose all that apply.)

给定一个Stream的实例s, 一个Collection的实例c, 下面哪些选项可以创建一个并行流?

A .

new ParallelStream(s)

B .

c.parallel()

C .

s.parallelStream()

D .

c.parallelStream()

E .

new ParallelStream(c)

F .

s.parallel()

正确答案: D F

Which of the following statements about the Callable call() and Runnable run() methods are correct? (Choose all that apply.)

A .

Both can throw unchecked exceptions.

B .

Callable takes a generic method argument.

C .

Callable can throw a checked exception.

D .

Both can be implemented with lambda expressions.

E .

Runnable returns a generic type.

F .

Callable returns a generic type.

G .

Both methods return void

正确答案: A C D F

What are some reasons to use a character stream, such as Reader/Writer, over a byte stream, such as InputStream/OutputStream? (Choose all that apply.)

A .

More convenient code syntax when working with String data

B .

Improved performance

C .

Automatic character encoding

D .

Built-in serialization and deserialization

E .

Character streams are high-level streams

F .

Multi-threading support

正确答案: A C

Assuming zoo-data.txt is a multiline text file, what is true of the following method?

private void echo() throws IOException {

try (FileReader fileReader = new FileReader("zoo-data.txt");

BufferedReader bufferedReader = new BufferedReader(fileReader)) {

System.out.println(bufferedReader.readLine());

}

}

A .

It prints the first line of the file to the console.

B .

It prints the entire contents of the file.

C .

The code does not compile because the reader is not closed.

D .

The code does compile, but the reader is not closed.

E .

The code does not compile for another reason.

正确答案: A 你的答案

What is the result of executing the following code? (Choose all that apply.)

String line;

Console c = System.console();

Writer w = c.writer();

if ((line = c.readLine()) != null)

w.append(line);

w.flush();

A .

The code runs without error but prints nothing.

B .

The code prints what was entered by the user.

C .

An ArrayIndexOutOfBoundsException might be thrown.

D .

A NullPointerException might be thrown.

E .

An IOException might be thrown.

F .

The code does not compile.

正确答案: B D E

Which of the following are true? (Choose all that apply.)

A .

A new Console object is created every time System.console() is called.

B .

Console can only be used for reading input and not writing output.

C .

Console is obtained using the singleton pattern.

D .

When getting a Console object, it might be null.

E .

When getting a Console object, it will never be null.

正确答案: C D

Suppose that the file c:\book\java exists. Which of the following lines of code creates an object that represents the file? (Choose all that apply.)

A .

new File("c:\book\java");

B .

new File("c:\book\java");

C .

new File("c:/book/java");

D .

new File("c://book//java");

E .

None of the above

正确答案: B C

Which of the following are built-in streams in Java? (Choose all that apply.)

A .

System.err

B .

System.error

C .

System.in

D .

System.input

E .

System.out

F .

System.output

正确答案: A C E

结对及互评

评分标准

正确使用Markdown语法(加1分):

不使用Markdown不加分

有语法错误的不加分(链接打不开,表格不对,列表不正确...)

排版混乱的不加分

模板中的要素齐全(加1分)

缺少“教材学习中的问题和解决过程”的不加分

缺少“代码调试中的问题和解决过程”的不加分

代码托管不能打开的不加分

缺少“结对及互评”的不能打开的不加分

缺少“上周考试错题总结”的不能加分

缺少“进度条”的不能加分

缺少“参考资料”的不能加分

教材学习中的问题和解决过程, 一个问题加1分

代码调试中的问题和解决过程, 一个问题加1分

本周有效代码超过300分行的(加2分)

一周提交次数少于20次的不加分

其他加分:

周五前发博客的加1分

感想,体会不假大空的加1分

排版精美的加一分

进度条中记录学习时间与改进情况的加1分

有动手写新代码的加1分

课后选择题有验证的加1分

代码Commit Message规范的加1分

错题学习深入的加1分

点评认真,能指出博客和代码中的问题的加1分

结对学习情况真实可信的加1分

扣分:

有抄袭的扣至0分

代码作弊的扣至0分

迟交作业的扣至0分

点评模板:

博客中值得学习的或问题:

xxx

xxx

...

代码中值得学习的或问题:

xxx

xxx

...

基于评分标准,我给本博客打分:XX分。得分情况如下:xxx

参考示例

点评过的同学博客和代码

上周博客互评情况

20155323

20145234

20145209

20155202

20155326

其他(感悟、思考等,可选)

本周学习的两章知识较为简单,但需要和之前学习的内容基础结合起来,所以前面的基础很重要,在学习过程中我不断翻阅前面知识,结合起来学习会使效率提高。

学习进度条

代码行数(新增/累积)博客量(新增/累积)学习时间(新增/累积)重要成长
目标5000行30篇400小时
第一周20/202/220/20
第二周140/1602/418/38
第三周113/2731/520/58
第四周335/6081/620/78
第五周1159/14081/730/108
第六周452/18601/820/128
第七周343/22031/930/158
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: