mercredi 4 septembre 2013

if else != :? ... a bit weird but let me explain


in a lot of tutorial you might see that the the ternary operator ?: is similar to an if ... else statement
A lot of tutorial are wrong on this subject
Trust me !
euh not just trust yourself ... and the compiler ... and the offical doc


the basic : 

?: operartor is very similar to if else


public class dummy {
 
 public static int f1_int() {return 1;}
 public static int f2_int() {return 2;}

 public static void main (String [] args)
 {
 boolean b = true;
 // this line
 int a = b?f1_int():f2_int();
 // is similar to this one
 if (b) { a = f1_int();} 
 else { a = f2_int();}
 }
}


But not fully : First Difference
?:  can be use only for assignment  !
not convinced , remove the assignment in the ?:
  • From :    int a = b?f1_int():f2_int(); 
  • To :    b?f1_int():f2_int();
it does not compile anymore : (

this brings some consequence  when you write condition?expression1:expression2
  • expression1 and expression2 cannot return void.
  • expression1 and expression2 must be "compatible" in term of type ( boxing , inheritance etc )

The last condition is not so simple . here is the full and official description that you can trust
here is the link to the official specification of the operator ?:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.25

for instance look on this tricks , what is the result  ?


public class dummy {

 
 public static int f(Object o) { return 1; }
 public static int f(Integer o) { return 2; }
 

 public static void main (String [] args)
 {
 boolean b = false;
 int a =  f(b?new Object():new Integer(44));
 
 System.out.println(a);
 }
}

this displays 1, even if b is false. You might believe that f(Integer) will be called but not
it is because the first operand has been detected as Object and the second will be assumed as Object too

if you do that using if , you will not have the same result

mardi 20 août 2013

@ Your web service sir !



With Java 7, we cannot imagine how simple is to make a web services server

//My OneLine Server ! 
package ws;

import javax.xml.ws.Endpoint;

public class ServerLauncher {

    public static void main(String[] args) {
        Endpoint.publish("http://localhost:29057/WS", new HelloImpl());
    }

}
//MyData 
package ws;

public class Mister {
    
    public String name;
}
//My Service
package ws;

import javax.jws.WebService;

@WebService
public class HelloImpl {

    public String hello (Mister m)
    {
        if (m.name.equals(""))
            {return "Hello Nobdy";}
        else 
            {return "Hello " + m.name;}
    }
}

then use your best SoaopUI and test !
then you can enrich the sample with more annotation

you can easily genereate a client using wsimport based on the wsdl there (http://localhost:29057/WS?wsdl)

be careful with the generation, it generate server interfaces ... but annotation has to be cut/copy into the implementation as they are not inherited ...

mercredi 31 juillet 2013

Are you Concurent


ConcurentHash map looks nice , and looks designed for concurency ... but even in a single threaded mode it can be "dangerous"

have a look in this Article , especially at the end


http://www.javacodegeeks.com/2011/05/avoid-concurrentmodificationexception.html



mercredi 17 juillet 2013

Legacy is cool




One thing is this world won't change soon ... is time always keep going on. 
The code you are writting few might be in most of the case legacy in few month, week, decade. 
In large scale software environment it is a fact. 

here is a presentation about how to deal with Legacy system, how to modernize system to make them more agile. 
i ll put more on that subject ... when this presentation became ... legacy



Today i learned a new word : Refuctoring


The process of taking a well-designed piece of code and, through a series of small, reversible changes, making it completely unmaintainable by anyone except yourself

From This















To ....


A lot of this stuff can be found there 


lundi 15 juillet 2013

It s cold let s hibernate ! :)

A set of (very) basics sample for hibernate but very clear

notice that i would recommand the non specific usage of JPA (having already a bad experience with ORM it is always safer to not go too deep in that stuff ... it avoids heavy wounds)


G1


 A lot of article deals with Hotspot, but since JDK 1.7 u4 i would say that most of those information are deprecated because of G1


What is behind G1 ?

a long presentation made by Charly Hunt one of the hotspot Guru
http://www.infoq.com/presentations/java-g1

and a smaller reading still on infoQ
http://www.infoq.com/articles/G1-One-Garbage-Collector-To-Rule-Them-All

this article has been followed by  a tunic tips
http://www.infoq.com/articles/tuning-tips-G1-GC