views:

1486

answers:

24

C# has a good momentum at the moment. What are the features that you would need to have in order to switch (or return) to Java?

It would also be quite useful if people posted workarounds for these for the current Java versions, e.g. Nullables being wrapped around custom classes, to make this a much more interesting wiki.

+9  A: 

I would need function pointers of some sort. The ability to use delegates in C# is so useful. There are so many times in Java where I want to create a map of delegates or function pointers as the solution, but I can't. I know you can simulate a similar thing in Java, but having the abstraction as part of the language is a huge plus!

John Sonmez
Anonymous delegates and lambdas would be fantastic - an anonymous classes like "new Delegate() { public void Invoke(String s){ return s+s; } }" just isn't usable compared to "s => s+s".
Callum Rogers
This is as far as I can see syntactic sugar around existing facilities? Is this right?
Thorbjørn Ravn Andersen
I don't think it need function pointers. What it needs is (mostly) the current semantics without all the boilerplate.
Tom Hawtin - tackline
You could simulate this with classes and interfaces, but it is more cumbersome, isn't it?
_NT
@John: I too have long lamented the absence of function pointers/callbacks - you might be interested in my callback solution at http://www.softwaremonkey.org/Code/Callback
Software Monkey
+10  A: 

Just going off the cuff, I think the biggest thing Java 7 needs that everyone is lamenting the lack of is closures.

MattC
Let's hope that they will consider them. Any ideas on how to influence such decisions?! :)
_NT
I hope they don't. If you want closures use Groovy or what have thee. I would like them to add CICE+ARM
mlk
Adding closures to our existing code base would be far better than rewriting everything just to take advantage of one language feature.
MattC
+20  A: 

In my experience, Java vs. .Net is more of a business decision than a technical one. Shops with MS experience trend towards .Net and shops with Java experience trend towards Java & OSS. I've seen little evidence of people switching based upon language features. On the other hand, I've seen shops heavily tilt towards one platform b/c of one or two key employees they wanted to hire being knowledgeable in that area.

jsight
I work on .NET projects and still use lots of OSS. I don't think it should be attached to just the java side.
Mike Two
@Mike - You are, of course, correct that there are .Net projects that use lots of OSS (I'm on one now... not as much OSS as most Java projects that I've been involved with, but definitely a significant amount). Having said that, as a general rule (lots of exceptions), I've seen much less in .Net projects than in Java. For example, I've seen server side guys in .Net who have never even heard of a logging framework (they reimplemented their own... badly). I've yet to meet one in the Java world thats not familiar with at least 1.
jsight
@John - I think in some ways, you have proven my point. Java w/ generics was released in September of 2004. C# with generics was released w/ Visual Studio 2005 in October of 2005. There was never a time when C# had generics and Java didn't.
jsight
The single feature that unambiguously determines that .NET is unusable for us, is that we do not deploy on Windows.
Thorbjørn Ravn Andersen
@jsight - I agree. It is true that you are more likely to find OSS in a Java shop. I just thought your answer made it sound like you would only find OSS on a java project. I still voted for your answer because I agree that it is more of a business decision in most cases. Admittedly I'm picking on a detail that does not detract from the quality of the answer.
Mike Two
@Mike - NP... I can see how it could be read that way.
jsight
Perhaps not shops, but there are people who do actually that. And shops many times make evaluations based on the skill set that they have in conjunction with support in terms of IDEs, frameworks, libraries, etc. So I believe there is some minor degree of influence...
_NT
@jsight "they reimplemented their own... badly. I've yet to meet one in the Java world thats not familiar with at least 1". Are you arguing that Java developers are more intillegent than C#/.NET? The big java logging frameworks are also ported to .NET (see nlog, log4net). Regarding generics, Java does not truly support Generics at all. Generics support is handled and removed at compile time so problems are encountered around reflection and performance.
No longer a user
@NoLonger - No, I'm not arguing about intelligence... just that the .Net developers that I have run into (quite a few for a while) were less knowledgeable of existing frameworks than the Java devs. I was also aware of the .Net logging frameworks (and had used one extensively) at the time. But this is more conjecture than anything else... ymmv. As to generics, it's not really fair to say that Java doesn't have them. It does... they are just implemented really badly (erasure instead of reification). That comment would make more sense if the one it responded to wasn't deleted. :)
jsight
+6  A: 

I think, choice between Java and C# is not a question of language features, but a question of platform and ecosystem choice.

So, I doubt that any new syntactic sugar in Java or C# can lead to a significant amount of switchers between the platforms.

After all, JVM world has Scala and many language-sensitive developers are using it in some way.

Vanya
+6  A: 

Another one I would like is some equivalent to LINQ. I noticed how much I actually depend on the ability to use LINQ when I tried to do a top coder competition and realized they only support .NET 2.0. Once you get used to using LINQ to solve problem and make it part of your regular programming vocabulary it is very difficult to not see problems in that light. It is akin to using generics and then not being able to use generic.

John Sonmez
You can use at least LINQ to Objects on .NET 2 without too much effort: http://www.albahari.com/nutshell/linqbridge.aspx.
Joey
+9  A: 

I'll also give one answer the other way around. C# needs an enumeration implementation like Java has. Java's enumerations rock!

Let me add anonymous overrides to this list also. Sorry to go off topic, but C# needs the ability to anonymously override methods. I have been switching back and forth between Java and C#, and I have to say for unit testing legacy code, there is nothing better than anonymous class overriding.

John Sonmez
C# enumerations are completely type safe base types, Java enums full fledges classes with a nice `const` preset syntax. C#'s enums will be far more performant as they are completely value based.
Dykam
What? Why is the Enumerator class better? All I've experienced of it is just a verbose way of doing it the C# way.
Callum Rogers
The enumeration class in Java allows you to have methods and treat it just like any other class. This is really useful for giving functionality to your enumeration. It also makes a really easy singleton.
John Sonmez
@John:it's benefit is small. enumerations in c# is as fast as an integer variable (it's integer by default but you can define it as a byte for example)
sirmak
@Dykam man if you are worried about performance at that level aren't you more concerned with all C#'s basic types being objects? Or, if you assume the compiler is smart enough to optimize one, why not the other?
Bill K
@Bill K: Only API wise all base types are objects. But all value types which, again, only inherit from object API wise, are no objects on runtime. They live on the stack and behave the same as C++ stackallocated class-instances. That is, by value, and not by reference.
Dykam
A: 

A simple way to map values in your model to the UI (like bind in Java/FX)

Aaron Digulla
+3  A: 

Properties!

Anonymous objects are nice too

