+ All Categories
Home > Education > effective java

effective java

Date post: 15-Jan-2017
Category:
Upload: tdc-globalcode
View: 316 times
Download: 4 times
Share this document with a friend
40
Globalcode – Open4education Trilha – Android Bruno Fernandes Campos Desenvolvedor Mobile EFFECTIVE JAVA
Transcript
Page 1: effective java

Globalcode – Open4education

Trilha – Android

Bruno Fernandes CamposDesenvolvedor Mobile

EFFECTIVE JAVA

Page 2: effective java

Globalcode – Open4education

About

Page 3: effective java

Globalcode – Open4education

Why?

Page 4: effective java

Globalcode – Open4education

How?

Page 5: effective java

Globalcode – Open4education

Agenda

• What the difference in performance between for loops?

• How to deal with several parameters classes?• How to make static classes safe?• How to avoid the unecessary objects creation?

• Inside classes• Inside for loops• When concatenating strings

Page 6: effective java

Globalcode – Open4education

Comparing For Loops

Page 7: effective java

Globalcode – Open4education

For Loops In Arrays

Manual for-loop x Enhanced for-loop

Page 8: effective java

Globalcode – Open4education

For Loops In Arrays

Manual for-loop x Enhanced for-loop

SAME PERFORMANCEWHEN READING ENTRANCES

Page 9: effective java

Globalcode – Open4education

For loops in CollectionsFor each X Iterator

Page 10: effective java

Globalcode – Open4education

For loops in CollectionsFor each X Iterator

SAME PERFORMANCE

Page 11: effective java

Globalcode – Open4education

For loops in ArrayListsManual for-loop X For each

Page 12: effective java

Globalcode – Open4education

For loops in ArrayLists

X For eachManual for-loop

Page 13: effective java

Globalcode – Open4education

For loops in ArrayLists

X

1061 ms 1183 ms

For each

100000 strings logged in the console performance test

Manual for-loop

Page 14: effective java

Globalcode – Open4education

Many Constructors Parameters

What should I do?

Page 15: effective java

Globalcode – Open4education

Nutrition Facts Class

Page 16: effective java

Globalcode – Open4education

Telescoping Pattern

Page 17: effective java

Globalcode – Open4education

Using Telescoping Constructor Pattern

● Force some unwanted values● It does not scale well!● Parameters reversion

Page 18: effective java

Globalcode – Open4education

Javabeans Pattern

Page 19: effective java

Globalcode – Open4education

Javabeans Pattern

● Easy to read● Precludes the possibility of making a class imutable● Imutability -> "All of the information contained in each instance is

provided when it is created and is fixed for the lifetime of the object"

Page 20: effective java

Globalcode – Open4education

The builder pattern!

Page 21: effective java

Globalcode – Open4education

Using Builder Pattern

● Easy to read and Write● Safe -> Imutable

Page 22: effective java

Globalcode – Open4education

How to turn static classes safe

Page 23: effective java

Globalcode – Open4education

Static class

Page 24: effective java

Globalcode – Open4education

AndroidAPIUtils Usage

Page 25: effective java

Globalcode – Open4education

Wrong usage

Page 26: effective java

Globalcode – Open4education

AndroidAPIUtils avoiding instantiability

Page 27: effective java

Globalcode – Open4education

Avoid Creating Unecessary Objects

Page 28: effective java

Globalcode – Open4education

Prefeer Static Factory Methods

Constructors will always create a new instance!

Page 29: effective java

Globalcode – Open4education

INSIDE CLASSES

Avoid Creating Unecessary Objects

Page 30: effective java

Globalcode – Open4education

Bad example

Page 31: effective java

Globalcode – Open4education

Use Static initializers

Page 32: effective java

Globalcode – Open4education

Why use static initialisers?● Calendar, Date and Timezone

instances created only once

● Executing 10 milion times : ○ 1st version took 32000ms○ 2nd version took 130 ms

250 times faster!

Page 33: effective java

Globalcode – Open4education

INSIDE FOR LOOPS

Avoid Creating Unecessary Objects

Page 34: effective java

Globalcode – Open4education

Bad practice at for loops

Page 35: effective java

Globalcode – Open4education

Good allocation example

Page 36: effective java

Globalcode – Open4education

WHEN CONCATENATING STRINGS

Avoid Creating Unecessary Objects

Page 37: effective java

Globalcode – Open4education

Bad Practice

Page 38: effective java

Globalcode – Open4education

Bad Practice

● Strings are immutable● Complexity square n

Page 39: effective java

Globalcode – Open4education

Good Practice

● Linear complexity● 85 times faster than the first one for 8.000 strings.

Page 40: effective java

Globalcode – Open4education

QUESTIONS?

This is the END


Recommended