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

it2022-05-09  34

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

教材学习内容总结

第十二章 Lambda Lamdba表达式 例如Comparator<String> byLength=(String name1,String name2)->name1.length()-name2.length();

等号右边是Lamdba表达式,等号左边是作为Lamdba表达式的目标类型。(String name1,String name2)->name1.length()-name2.length(); 表示接受两个字符串型参数name1,name2,而->右边定义了会返回结果的表达式。

管道操作风格 来源:可能是文档、数组、群集、产生器等等。中间操作(0或多个):又称集合操作,调用时,并不会立即进行手边的数据处理,只会在后续中间操作要求数据时才会动手处理下一笔数据。最终操作(1个):最后真正需要结果的操作,会要求之前的中间操作开始动手。第十三章 时间与日期 Calendar Calendar是个抽象类,java.util.GregorianCalendar是其子类,操作了儒略历与格里高利历的混合历,通过Calendar的getInstance()取得的Calendar实例,默认就是GregorianCalendar实例。

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

问题1:P389代码ThisDemo.java和ThisDemo2.java运行结果分别是:

两个文件的代码差别在于Demo中是

class Hello { Runnable r1 = new Runnable() { public void run() { out.println(this); } }; Runnable r2 = new Runnable() { public void run() { out.println(toString()); } }; public String toString() { return "Hello, world!"; } }

Demo2中

class Hello2 { Runnable r1 = () -> out.println(this); Runnable r2 = () -> out.println(toString()); public String toString() { return "Hello, world!"; } }

为什么只是写成了Lamdba表达式,运行结果就不一样了?

问题1解决方案:因为Demo中this的参考对象和toString()的接受者,是Runnable实例,而Runnable的toString()并没有定义,所以显示了Object默认的返回字符串。而Lamdba表达式中this的参考对象和toString()的接受者,是来自Lamdba的周围环境,即表达式在哪个名称范畴,就能参考该范畴内的名称。

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

问题1:P432页HowOld.java第14行(life / (365 * 24 * 60 * 60 * 1000L)));1000L后面的L是什么?如果去掉L再println结果出现错误。

问题1解决方案:百度了一下,关于L,是指数据超出了整形int的范围,要把该数值转换为long长整形,所以L相当于强制类型转换,一般放在计算的第一个数。

代码托管


上周考试错题总结

下面代码中共有()个线程? public class ThreadTest { public static void main(String args[]){ MyThread myThread =new MyThread(); Thread t1=new Thread(myThread); Thread t2=new Thread(myThread); t1.start(); t2.start(); } } class MyThread extends Thread { ... }

A .1 B .2 C .3 D .4 错误原因:忘记main主线程,答案应该是C三个线程。

调用线程的interrupt()方法 ,会抛出()异常对象?DE A . IOException B . IllegalStateException C . RuntimeException D . InterruptedException E . SecurityExceptionWhich of the following statements about the Callable call() and Runnable run() methods are correct? (Choose all that apply.) ADE 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 voidWhat are some reasons to use a character stream, such as Reader/Writer, over a byte stream, such as InputStream/OutputStream? (Choose all that apply.)AC

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

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.) BC

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


点评过的同学博客和代码

本周结对学习情况 20155313杨瀚上周博客互评情况 20155313杨瀚20155236范晨歌20155316黄月20155322秦诗茂20155319任泓霖

感悟与思考

看教材第十二章管道操作部分的时候,感叹于管道操作的高效率性和便捷性,但也有种无力感,因为感觉自己还是个新手,不能熟练掌握这种编程模式,可能编程的时候脑子都会乱……。感觉十三章内容非常有意思,还学习了各种事件的度量。

学习进度条

代码行数(新增/累积)博客量(新增/累积)学习时间(新增/累积)重要成长第七周494/39122/220/112完成实验一,初步学习了Lambda表达式和Java中的时间表示

计划学习时间:18小时

实际学习时间:20小时

改进情况:多学习了两个小时

参考资料

Java学习笔记(第8版)

《Java学习笔记(第8版)》学习指导

Java中的L是什么

转载于:https://www.cnblogs.com/zhuohua/p/6685750.html


最新回复(0)