Identification of code that compiles in both Java and C # but works differently

I need to port code from Java to C # (and soon vice versa) by copying and pasting and then editing compiler errors. (Accept that this is necessary, I can explain if necessary). Are there cases where the same piece of code works differently in two languages? I would include cases where the order or operation or priority of the operator was defined in one language, but undefined in another, but excluded cases when it was undefined in both.

I would be especially interested in any development tools that could identify such fragments.

@Thomas. Thank you J # is not an option - the code should be in C #. What does "this" mean in your comment?

+4
source share
4 answers

One example is that Java starts variable initializers after the superclass constructor. C # starts them earlier. This affects things if the superclass constructor then calls a virtual method overridden in the class in question. (This is usually a bad idea, but it happens.)

Then, of course, there are generics that are very, very different. For instance:

public class Foo<T> { static int counter; } 

There is one counter variable in Java. In C #, there is one for the type built, so Foo<string>.counter and Foo<int>.counter independent.

I'm afraid I donโ€™t know any tools to define this kind of thing.

I have ported Java to C # myself, and I think itโ€™s very important to try to make the resulting code idiomatically โ€œsharpโ€ as you can. For example, for example, converting getters and setters into properties, and sometimes indexers. Implementing IDisposable when necessary ... such things.

+7
source

Converting from Java to C # is difficult not only because of the language difference, but also because of incompatible base libraries. For me, the best way to convert from Java to .NET uses IKVM - a Java implementation for Mono / .NET

+4
source

Maybe the Java Language Language Translation Assistant Assistant could help you here?

http://msdn.microsoft.com/en-us/library/7tatw8a2(VS.80).aspx

In addition, I would suggest something like Resharper or another refactoring tool to take advantage of the C # language and make your code more efficient and readable.

edit:

V3 is available with VS2005, read this if you don't seem to have installed it: http://natarajana.com/jlca.aspx

+2
source

IKVM is a great option for running Java code in C #. You do not need to change your Java code. It just takes the jar file and generates a .NET dll.

http://www.ikvm.net/

Moving in the other direction, from C # to java, will be difficult, since C # is basically a superset of java. You will need to limit the use of constructs that are not available in java.

0
source

All Articles