Your SlideShare is downloading. ×

Advanced java

949

Published on

Some important issues

Some important issues

Published in: Technology, Education
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total Views
949
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
22
Comments
1
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Giacomo Veneri http://jugsi.blogspot.com1 Just to drink a cup of coffee maybe you don't know … about java • Java – Passing by reference or by value (final keyword) – stricftp keyword – Equals .. be caught • Java EE – Do NOT override java.library.path on application server – Transient work on cluster • Use Collection Efficiently • Multithreading – TimeZone is synchronized – DateFormat is NOT thread safe – Loggings should be synchronized – Volatile keyword
  • 2. Giacomo Veneri http://jugsi.blogspot.com2 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.
  • 3. Giacomo Veneri http://jugsi.blogspot.com3 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
  • 4. Giacomo Veneri http://jugsi.blogspot.com4 Java: equals byte b = 'a'; byte c = 'a'; String s = “a”; s.equals(b); //return false s.equals(c); //return false b == c; //return true s ==”a”; //return true •Formally speakingFormally speaking: Object.equals return false when the object's classes are different, value (or refrence) are different •HoweverHowever: litterals or charecter/integer between 0..128 could surprise us due to java optimization •SuggestionSuggestion: use equals for Object or primitive wrapper and “==” for primitive •RemeberRemeber: when you re-implement equals on your own class re-implement also hashCode() method
  • 5. Giacomo Veneri http://jugsi.blogspot.com5 Java: init class MyClass { public MyClass() { //constructor } { //init before constructor } } • Formally speaking: the block highlighted is callde before the constructor
  • 6. Giacomo Veneri http://jugsi.blogspot.com6 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
  • 7. Giacomo Veneri http://jugsi.blogspot.com7 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
  • 8. Giacomo Veneri http://jugsi.blogspot.com8 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
  • 9. Giacomo Veneri http://jugsi.blogspot.com9 JEE: java.library.path • When you need to connect your java EE application to an EIS you must use JCA –Connector 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 server –Weblogic 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 it –Be caught! On multi server or cluster environent you can modify the entire behavior
  • 10. Giacomo Veneri http://jugsi.blogspot.com10 MT: transient @EJB transient 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
  • 11. Giacomo Veneri http://jugsi.blogspot.com11 java.util collection efficiency from 10 to 1 10 new HashMap() 08 new TreeMap() 03 new Hashtable(n) : synchronized and define capacity 02 new Hashtable() : synchronized 01 Collections.synchronizedMaps (new HashMap()) 01 Collections.synchronizedColle ctions(new ArrayList()) 03 new Vector(n) : synchronized and define capacity 02 Vector : synchronized 10 Iterator, Enumeration 09 new ArrayList(n): define capacity 08 new ArrayList() 07 new LinkeList(n) : manage queue and define capacity 06 new LinkeList() : manage queue 05 new HashSet() : prevent duplicate 04 new TreeSet() : implements sorting
  • 12. Giacomo Veneri http://jugsi.blogspot.com12 java.util collection The given table is just a reference to take in consideration but Map and set are not comprable ... 1.Use ensureCapacity for better performance to write data 2.Performance of LinkedList decreases when size increases 3.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
  • 13. Giacomo Veneri http://jugsi.blogspot.com13 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
  • 14. 16/07/13 Giacomo Veneri - http://jugsi.blogspot.com14 Giacomo Veneri, PhD, MCs (IT Manager, Human Computer Interaction Scientist) @venergiac g.veneri@ieee.org http://jugsi.blogspot.com My Professional Profile http://it.linkedin.com/in/giacomoveneri My Publications on HCI http://scholar.google.it/citations?user=B40SHWAAAAAJ My Research Profile http://www.scopus.com/authid/detail.url?authorId=36125776100 http://www.biomedexperts.com/AuthorDetailsGateway.aspx?auid=2021359 https://www.researchgate.net/profile/Giacomo_Veneri/

×