简体   繁体   English

Effective java Item no 5 示例代码第二版

[英]Effective java Item no 5 example code second edition

public class Person
private final Date  birthDate;
// others fields omitted
public  boolean isBabyBoomer() {
Calendar gmtCal =
Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.set(1946,Calendar.JANUARY,1,0,0,0);
Date boomStart =gmtCal.getTime();
gmtCal.set(1965,Calendar.JANUARY,1,0,0,0);
Date boomEnd = gmtCal.getTime();
return birthDate.compareTo(boomStart) >= 0 &&   //where is Birthdate instance 
                                                 // to compare                                   
birthDate.compareTo(boomEnd) < 0;

}
}
  1. when and how the program gets birthdate instance ?程序何时以及如何获取生日实例?
  2. Do they omitted coding process for that instance ?他们是否省略了该实例的编码过程?
  3. For what purpose they compared results with zero(>= and < ) inside return ?他们出于什么目的将结果与 return 中的零(>= 和 < )进行比较?
  4. Can not we use simple return without using zeros integer (boolean only for testing) ?我们不能在不使用零整数(布尔值仅用于测试)的情况下使用简单返回吗?
  1. Given fact birthDate is final, i would say it is passed to class via constructor,鉴于birthDate是最终的,我会说它是通过构造函数传递给类的,

  2. It is common practice in books and other tutorin materials to omit bits of code which are not relevant for given example to avoid creating extra confision for reader书籍和其他教程材料中的常见做法是省略与给定示例无关的代码位,以避免为读者造成额外的理解

  3. Method compareTo returns int hence if you want to convert this to boolean result, you have to compare it to some number by using one of of the operators ==, !=, <, >, <=, >=方法compareTo返回int因此,如果要将其转换为布尔结果,则必须使用运算符==, !=, <, >, <=, >=之一将其与某个数字进行比较

  4. And how you want to use simple return?你想如何使用简单的返回?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有效的Java Item 9,CaseInsensitiveString示例是否正确? - Effective Java Item 9, is the CaseInsensitiveString example correct? 来自Effective Java的第9项(等于合同):示例是否正确? - Item 9 (equals contract) from Effective Java: is the example correct? 有效的Java项目16(第2版) - Forwarding类是否仅用于允许重用? - Effective Java item 16 (2nd edition) - Is Forwarding class only used to allow re-use? 有效的Java项目47:了解并使用您的库 - 有缺陷的随机整数方法示例 - Effective Java Item 47: Know and use your libraries - Flawed random integer method example 有效的Java项目7:避免使用终结器 - Effective Java Item 7: Avoid Finalizers 有效的Java项目13和TDD - Effective Java Item 13 and TDD Bloch的Effective Java第2版中常量字段的定义 - Definition of a constant field in Bloch's Effective Java 2nd edition 在有效Java的这段示例代码中,如何防止JVM“优化所有内容”? - How is the JVM prevented from 'optimizing away everything' in this piece of example code from Effective Java? 为什么在这段来自有效Java的示例代码中使用布尔逻辑运算符^? - Why is the boolean logical operator ^ being used in this piece of example code from Effective Java? 了解有效的Java深层复制示例 - Understanding Effective Java deep copy example
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM