Jason Kuan
Effective Java (2nd)
Item 1: Consider static factory
methods instead of constructors
public class Person {

private String name;

private int age;



public Person(String name, int age) {

this.name = name;

this.age = age;

}

}
public static void main(String args[]) {

Person person = new Person("John",20);

}
public class Person {

private String name;

private int age;



private Person(String name, int age) {

this.name = name;

this.age = age;

}
public static Person createPersonWithNameAndAge(String name, int age) {

return new Person(name, age);

}


}
public static void main(String args[]) {

Person person = Person.createPersonWithNameAndAge("John",20);
}
Item 2: Consider a builder when
faced with many constructorparameters
public static void main(String args[]) {

MyCharacter character = new MyCharacter.Builder("Merry")

.wearHead(" ")

.wearBody(" T")

.wearPants(" ")

.build();
System.out.println(">>> Item 2:" + character.toString());
}
Item 49: Prefer primitive
types to boxed primitives
int preIntA = 20;

int preIntB = 20;



Integer boxIntA = new Integer(20);

Integer boxIntB = new Integer(20);



System.out.println(">>> preIntA:" + preIntA +", preIntB:" + preIntB);

System.out.println(">>> preEqual? :" + (preIntA == preIntB));



System.out.println(">>> boxIntA:" + boxIntA +", boxIntB:" + boxIntB);

System.out.println(">>> boxEqual? :" + (boxIntA == boxIntB));
Macbook Air 10.11.5 (2013)
public static void main(String args[]) {
Long sum = 0L;

for (long i = 0; i < Integer.MAX_VALUE /100 ; i++) {

sum += i;

}
}
vs
public static void main(String args[]) {
long sum = 0L;

for (long i = 0; i < Integer.MAX_VALUE /100 ; i++) {

sum += i;

}
} 0.936 sec
29.438 sec
MAX_VALUE = 2^31 -1
Integer a = new Integer(20);

int b = 10;

Integer c = a + b;



System.out.println(">>> c = " + c);
Integer c = a + b;
b boxing!→ Integer
Integer c
—————————————————————————-
Integer a = new Integer(20);

int b = 10;

int c = a + b;



System.out.println(">>> c = " + c);
int c = a + b;
a unboxing!→ int
int c
Why need boxed primitive type ?
!#Correct
List<Integer> boxList = new ArrayList<>();

boxList.add(3);
!#Compiler Time Error
List<int> preList = new ArrayList<>();
Type argument cannot be of primitive type
Item 41: Use overloading
judiciously
public class CalculatorMachine {



public CalculatorMachine() {

}

public int add(int a, int b){

return a+b;

}

public int add(int a, int b, int c){

return a+b+c;

}

public int add(double a, double b){

return (int) (a+b);

}

}
public static void main(String args[]) {
CalculatorMachine calculator = new CalculatorMachine();

System.out.println(">>> Item 41:" + calculator.add(1, 5));

System.out.println(">>> Item 41:" + calculator.add(1, 5, 8));

System.out.println(">>> Item 41:" + calculator.add(1.1, 4.3));
}
public static void main(String args[]) {
Set<Integer> set = new TreeSet<>();

List<Integer> list = new ArrayList<>();



for (int i = 51; i $% 56; i++) {

set.add(i); !# [51, 52, 53, 54, 55]

list.add(i); !# [51, 52, 53, 54, 55]

}



set.remove(52);

list.remove(52);



System.out.println(">>> Item 41: set = " + set.toString());

System.out.println(">>> Item 41: list = " + list.toString());
}
public static void main(String args[]) {
Set<Integer> set = new TreeSet<>();

List<Integer> list = new ArrayList<>();



for (int i = 51; i $% 56; i++) {

set.add(i); !# [51, 52, 53, 54, 55]

list.add(i); !# [51, 52, 53, 54, 55]

}



set.remove(52);

list.remove((Integer)52);



System.out.println(">>> Item 41: set = " + set.toString());

System.out.println(">>> Item 41: list = " + list.toString());
}
ref:
https://github.com/HackathonHackers/
programming-ebooks/blob/master/Java/Effective
%20Java%20%282nd%20Edition%29.pdf
code:
https://github.com/jason9075/soohoobookTechTalk

