记录

正在加载古诗词……

OCP-1Z0-816模拟测试2回顾

1. Given

class Booby {
}
class Dooby extends Booby {
}
class Tooby extends Dooby {
}

public class TestClass {
  Booby b = new Booby();
  Tooby t = new Tooby();
  public void do1(List<? super Dooby> dataList) {
    //1 INSERT CODE HERE
  }
  public void do2(List<? extends Dooby> dataList) {
    //2 INSERT CODE HERE
  }
}

and the following four statements:

  1. b = dataList.get(0);
  2. t = dataList.get(0);
  3. dataList.add(b);
  4. dataList.add(t);

What can be inserted in the above code?

  • Statements 1 and 3 can inserted at //1 and Statements 2 and 4 can be inserted at //2.
  • Statement 4 can inserted at //1 and Statement 1 can be inserted at //2.
  • Statements 3 and 4 can inserted at //1 and Statements 1 and 2 can be inserted at //2.
  • Statements 1 and 2 can inserted at //1 and Statements 3 and 4 can be inserted at //2.
  • Statement 1 can inserted at //1 and Statement 4 can be inserted at //2.

Explanation

  1. addData1(List<? super Dooby> dataList)
    This means that dataList is a List whose elements are of a class that is either Dooby or a super class of Dooby. We don’t know which super class of Dooby. Thus, if you try to add any object to dataList, it has to be a assignable to Dooby.
    Thus, dataList.add(b); will be invalid because b is not assignable to Dooby.
    Further, if you try to take some object out of dataList, that object will be of a class that is either Dooby or a Superclass of Dooby. Only way you can declare a variable that can be assigned the object retrieved from dataList is Object obj. Thus, t = dataList.get(0); and b = dataList.get(0); are both invalid.

  2. addData2(List<? extends Dooby> dataList)
    This means that dataList is a List whose elements are of a class that is either Dooby or a subclass of Dooby. Since we don’t know which subclass of Dooby is the list composed of, there is no way you can add any object to this list.
    If you try to take some object out of dataList, that object will be of a class that is either Dooby or a subclass of Dooby and thus it can be assigned to a variable of class Dooby or its superclass.. Thus, t = dataList.get(0); is invalid.

WTF
A type argument $T_1$ is said to contain another type argument $T_2$, written $T_2 <= T_1$, is the set of types denoted by $T_2$ is provably a subset of the set of types denoted by $T_1$ under the reflexive and transitive closure of the following rules(where $<:$ denotes subtyping($\S4.10$)):

  • $?\space extends\space T<=\space ?\space extends \space S$ if $T <: S$
  • $?\space extends \space T<=\space ?$
  • $?\space super \space T<=\space ?\space super \space S$ if $T <: S$
  • $?\space super \space T<=\space ?$
  • $?\space super \space T<=\space ? \space extends \space Object$
  • $ T<=\space T$
  • $T <= \space ? \space extends \space T$
  • $T <= \space ? \space super \space T$

2.


3.


5.


6.


7.


10.


11.


12.


14.


15.


19.


21.


22.


23.


24.


25.


27.


28.


29.


30.


31.


33.


38.


44.


45.


46.


48.


50.


54.


55.


57.


58.


59.


60.


62.


63.


65.


66.


69.


70.


73.


74.


75.


76.


77.


79.


80.


81.


阅读更多📰

第11章练习回顾

OCP 11-1 Book  2020年3月22日

1. Which of the following is an advantage of the Java Platform Module System? B

A. A central repository of all modules
B. Encapsulating packages
C. Encapsulating objects
D. No defined types
E. Platform independence

2. Which statement is true of the following module? D

zoo.staff
|---zoo
|-- staff
|-- Vet.java

A. The directory structure shown is a valid module.
B. The directory structure would be a valid module if module.java were added directly underneath zoo.staff.
C. The directory structure would be a valid module if module.java were added directly underneath zoo.
D. The directory structure would be a valid module if module-info.java were added directly underneath zoo.staff.
E. The directory structure would be a valid module if module-info.java were added directly underneath zoo.
F. None of these changes would make this directory structure a valid module.

解释: Modules are required to have a module-info.java file at the root directory of the module. Option D matches this requirement.

3. B

