Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I convert Java code to C# code?

Tags:

java

c#

I'm porting a Java library to C#. I'm using Visual Studio 2008, so I don't have the discontinued Microsoft Java Language Conversion Assistant program (JLCA).

My approach is to create a new solution with a similar project structure to the Java library, and to then copy the java code into a c# file and convert it to valid c# line-by-line. Considering that I find Java easy to read, the subtle differences in the two languages have surprised me.

Some things are easy to port (namespaces, inheritance etc.) but some things have been unexpectedly different, such as visibility of private members in nested classes, overriding virtual methods and the behaviour of built-in types. I don't fully understand these things and I'm sure there are lots of other differences I haven't seen yet.

I've got a long way to go on this project. What rules-of-thumb I can apply during this conversion to manage the language differences correctly?

like image 543
Lee Avatar asked Oct 03 '08 20:10

Lee


People also ask

Can we convert Java code to C?

Yes, there's a Java to C source converter: a human programmer. (Reliability may be an issue, though.) If you really want to compile Java to C, you might try compiling Java to machine code with GCJ, then disassembling the machine code, then (somehow?) converting the assembly code to C.

Can we convert Java to C++?

Can we convert Java code to C++? Java to C++ Converter converts only some Java library types to C++ equivalents. The converter offers options to use your own custom replacements. Java to C++ Converter cannot ensure proper deallocation of pointer variables.

Is it easier to code in Java or C?

It's a general consensus that Java is easier to learn because its syntax is closer to natural language than C.

Is C always faster than Java?

C is normally faster than Java, if it is written efficiently, but it always depends on the compiler. Some compilers support optimization on the compile time, to produce more efficient code, by removing redundant code and other unnecessary artefacts.


2 Answers

Your doing it in the only sane way you can...the biggest help will be this document from Dare Obasanjo that lists the differences between the two languages:

http://www.25hoursaday.com/CsharpVsJava.html

BTW, change all getter and setter methods into properties...No need to have the C# library function just the same as the java library unless you are going for perfect interface compatibility.

like image 165
FlySwat Avatar answered Sep 28 '22 08:09

FlySwat


Couple other options worth noting:

  • J# is Microsoft's Java language implementation on .NET. You can access Java libraries (up to version 1.4*, anyways). *actually Java 1.1.4 for java.io/lang, and 1.2 for java.util + keep in mind that J# end of life is ~ 2015-2017 for J# 2.0 redist

  • Mono's IKVM also runs Java on the CLR, with access to other .NET programs.

  • Microsoft Visual Studio 2005 comes with a "Java language conversion assistant" that converts Java programs to C# programs automatically for you.

like image 35
Judah Gabriel Himango Avatar answered Sep 28 '22 09:09

Judah Gabriel Himango