Advanced java

Embed Size (px)

Citation preview

Just to drink a cup of coffee
maybe you don't know about java

JavaPassing by reference or by value (final keyword)

stricftp keyword

Equals .. be caught

Java EEDo NOT override java.library.path on application server

Transient work on cluster

Use Collection Efficiently

MultithreadingTimeZone is synchronized

DateFormat is NOT thread safe

Loggings should be synchronized

Volatile keyword

Java: passing parameter by reference or by value?

public void myMethod(MyObject a, int b) { a.setInternalValue(mystring); b++;}

Formally speaking: java passes parameter by value.

However: experienced programmers know that the code listed here will modified the status of 'a' outside the scope of the method and b will NOT changed outside the scope of the method.

Indeed java passes for b the value of b and for a the value of the refrence to a instance.

Then: java passes the value of parameters on both cases.

Suggestion: consider the final keyword, to use im-mutable object or to make a copy before calling the method.

Java: stricftp

public strictfp double callBystrictFP(double a) { return (a / 2 ) * 2 ;}..callBystrictFP(Double.MIN_VALUE); // it will return 0 instead of 6..E-341

Formally speaking: FP-strict expressions must be those predicted by IEEE 754 arithmetic on operands represented using single and double formats

However: from 1.2 java implementation of FP arithmetic is platform dependent

Then: to ensure IEEE 754 arithmetic use strictfp

Suggestion: if you are an ERP programmer consider BigDecimal and MathContext for approximation, it should save your life

Java: equals

byte b = 'a'; byte c = 'a'; String s = a;s.equals(b); //return falses.equals(c); //return falseb == c; //return trues ==a; //return true

Formally speaking: Object.equals return false when the object's classes are different, value (or refrence) are different

However: litterals or charecter/integer between 0..128 could surprise us due to java optimization

Suggestion: use equals for Object or primitive wrapper and == for primitive

Remeber: when you re-implement equals on your own class re-implement also hashCode() method

Java: init

class MyClass { public MyClass() { //constructor } { //init before constructor }}

Formally speaking: the block highlighted is callde before the constructor

MT: SimpleDateFormat

static DateFormat df = new SimpleDateFormat(yyyy.MM.dd G 'at' HH:mm:ss z);df.parse(..);

Formally speaking: SimpleDateFormat is not thread safe

Then: if you declare SimpleDateFormat static or on a singletone class on Multi Thread enviroment (EJB, Web,...) you will experience the most absurd bug of your life

Suggestion: do NOT declare SimpleDateFormat as attribute or static member of your class

MT: logging

Some frameworks such as log4j or logback block execution (synchronization) to write into a file.

Suggestion: consult logback or log4j documentation to use Async Logging

MT: volatile

volatile MyCache cache = null;

public MyCache getCache() {if (cache ==null) || (elapsed(cache)) { synchronized(this) { if (cache ==null) { // create cache } }} else return cache;}

Formally speaking: thread should save local variable on thread stuck

Then: if another thread acquire the lock and modify the local variable the second thread could NOT see the change

Suggestion: volatile force s the thread to read the in memory variable; volatile reduces performance (1:10)

Reference: look for double check on google

JEE: java.library.path

When you need to connect your java EE application to an EIS you must use JCAConnector Architecture (JCA) is a Java-based technology solution for connecting application servers and enterprise information systems (EIS)

However: if you are too lazy to implement your JCA component you might be tempted to declare the java.library.path to point your native library

Then: you can overwrite the native library used by your application serverWeblogic uses java.library.path to pint native io in order to improve performance

Suggestion: use JCA . or . find java.library.path of your application server and adjust itBe caught! On multi server or cluster environent you can modify the entire behavior

MT: transient

@EJBtransient MyEjb myEjb;

Formally speaking: using transient java doesn't serialize the object, rseources (Connection or EJB) on JEE cannot be serialized

Then: on Migratable server two node could share session each other

Suggestion: use transient when you reference resources

java.util collection
efficiency from 10 to 1

10 new HashMap()08 new TreeMap()

03 new Hashtable(n) : synchronized and define capacity02 new Hashtable() : synchronized

01 Collections.synchronizedMaps(new HashMap())

01 Collections.synchronizedCollections(new ArrayList())

03 new Vector(n) : synchronized and define capacity02 Vector : synchronized

10 Iterator, Enumeration09 new ArrayList(n): define capacity08 new ArrayList()07 new LinkeList(n) : manage queue and define capacity06 new LinkeList() : manage queue05 new HashSet() : prevent duplicate04 new TreeSet() : implements sorting

java.util collection

The given table is just a reference to take in consideration but Map and set are not comprable ...

Use ensureCapacity for better performance to write data

Performance of LinkedList decreases when size increases

Iterator is faster than for cycle over set, but, on Map, when you ned to create iterator by keySet, doesn't produce any valuable difference

Quiz ;-)

How many issues did you know

3 maybe you are 3 year experienced programmer

5 maybe you are 5 year experienced programmer

7 maybe you are junior JEE architect

10 you are senior JEE architect

..or maybe not

Giacomo Veneri, PhD, MCs(IT Manager, Human Computer Interaction Scientist)@venergiac

[email protected]

http://jugsi.blogspot.com

My Professional Profilehttp://it.linkedin.com/in/giacomoveneri

My Publications on HCIhttp://scholar.google.it/citations?user=B40SHWAAAAAJ

My Research Profile http://www.scopus.com/authid/detail.url?authorId=36125776100http://www.biomedexperts.com/AuthorDetailsGateway.aspx?auid=2021359https://www.researchgate.net/profile/Giacomo_Veneri/