4. D -> G The -m or –module option is used to specify the module and class name. The -p or -module-path option is used to specify the location of the modules. Option D would be correct if the rest of the command were correct. However, running a program requires specifying the package name with periods (.) instead of slashes. Since the command is incorrect, option G is correct.

5. AF -> AFG Options C and D are incorrect because there is no use keyword. Options A and F are correct because opens is for reflection and uses declares an API that consumes a service. Option G is also correct as the file can be completely empty. This is just something you have to memorize.

6. BDF -> BC Packages inside a module are not exported by default, making option B correct and option A incorrect. Exporting is necessary for other code to use the packages; it is not necessary to call the main() method at the command line, making option C correct and option D incorrect. The module-info.java file has the correct name and compiles, making options E and F incorrect.

7. EF -> DG Options A, B, E, and F are incorrect because they refer to keywords that don’t exist. The requires transitive keyword is used when specifying a module to be used by the requesting module and any other modules that use the requesting module. Therefore, dog needs to specify the transitive relationship, and option G is correct. The module puppy just needs to require dog, and it gets the transitive dependencies, making option D correct.

阅读更多📰

RSocket与Spring Security简单整合

Spring Tips 学习记录  2020年3月10日

创建工程

  • greetings-service

    在 start.spring.io 选择 2.3.0 M2 版本 Spring Boot,依赖项如下

    Lombok

    RSocket

    Spring Security

  • greetings-client

    客户端的依赖项也是

    Lombok

    RSocket

    Spring Security

服务端应用

GreetingsServiceApplication.java

阅读更多📰

网络爬虫学习笔记

scraping the web for fun and for profit  2020年3月2日

课程计划

  • 入门

  • HttpClient

  • Jsoup

  • 案例

    dependencies {
        implementation('org.jsoup:jsoup:1.12.2')
        implementation('org.apache.httpcomponents:httpclient:4.5.2')
      
        testImplementation('org.slf4j:slf4j-log4j12:1.7.25')
        testImplementation('org.junit.jupiter:junit-jupiter:5.6.0')
    }
    

爬虫功能

从功能上来讲,爬虫一般分为数据采集,处理,存储三个部分。爬虫从一个或若干个初始页面的URL开始,获得初始页面上的URL,在抓取页面的过程中,不断从当前页面抽取新的URL放入队列,直到满足系统的一定停止条件。

  • 数据采集
  • 处理
  • 存储

从初始页面开始,爬取这个页面里面的详细页面连接,接着是下一页,等等。

阅读更多📰

OCP-1Z0-816 模拟测试1回顾

题目比较多  2020年2月26日

4. Given:

Path p1 = Paths.get("c:\\temp\\test1.txt");
Path p2 = Paths.get("c:\\temp\\test2.txt");

Which of the following code fragments moves the file test1.txt to test2.txt, even if test2.txt exists?

  • Files.move(p1, p2);

    This will throw a java.nio.file.FileAlreadyExistsException if the file already exists.

  • Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING);
  • try(Files.move(p1, p2)) { }

    Files.move returns a Path object (of the destination file), which is not a resource that can be closed because it does not implement AutoCloseable interface. So this will not compile.

  • try(Files.copy(p1, p2, StandardCopyOption.REPLACE_EXISTING)) { Files.delete(p1); }
  • Files.copy(p1, p2, StandardCopyOption.REPLACE_EXISTING); Files.delete(p1);

Explanation
Files.copy method will copy the test1.txt into test2.txt. If test2.txt doesn’t exist, it will be created. However, Files.isSameFile method doesn’t check the contents of the file. It is meant to check if the two path objects resolve to the same file or not. In this case, they are not, and so, it will return false.
The following is brief JavaDoc description for both the methods:
public static Path copy(Path source, Path target, CopyOption... option) throws IOException

阅读更多📰

816考点速记

1. Unmodifiable collections using of/copyOf and Collections.unmodifiableXXX methods

java.util.List and java.util.Set have of and copyOf static factory methods that provide a convenient way to create unmodifiable lists/sets.

The of methods accept either an array or multiple individual parameters. If you pass it a collection, it will be treated as a regular object i.e. it will return a list/set containing the same collection object instead of returning a list/set containing the objects that the passed collection contains.

The copyOf, on the other hand, accepts only a Collection. It iterates through the passed Collection and adds all the elements of that Collection in the returned list/set.

阅读更多📰