20160616技術會議

  • 1.
  • 2.
  • 3.
    Item 1: Considerstatic factory methods instead of constructors public class Person {
 private String name;
 private int age;
 
 public Person(String name, int age) {
 this.name = name;
 this.age = age;
 }
 } public static void main(String args[]) {
 Person person = new Person("John",20);
 }
  • 4.
    public class Person{
 private String name;
 private int age;
 
 private Person(String name, int age) {
 this.name = name;
 this.age = age;
 } public static Person createPersonWithNameAndAge(String name, int age) {
 return new Person(name, age);
 } 
 } public static void main(String args[]) {
 Person person = Person.createPersonWithNameAndAge("John",20); }
  • 5.
    Item 2: Considera builder when faced with many constructorparameters public static void main(String args[]) {
 MyCharacter character = new MyCharacter.Builder("Merry")
 .wearHead(" ")
 .wearBody(" T")
 .wearPants(" ")
 .build(); System.out.println(">>> Item 2:" + character.toString()); }
  • 6.
    Item 49: Preferprimitive types to boxed primitives int preIntA = 20;
 int preIntB = 20;
 
 Integer boxIntA = new Integer(20);
 Integer boxIntB = new Integer(20);
 
 System.out.println(">>> preIntA:" + preIntA +", preIntB:" + preIntB);
 System.out.println(">>> preEqual? :" + (preIntA == preIntB));
 
 System.out.println(">>> boxIntA:" + boxIntA +", boxIntB:" + boxIntB);
 System.out.println(">>> boxEqual? :" + (boxIntA == boxIntB));
  • 7.
    Macbook Air 10.11.5(2013) public static void main(String args[]) { Long sum = 0L;
 for (long i = 0; i < Integer.MAX_VALUE /100 ; i++) {
 sum += i;
 } } vs public static void main(String args[]) { long sum = 0L;
 for (long i = 0; i < Integer.MAX_VALUE /100 ; i++) {
 sum += i;
 } } 0.936 sec 29.438 sec MAX_VALUE = 2^31 -1
  • 8.
    Integer a =new Integer(20);
 int b = 10;
 Integer c = a + b;
 
 System.out.println(">>> c = " + c); Integer c = a + b; b boxing!→ Integer Integer c —————————————————————————- Integer a = new Integer(20);
 int b = 10;
 int c = a + b;
 
 System.out.println(">>> c = " + c); int c = a + b; a unboxing!→ int int c
  • 9.
    Why need boxedprimitive type ?
  • 10.
    !#Correct List<Integer> boxList =new ArrayList<>();
 boxList.add(3); !#Compiler Time Error List<int> preList = new ArrayList<>(); Type argument cannot be of primitive type
  • 11.
    Item 41: Useoverloading judiciously public class CalculatorMachine {
 
 public CalculatorMachine() {
 }
 public int add(int a, int b){
 return a+b;
 }
 public int add(int a, int b, int c){
 return a+b+c;
 }
 public int add(double a, double b){
 return (int) (a+b);
 }
 } public static void main(String args[]) { CalculatorMachine calculator = new CalculatorMachine();
 System.out.println(">>> Item 41:" + calculator.add(1, 5));
 System.out.println(">>> Item 41:" + calculator.add(1, 5, 8));
 System.out.println(">>> Item 41:" + calculator.add(1.1, 4.3)); }
  • 12.
    public static voidmain(String args[]) { Set<Integer> set = new TreeSet<>();
 List<Integer> list = new ArrayList<>();
 
 for (int i = 51; i $% 56; i++) {
 set.add(i); !# [51, 52, 53, 54, 55]
 list.add(i); !# [51, 52, 53, 54, 55]
 }
 
 set.remove(52);
 list.remove(52);
 
 System.out.println(">>> Item 41: set = " + set.toString());
 System.out.println(">>> Item 41: list = " + list.toString()); }
  • 16.
    public static voidmain(String args[]) { Set<Integer> set = new TreeSet<>();
 List<Integer> list = new ArrayList<>();
 
 for (int i = 51; i $% 56; i++) {
 set.add(i); !# [51, 52, 53, 54, 55]
 list.add(i); !# [51, 52, 53, 54, 55]
 }
 
 set.remove(52);
 list.remove((Integer)52);
 
 System.out.println(">>> Item 41: set = " + set.toString());
 System.out.println(">>> Item 41: list = " + list.toString()); }
  • 17.