Jul 15 2009
How to write Java code?
- Use “class” instead of “interface” in enumerations and add “public final static” keywords before enumerations.For example:
public class EnumEOL
{
public final static int enumEOLNative = 0;
public final static int enumEOLWindowsCRLF = 1;
public final static int enumEOLUnixCR = 2;
public final static int enumEOLMacLF = 3;
} - Don’t use “e.printStackTrace” in try… catch and System.out.println(e.getStackTrace(…)) in classes.
- Don’t use annotations like @Override, @suppresswarning.
- Use “CompareTo” and “CompareToIgnoreCace” to compare strings. Don’t use “equels ()” or “==”.
- Replace all “String” with “CString”, which is a class defined in the CSharpJavaMerger Framework, except for the following two places:
- Don’t change the “String” in “String Object”.
- Don’t change the “String” in string constant. For example, public final static String VSSVER= “vssver.scc”
Two things to notice:
- Use “string == null” instead of “CSting == null || CString.getValue() == null”.
- The logic of Substring (int, int) is changed. The meaning of the 1st parameter is the same in C# and Java, while the 2nd one not. The 2nd parameter is the subscript of the substring in Java, and it means the length of the substring in C#. The substring of “CString” here is same with substring in C#.
- Use Debug.Assert (b) instead of assert (b). Debug is a class defined in CSharpJavaMerger Framework.
- Omit “public” keyword in the member functions in interfaces.
- Make sure the subclasses’ functions, including returned value, function names, parameter list, are different from that of the superclass prototype, except implementation classes of Abstract and interface.
- The name convention for variables of checkbox control variables: m_chk, for example, m_chkUseSSL.
- Exception processing
- Only catch the exceptions in java.lang.Exception, which is a base class of Java.
After caching an exception, if any functions in java.lang.Exception need to be called, please add the objects in the constructor of CException, new an object of CException and call the according function of CException.For example:
try
{
//...
}
catch(Exception e1)
{
CException exp = new CException(e1);
CString str = exp.GetMessage()
} - Use e.GetMessage() instead of e.getLocaleMessage().
- Only catch the exceptions in java.lang.Exception, which is a base class of Java.
- File, I/O classes processing
- Use CFile instead of File. CFile is a class in CSharpJava Framework. The class is used to judge file path, whether file exists or not, file’s properties, and whether files is deleted.
- Use FileStream to read/write binary.
- Use StreamWriter/StreamReader to read/write text.
- Use CFileInputStream to read all files.
- Use CFileOutputStream to write to all files.
- Use “DateTime” instead of “Calendar”. DateTime is a function in CSharepJavaMerger Framework.
- Use Guid instead of UUID.
- Omit Constructors called by Constructors. Replace “this ()” in Constructors with corresponding functions.
- Add “break” before Default in switch case.
- Use Boolean instead of Boolean. Boolean is a system class.
- Replace Anonymous inner classes with inner or common classes.
- Use List, Map etc instead of ArrayList, HashMap for statement. For example:
Use: CList list = CList();
Instead of :ArrayList list = ArrayList()
- Use List, Map etc instead of ArrayList, HashMap for statement. For example:
- There may be some tricks when adding strings. For example:
- CString str1 = new CString(”hello”);
- CString str2 = new CString(”world” + str1);
//This line can be compiled successfully.
//However, what you want may be CString str2 = new CString(”World” + str1.getValue());
- Use CThread instead of Thread.
- Use DESUtil instead of DES.
- Capitalize the name of all packages.
- Replace the function used for sorting “Collections.sort(listChangeSet,comparator);” with “list.Sort(Comparer comparer);”. Compare in a class in CSharepJavaMerger Framework.
For example, use the following code:
class SortListComparator extends Comparer<Object>
{
public int Compare(Object obj1, Object obj2)
...
}
Rather than:
class SortListComparator implements Comparator<Object>
{
public int compare(Object obj1, Object obj2)
...
} - Use CMap instead of Map. For example:
Use:
“CMap map = new CMap();”
Instead of:
“Map map = new HashMap();” - Don’t use Integer and Long system functions.
- Change private inner constructors to be public.
- If a member variable of a private inner class needs to be used by its external classes, please change the member variable to public.
- If you want to convert between CString and Long, integer, please use Str.ToLong() instead of new Long(CString.GetValue).longValue().
- Add [] before array definition like this:
String [] var = null - Don’t use keywords as variables. For example,
Do not use “out” which is in CFileStream out;. - Don’t get the string out of CString and then call the functions of String. For example:
- Don’t use the following way: CString str; str.GetValue().compareTo
- Other classes either. Don’t get the system classes and then call system functions.
- Don’t use the following functions:
- Integer.intValue()
- new Integer()
- new Long
- Long.longValue()
- If conditional expression (? : ) uses string, it’s not CString.
Links:
Previous article >>>>: How to write C# code?
Next article >>>>: License
WCF & Java Interop series home page: CSharpJavaMerger Framework