JonoW
Java HAS properties already (but they probably are not what you think of :) )
Thorbjørn Ravn Andersen
I don't really get `properties` As I understand they have to be declared public in first place right? if you declare your attributes as public in Java you'll get the same result `public String name; employee.name` I know you can also "add" actual code in the get/set but that's discouraged.
OscarRyz
@Oscar - Properties are simply syntactic sugar for get/set methods (and don't have to be public). It is not like declaring a variable public as the property can do stuff (just like any other method). I use to be dead against them but after using them for a bit they do make the code a little bit more readable.
mlk
No, you can have any access modifier on C# properties. You can also have different access modifiers for the get and set parts of the property. E.g. a public getter but a private setter. Not sure why you say adding code in getters/setters is bad, there are plenty of examples where this is a good idea (authentication, logging). In C#, if you don't need to add any custom code, you can use automatic properties, e.g. public String name { get; set; } .No more complex than a public variable, and if you need to add code to it later, you can expand it.
JonoW
In general, the need for properties implies you might be programming better. Not always, but I can tell you for certain that if you always add setters whenever you create a property you are doing it very wrong. Getters--meh, sometimes you need them.
Bill K
+10  A: 

I'm a C# programmer and about one year ago I had to participate in the development of an application in Java. What I missed the most was:

  • Visual Studio (Eclipse is nice, but VS is VS, especially Team Edition)
  • Comparing strings with ==
  • Properties
  • Basic types as first-class objects (e.g. not "int" type vs "Integer" class)
  • Anonymous methods (a workaround can be made by using anonymous classes but it is not the same)
  • LINQ

And there was one single Java feature that I missed when I went back to C#: explicit exception declaration in method signatures.

DISCLAIMER: I am speaking about a somewhat old project, I don't know if some of these features are present in current versions of Java.

Konamiman
+1 - I missed the same aspects.
Daniel May
Explicit exceptions (checked versus unchecked) is a big issue of debate. Many loathe checked exceptions. Could you tell more about why you missed them? I'm curious to hear it from one who tried both seriously.
Thorbjørn Ravn Andersen
I missed them because it forced me to write self-documented code. When you deal with exception management code, you always know in advance which exceptions you can encounter, which reduces the risk of application-wide-unexpected-exceptions. In .NET, you can only hope that the developer of the library you are using was kind enough to document all possible exceptions thrown by the public members.
Konamiman
The point of the .NET way is that you shouldn't have to document the exceptions. All exceptions thrown are by definition unexpected. I'm not saying it's better or worse than Java, just that when used correctly it makes sense.
Christian Hayter
checked exception is a bad idea. Experiences shows that it seems good for small projects, but a disaster in enterprise ones
sirmak
+27  A: 

As a .NET/C# developer here are the missing features that annoy me. This list in no particular order - just as thoughts come to mind:

  1. The Java library is too small. For common things I have to choose between 5 competing open source products because the base library is lacking in so many ways.
  2. This is an Object Oriented programming language right? Why in the heck do primitive types not inherit from "object"?
  3. Pointers
  4. Lambdas
  5. Closures
  6. Partial Classes and to a lesser extent partial Methods
  7. Real support for Generics
  8. Using statements and Dispose - this was a real WTF for me. You really have to explicitly close connections in try/catch blocks. Poor Java guys!
  9. Yield return would be nice
  10. Unsigned integers - again WTF? I have to use number types larger than I need for what purpose again?
  11. In Java you can return from final blocks of try/catch. A co-worker confused the hell out of me for hours by introducing a bug this way. This behavior should be prohibited as in C#.

I rarely have to use Java and when I do, I have all sorts of WTF moments.

Edit: I Removed for-each comment based on the fact it is no longer a missing Java feature since 1.5.

No longer a user
I would also add lack of a foreach keyword by replacing it with an Iterator class that just increases code verbosity when it isn't needed. And LINQ.
Callum Rogers
Would you mind explaining 12? I've not done anything major with C# but how does it foreach differ from Java for(Object : collection)?
mlk
I would also add extension methods, anonymous types and dynamic types (C# 4.0)
Thomas Levesque
There is a foreach now (not called 'foreach', but the same functionality) So what if it uses Iterable Objects, it serves the same purpose. You can Yield (Thread.yield()). Having to close connections / sockets is a WTF for you? Really? Why would you want to return from the finally block? Seriously, I don't get half of your arguement.
amischiefr
This list is quite comprehensive. With regards to the 'WTF' moments, these exist in all languages, including C# imho.
_NT
As a few of you indicate, I see that as of J2SE 5.0 "for" can be used multiple ways which includes the same behavior as c#'s foreach. So ignore that.
No longer a user
amischiefr, your tone feels argumentative; why is that? This is not a religious language war. Thread.yield() has abosolutely nothing to do with the yield keyword http://msdn.microsoft.com/en-us/library/9k7k7cf0%28VS.80%29.aspx."Why would you want to return from the finally block?" - I don't. In java you can and in C# you cannot. Buggy code can result from returning in a finally block.
No longer a user
I think lack of value types and properties should be on this list. What do you say?
Joren
The Java library is too _small_? That's news to me :)
Thorbjørn Ravn Andersen
Ma teacher told me that Pointers are baaad ;)
tulskiy
Piligrin: That's why you wrap their usage in a `baaad { }` block, erm, I mean an `unsafe { }` block
Joey
Please don't mix pointers into my higher level language.
seanmonstar
What you need is **Groovy**
OscarRyz
Very true, Java has some short comings. But it's always easier to be the guy learning from someone's mistakes (C#) than the guy first implementing the language. Let's be honest C# is a port of Java to .NET. We could talk about how insanely ugly Visual Basic is.
Richard Clayton
Having competing Open-Source-Libraries for a problem is a big advantage!
Mnementh
@Richard - I would say that C# is a response to Java, not a port of Java. In many ways C# draws from a lot of different languages. I don't see that as a bad thing. From a technical perspective, I think C# is rather leading Java in features now rather than the reverse, incorporating more and more features from dynamic languages. Java, too, is morphing but, from an outsider's perspective, more slowly than C#.
tvanfosson
"Please don't mix pointers into my higher level language." ---> (continued: So that I can never do low level stuff the easy way, instead use JNI or some other invention to read and write to a simple bitmap.) <--- Pointers are not overused in C# (actually they are highly underused) because they can only be used inside unsafe blocks, and unsafe blocks need to be enabled using a compiler switch and there are more restrictions: they can't be fields of classes, etc, Java can do the same.
Pop Catalin
Pointers are not a crucial part of C#. It is actually more hidden, but still available to attract C++ developers. C# code that use pointers, must be contained is a special block with "unsafe code". Standard C# code does not use pointers. The memory in .NET is managed and controlled by the framework.
awe
+9  A: 

Less heinous XML parsing and manipulation tools. Doing anything with XML in Java sucks.

Matt Ball
Ahhh. so true! When you can, use JSON instead.
Software Monkey
Since when is JSON easy to generate in Java?
Matt Ball
+6  A: 

Momentum. I have done a lot of development on both platforms. I'm enjoying the .NET side more because of the way the C# language is evolving. Java's evolution seems to be more a reaction to C# than an actual vision. So I think the best thing for Java to do is ignore .NET and create something new of its own.

Mike Two
Curious about the down vote. I think you're well within your rights to disagree and vote down. Not required to comment, but just wondering.
Mike Two
How can you say that when C# was largely inspired by Java? At worst, it is co-evolution, isn't it?
_NT
@ _NT - Agreed that it started that way, but then C# seemed to take the lead in terms of speed of evolution. It was first with generics and attributes just to name 2. So I think it is more in the driver's seat. C# is now influenced by many other languages and programming paradigms such as functional programming and dynamic languages. Those influences are not from Java anymore. After the initial release of C# I can't think of a feature that Java added before C#. But of course the core part of C# is inspired (or even copied) from Java.
Mike Two
Java's evolution now might be reactionary to C#'s, but I beg to differ that this was the case a couple of years ago.
Richard Clayton
@Richard - I didn't say it has always been reactionary. Clearly it can't have been for it's entire lifetime.
Mike Two
+12  A: 

in no particular order:

  • function pointers (delegates); the whole passing interfaces around thing is stupid
  • real generics; what's the point of having type safe generics if the compiler can't even hold the meta data through a unit's compilation?
  • ui speed; all the self-drawing ui libraries are very slow compared to native controls wrapped in "managed" libraries, not to mention that microsoft's self-drawn ui is hardware accelerated through direct3d
  • an yield return construct; c# is so user friendly in this it's crazy
  • operator overloading
  • linq; just cuz its so addicting
  • properties; more synctatic sugar you get addicted to
  • better interop with native code; c#'s p/invoke and native com support makes interop so easy compared to jni -.-
  • first class value types; this goes hand in hand with real generics, having generic lists that never box/unbox in c# is part of why c# is faster than java.

and i'd say this is the most important one:

  • a responsive, informative debugger; nothing can even come close to visual studio right now
Blindy
About debugger and visual studio in general - try IntelliJ Idea (it's opensource now), VS is nothing but a fancy notepad compared to this program. I agree that self-drawing UI is slow, but it's the best solution for cross-platform development.
tulskiy
@Piligrim: Are you really trying to say the .NET/C# debugger is out of date? Have you used it?
280Z28
+5  A: 

I think, you should consider swithcing this holywar from "Java vs C#" to "JVM vs CLR", because JVM is (in the last years — mostly) not only Java, but also Scala, Groovy, Clojure, JRuby, Jython, and dozen of JVM-languages.

folone
Doesn't this apply to CLR languages too?
_NT
I found another one: http://www.jabaco.org/
yelinna
@nt ofcourse it does.
folone
@yellina funny, lol
folone
@nt I mean, the author tries to compare [probably] the best clr language with [IMHO] not the best jvm language. Which is not really "fair", I suppose.
folone
Agree with folone. Clojure and Scala are far better than Java, and I would argue that Clojure is far more powerful than C#.
Richard Clayton
A: 

I think it depends on bussiness decisions, not the languages themselves. But I really really really love C# lambdas and curry :D :D

http://mikeomatic.net/?p=82

yelinna
A: 

a map({codeblock}) which runs over anything iterable would be nice. And filter too. And being able to return multiple values easily from a method.

(a,b,c) = getThreeValues();

(would assign the individual variables a, b and c).

Actually I just think they should have Haskell as a supported scripting language on the JVM :D

Thorbjørn Ravn Andersen
+2  A: 

Continuations, like Scala (on top of the rest) would be good too, for agents development.

Nikolaos
+2  A: 

Support to run over CLR (and vice versa perhaps for JVM) without IKVM and other such layers.

Scooterville
A: 

on the other side, has .net got enterprise open projects like Terracotta (semi commerical), Infinispan, Compass ? no. ncache (commerical), lucene.net are far behind them. Especially Terracotta is unique, it can improve some of your app x100, it's simply perfect and free (partitioning is commerical). If we implement a high load app in both .net with anything and java with terracotta + hibernate + terracotta-hibernate-integration, java app will probably far more performant than .net one. Ther're some ports to .net like nhibernate, log4net, lucene.net, but all of them are trying to catch java versions. And entity framework is a disaster, they have to start with nhibernate as the base or get some lessons from them.

.net is only working on windows (mono is far from enterprise, there isn't any enterprises using it), how much money does myspace.com spent for their 4000 windows web server licences ? 1 million ?

You can't install a simple plugin to VS (and can't do some other things) if don't buy professional edition,

Some windows instance types in amazon web services are nearly x2 price of the linux ones.

if you look at ohloh.net open source java project counts (with language comparison tools), you'll see that java has x5 more volume than c#.

also Java has %20 share on worldwide while c# has %4.x (source:tiobe.com)

Look at the top web sites; only microsoft and myspace are using .net. google, amazon, ebay, linkedin, alibaba, twitter (switched to scala from rails)... many of them are using java and many others are using php, ruby (facebook, yahoo,..)

As a language Java is far behind c#, but Scala (runs on jvm) is as good as c# and it's performance is nearly same as java and also it can use all jdk and other java code as his library.

I'm not saying java is better, but I'm saying java is as valuable as .net, too.

sirmak
A: 

linq, lambda, anonymous types

Omu
+1  A: 

A native 'decimal' type for Java replacing the BigDecimal class would be nice. But Java thread-safe collections are nice.

Perhaps the question is not so much what Java 7 needs to persuade developers to use it, but more a case of what makes developers want to move from Enterprise/server-side java language to a different C# desktop Windows-only oriented world?

For most developers, the language isn't difficult to pick up be it Java or C#. I develop in Java, but Linq didn't take long to understand and use.

I think the choice of Java or C# depends on what motivates you personally -perhaps money? in which case either language will do whether they have certain features or not.

Ste

SteJav
A: 

Let's be clear that we need to distinguish between Java and the JVM. I actually switched from C# to Java, but I admit it was not because of Java's amazing language features! In my very humble opinion, C# is the better language, and CLR is the perhaps a more elegant VM. However, even with Mono, you're not writing applications that run everywhere!

I think the greatest argument for Java is the amazing community it has. This is where the cutting edge technology is being developed, not at Sun (now Oracle). The Java community has consistently been the leader in developing Enterprise technologies. For instance, how long did it take Microsoft to provide a DI Framework? Where's AOP in .NET? When I start a .NET project, my foundation is Spring.NET, a Java port. When I need ORM, it's NHibernate. Need a testing framework? NUnit. I realize there are other OSS projects for the .NET platform, but their numbers and support from Microsoft are laughable compared to the Java community.

Richard Clayton
A: 

They should start by fixing the Calendar/Date related classes, even that seems too much to ask.

hawk
A: 

I don't see any point in "luring" anyone anywhere. They solve different problems and you should use whichever one suits you.

Java has less "Language" and less structures to trip up new people, it's platform independent and it doesn't change too fast allowing old code to stick around for a LONG time (Good for some large companies).

C# has tight desktop integration and a slew of nice features that make it more fun to program. It has .net integration. It has pointers, closures, etc which make it harder for n00bs (a valid language target, hence Basic) but more fun for experienced programmers--I haven't convinced myself that these features make you more productive, but in some situations they can make your code a lot nicer! Also if you need pointers, you need pointers.

I don't see a whole lot of overlap in target audiences. Why change a language to attract programmers when it's healthier for the entire industry to have two healthy languages each targeted at solving different problems attracting different developers?

Bill K
Because being a "n00b" language as you call it, will actually hurt Java not help it in any way. Lack of modern features do make Java easier to learn, but also make Java applications harder to write (due to the lack of modern features). It's like Pascal vs C in the old days, Pascal was used for learning (by n00bs) but C kept gaining ground because it was more "powerful", until Pascal faded away.
Pop Catalin