816基准测试回顾

1. Which of the following annotations are retained for run time?

  • @SuppressWarnings

    It is defined with @Retention(SOURCE)

  • @Override

    It is defined with @Retention(SOURCE)

  • @SafeVarargs

    It is defined with @Retention(RUNTIME)

  • @FunctionalInterface

    It is defined with @Retention(RUNTIME)

  • @Deprecated

    It is defined with @Retention(RUNTIME)


2. Your application needs to load a set of key value pairs from a database table which never changes. Multiple threads need to access this information but none of them changes it.
Which class would be the most appropriate to store such data if the values need not be kept in a sorted fashion?

  • Hashtable
  • HaspMap
  • Set
  • TreeMap
  • List

Explanation
You should know that all Hashtable methods are synchronized and this compromises its performance for simultaneous reads.
Since not thread modifies the data, it is not efficient to use a Hashtable.
A HashMap is a perfect choice because its methods are not synchronized and so it allows efficient multiple reads. TreeMap is used to keep the keys sorted which makes it a little bit slower than HashMap.
Set and List can’t be used since we need to store Key-value pairs.

阅读更多📰

Exercises for Inner class

内部类章节的练习  2020年2月15日

  1. What is an inner class? Differentiate between member, local, and anonymous inner classes.

    An inner class declares inside a top-level class without a static modifier. Inner classes that declared at member level are called member inner classes, local to the method, or instance initialization block are local inner class. And local inner classes that have no name are anonymous inner classes.

阅读更多📰

Travis CI上手

玩下持续构建、持续集成  2020年2月12日

配置文件

我用于博客自动构建的.travis.yml脚本:

language: ruby
rvm:
  - 2.6.3
env:
  global:
  - NOKOGIRI_USE_SYSTEM_LIBRARIES=true
  - secure: D0RrVHHBEEJMyJK9OUUPM6Wms+D+zifJhHFl4BcmC0leSR0vANoQ6357Bl4miZL2flmnBU/1Z9WNzcDVM0JwQpF8ZVeEdMvgBY5+73Xjp8cIwYfWx8MAUB8J1Hug2aEiXw6KoSxjFOnUpSRnhCcWpviFNCZOa5XavMfe7+OHbNQT1U1xu+ZOmDG6Hrm3CMhfzu2cRPIfziAAgnWVfgtqA0STXnY9D0HvfsKPl6khy6VWkeF09Chqo/XVO/SV5LSgplkgXh1OzS6ITwT99UWDRqIeXYAvMJFyrHkzdxNgtAddYVxmx8WQqfqUFQ6A3HSnJy2Fq04H3gLszQxrVPzw8fNeOzBe7hIviaZdgRsglc26NO13EX//e4DA1ImiYKciFGvDNDBrqtv8u3XAG+8jVe/dLwKbBXnvi64udh2/H/lTBfWQ3eV08/R6m0m4aF8jbv0oBuXReyXuEy9D6xNm+yceekWzKAmTYPFb9W5XLveIgt8SIQO9P6tGuMWV6w7KRhgbpgI886ZeSVS3WcAGIr11SvSNa+x5WapOd/mizHrA8VvW4v8GjmYYwTOsmiLuYl2t1fWY76yYOOdSDLC9XhJN/mS3qxxX7WR4KCJgpstyzTVXj+uyuzi23sV1SdUvO35r3VkZk11ad9Qmp9WLXpdSQfMAKbu/lC7C9HfrGuM=
cache:
  bundler: true
  directories:
    - /home/travis/.rvm/
install:
- gem install bundler
- bundle install
script:
- bundle exec rake deploy
after_success:
- bash <(curl -s https://codecov.io/bash)

发现启用了 cache 之后,快很多了。搞到动图凑内容:

cache_1581461859_5053

用来测试构建时长,哈哈哈哈!

阅读更多📰

AsciiDoc语法参考

学习笔记  2020年2月12日

段落

段落不需要特别的格式,直接写就是了。 然后呢

自然段

这是普通段落

开始至少空一格就成为自然段,同一个自然段里面的内容必须相邻,
即不能隔空行。
隔空行就是另一个自然段了。自然段文字是等宽字体,然后会保留空格和新行。
例如    保留了前边的空格,
然后这样,保留了换新行。

现在这里又是一个普通段落了。

阅读更多📰