Using different data types between Java and WCF

May 07, 2009

Many C# or Java data types can be described by using XML Schema so that we can use XML Schema to communicate between Java and WCF.


  1. Primitive Data Types Primitive data types such as byte, short, int, long, float, double, boolean, char are almost the same in C# and Java. You can use these data types in WCF Contract normally. But there are some points you must pay attention to:         using short in Java when the type is byte in C#;         using int in Java when the type is char in C#.

Here, I listed the details of primitive data types:

Java Client Data Type C# WCF Data Type
short byte
short short
int int
long long
float float
double double
Boolean Boolean
int char
  1. String C# has a System.String class which is analogous to the java.lang.String class. In C#, the string class can be written as string or String.

  2. Enumeration WCF sends and receives an enumeration value with its name in the String format. For example, for an enumeration variable (C#) as follows:

public enum EnumColor {
  enumBlack = 0,
  enumWhite = 1,
  enumRed = 2 

The generated code is as follows:

@XmlType(name = "EnumColor") 
public enum EnumColor {
  private final String value;
  EnumColor(String v) {
    value = v;
  public String value() {
    return value;
  public static EnumColor fromValue(String v) {
    for (EnumColor c: EnumColor.values()) {
      if (c.value.equals(v)) {
        return c;
      throw new IllegalArgumentException(v);     
  1. Complex Types: Structure or Class
public class CompositeType {
  bool boolValue = true;
  string stringValue = "Hello ";
  public bool BoolValue {
    get { return boolValue; }
    set { boolValue = value; }
  public string StringValue {
      get { return stringValue; }
    set { stringValue = value; }

“[DataContract]” and “[DataMember]” make a class or a structure serializable. If a member variable is declared as a [DataMember], it will be serialized. Then you can write the following code in Java class: ObjectFactory of = new ObjectFactory(); CompositeType composite = of.createCompositeType(); composite.setBoolValue(true); composite.setStringValue(of.createCompositeTypeStringValue("world")); CompositeType returnComposite = port.getDataUsingDataContract(composite);

  1. Array, List The Array or List data types can be used as parameters in WCF Operation Contract and the generated Java code of these two data types is the same. Metro generates a new data type by adding a prefix “ArrayOf”. For example: List and String[] are both translated to ArrayOfstring. This class can be used in Java as follows:
ArrayOfstring arrayValue = new ArrayOfstring();
  1. DateTime There is a data type named “DateTime” in C#. It describes date, time and time zone information. For the DateTime type, Metro generates Java code with the corresponding type “XMLGregorianCalendar”. You can use the XMLGregorianCalendar type directly. However, Java generally uses “GregorianCalendar” to declare date time variable. Therefore, it is necessary to make a conversion between GregorianCalendar and XMLGregorianCalendar.
/*Convert GregorianCalendar to XMLGregorianCalendar*/
public XMLGregorianCalendar convertCalendarToXMLCalendar(GregorianCalendar objGregorianCalendar) {     XMLGregorianCalendar objXMLGregorianCalendar = DatatypeFactory.newInstance().
    return objXMLGregorianCalendar;
/*Convert XMLGregorianCalendar to GregorianCalendar*/
public GregorianCalendar convertXMLCalendarToCalendar(XMLGregorianCalendar objXMLGregorianCalendar) {     objXMLGregorianCalendar.objXMLGregorianCalendar.toGregorianCalendar(); }

Download source code

Search Blog Posts