2

I like C#, but I have to write Java and I really dislike Java as a language. But what is the difference? If we omit structs and interop related stuff, then the resulting C# code should be easily compilable into JVM, isn't it?

So, my question is this - is there a tool that:

  1. Can compile C# into JVM, provided that the C# code is compatible with JVM and fail the compilation if not?
  2. Makes it possible to reference Java libraries from such C# code?

Thanks.

EDIT

When I mean the languages are similar, I mean that the basic concepts should be implemented similarly in JVM and MSIL. As for the languages, I find the Java language too verbose and inconvenient to program compared to C# 4.

EDIT2

Another clarification. I am not intending to target both CLR and JVM. At the end of the day, I need JVM. My only problem is that it feels really awkward to program Java after having programmed C# 4, which has all these little things, which are usually no more than syntactic sugar of the compiler, but they make all the difference.

mark
  • 59,016
  • 79
  • 296
  • 580
  • 5
    And my question is, if they're so similar why not just write Java? – Andrew Cooper Nov 20 '11 at 11:16
  • 1
    For info, the reverse is possible - see [IKVM](http://www.ikvm.net/uses.html) – Marc Gravell Nov 20 '11 at 11:19
  • 1
    I love both `C#` and `Java`. Programmers should be open-minded to different languages(also `C#` and `Java` are not very different as syntax). – Petar Minchev Nov 20 '11 at 11:21
  • If Java to you is "C# without structs and with limited generics", aren't you already writing Java? Also see J#. – BoltClock Nov 20 '11 at 11:21
  • Take a look: http://stackoverflow.com/questions/682608/implementing-c-sharp-for-the-jvm – Marc Schlösser Nov 20 '11 at 11:23
  • 3
    Not sure why the downvotes; seems a pretty fair and reasonable question to me – Marc Gravell Nov 20 '11 at 11:28
  • 1
    If you want to swim in the river, you should make friends with the crocodile :-) Write in Java, follow it's conventions, do it good, and Your co-worker will love you... – KarlP Nov 20 '11 at 11:58
  • @mark: if you're interested in writing code targeting both the *.Net CLR* and the *Java VM*, then you may want to check the "fantom" programming language. It's an interesting project: http://fantom.org/ – TacticalCoder Nov 20 '11 at 13:11

2 Answers2

1

You're probably better off googling for a C#-to-Java code converter and then compiling that code as normal Java.

Tudor
  • 61,523
  • 12
  • 102
  • 142
0

You could even implement full c# compiler for jvm. Generics the way they are and missing structs are part of java the language not the virtual machine. And they are the way they are because of backwards compatibility.

I don't know if there exists any c# to jvm bytecode compiler, and I don't think there's enough incentive to create one.

soulcheck
  • 36,297
  • 6
  • 91
  • 90
  • 1
    your point about generics and structs is not quite accurate, IMO. In .net, these get support from **both** the compiler **and** the runtime. It would be a challenge to make them work compiler-only. Or at least, it would work in a heavily limited way, like it does in MonoTouch/AOT – Marc Gravell Nov 20 '11 at 11:23
  • They point is he'd have to implement c# generics from the ground up, instead of using the ones provided by java. There's no problem in implementing runtime support for generics. Generics aren't part of jvm, they're part of java standard library. – soulcheck Nov 20 '11 at 11:40
  • 1
    Which (the point I was trying to make) **is not possible** just at the compiler (except in a very limited way) – Marc Gravell Nov 20 '11 at 11:45
  • How isn't it possible? jvm bytecode is turing complete - there's nothing in c# that can't be compiled to jvm bytecode. – soulcheck Nov 20 '11 at 11:50
  • exactly - my point is: the **language** (c#) doesn't provide these services. The CLI (think: JVM) does **itself**. Without the CLI support (i.e. if implemented at the compiler), you would have much more limited features – Marc Gravell Nov 20 '11 at 12:07
  • 2
    And re being Turing complete - that is irrelevant; show me some JVM bytecode that talks to an array of custom value-types, for example. Let alone any of the pointer-work (aka "unsafe", but a real part of the language). There genuinely are things in c# that do not have JVM counterparts. – Marc Gravell Nov 20 '11 at 12:11
  • Pointers aren't supported i'll give you that. Still, I don't see any problem in implementing jvm c# compiler which stuffs it's own non-type erasing jvm bytecode generics into any program it compiles. – soulcheck Nov 20 '11 at 12:42
  • @soulcheck We eargerly await your proof of concept. Or, I'll accept any reference to prior art that shows it has been done – sehe Nov 20 '11 at 19:58
  • Agree with Marc and sehe there: Just because something is turing complete doesn't mean you can implement/emulate every construct out there. Generics were explicitly implemented the way they are now because backwards compatibility with JVMs WAS important. You'll note that when MS implemented Generics in C# they broke backcomp. You can still implement a C#->Java converter but you'll lose expressiveness. The final result (ie executed code) should still be ok.. generics limit the programmer don't give her additional possibilities. – Voo Nov 20 '11 at 20:22
  • cont. But there are other things that seem even more problematic.. contravariance in C# 4.0 for one? – Voo Nov 20 '11 at 20:25
  • Aye, i went too far saying one can emulate everything. On the other hand, since it looks like I'm the only one thinking it's possible to implement .net generics it should be relatively easy for to prove me wrong by pointing to some article or even posting the answer here. What is really that difficult in implementing .net like generics for jvm if you don't have to care about backwards compatibility? I just don't see it. – soulcheck Nov 20 '11 at 20:29
  • Suppose this thread should have been moved to chat. I don't know how to move it there now :) Anyway I'd be very interested to see some articles that at least talk about changes that would have to be made to jvm to make those generics work. I did a quick google but all I found was rather superficial or talking about syntax instead of implementation. In particular it would be interesting to see why those changes have to be implemented on virtual machine level instead of compiler level. – soulcheck Nov 20 '11 at 22:59