Is LINQ leaving Java in the dust?

Tags:

Interesting story today asking whether LINQ has given .NET an edge over Java. LINQ is best-known as a way to embed SQL-like statements directly in code. However, it's really a much deeper technology that allows you effectively build DSL-like constructs in libraries that work as if they are part of the language. And that is undeniably powerful.

From what I've seen, the C# code using LINQ also seems to be relatively readable (due to the DSL-like nature of it) and that is a big win from both the power and complexity standpoints.

So, then we have the question of Java, which does not (currently) have the ability to weave structures into the language as elegantly as LINQ. I think the closures work could make that possible in Java 7, but we're talking probably 2010 before Java 7 is in common use.

From a language feature perspective, C# and LINQ are evolving faster and adding more important features than Java.

We have to take a step back though and consider some other things. C#/.NET are not driven by standards or a loose coalition of competing/cooperative companies; they're driven solely by Microsoft. And Microsoft is happy to break backwards compatibility and force platform upgrades as the sole provider of the technology. Things are a bit more bohemian in the Java world and the JCP may be the tortoise to Microsoft's hare in this regard.

But I wouldn't count Java out.

The addition of annotations, static imports, and generics (even crippled generics) has opened some interesting opportunities and I think we've only recently started to see people target and push those features to their fullest. I suppose the poster-child for this in the context of LINQ must be Quaere, which aims to do much the same as LINQ by building an internal DSL. Guice is another good example of pushing these features to their limit.

You might also point to the possibility of dropping into Groovy and using builders or other DSL-friendly tools there. Or moving slightly farther afield, use JRuby or Scala to develop the data access parts where you need more flexibility.

Between a quickly-evolving .NET with a limited community vs a more slowly-evolving Java with a rich community, gimme the Java world any day.

But, I'd like to hear what you think. Are C#/.NET innovations changing your mind? Are they just a call for improvement in the Java world? What can we learn from each other?

Average: 4 (4 votes)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Denis Robert replied on Wed, 2008/01/30 - 12:45pm

Call for improvements in the Java world... .NET is M$s beast, and it's hostage to their marketing whims.

 

period.

 

Dean replied on Wed, 2008/01/30 - 12:46pm

They're doing it so we have to do it too!.... right?

chhum replied on Wed, 2008/01/30 - 12:48pm

Well competition has made Java stronger in general terms. We probobly got annotations thanks to C# and these are clearly a good thing.  Lots of people think generics were only added because of C# to although this isn't actually the case.

LINQ is an interesting conceptually but it turns out to be pretty disappointing I think.  For a start the LINQ to SQL stuff is awful - it can't handle basic things like GROUP BY without using multiple queries or complex workarounds and is horribly inefficient.  Moreover the amount of type inference that is now present in C#  to make LINQ work  undermines one of the key strengths of statically typed languages it seems to me.   You can have, say, var x equals some random expression and no longer know they type of variable x.

One of the key features of Java is that it is easy to read and maintain – which is why so many enterprises bet their business on it.  And we need to stick to that.  So I think Java still has the edge and being relatively steady and cautious is likely to continue to win the race.  Where more "dynamic" language features are required you can mix and match with JRuby, Groovy, Jython or whatever.

So I think .NET is great news for Java – we get to learn from it, improve on it and (hopefully) avoid some of its mistakes.  ARM blocks is a nice example for Java 7 of taking a C# concept and doing it better.

Patrick Forhan replied on Wed, 2008/01/30 - 1:55pm

I'm completely uninformed with respect to LINQ, but JavaFX Script has an incredible (and incredibly fun) SQL-like syntax (slightly out-of-date docs here) for dealing with JFX arrays:.

var names:String* = ["Bob", "John", "Bill", "Jenny"];
var prefixedNames:String* = select "Name is {name}\n" from name in names where indexof name > 1;
println(prefixedNames);

results in:

Name is Bill
Name is Jenny

It is kind of nice, and if it makes it into the final version of JFX, that would be cool. If it supported Collections API, then it would be even cooler.

Eric Bresie replied on Wed, 2008/01/30 - 3:36pm

I'm not well versed in LINQ, so maybe someone can answer me this.  Would some existing JDBC, DAO, XML binding/querying, or POJO capabilities fill the voids currently provided by LINQ?

Scott Fleckenstein replied on Wed, 2008/01/30 - 4:20pm

Arguing that Java's relatively slower ability to advance the language design is because Microsoft can break backwards compatibility is being facetious.  One of the reasons that .Net was even created was to improve the backwards compatibility of a windows system while maintaining the ability to quickly improve code.  This goal gave birth to tech like side-by-side assemblies.
Additionally, you should know that the addition of LINQ support was done entirely as compiler magic...  it runs fine on the .Net 2.0 CLR.  There's nothing preventing the Java world from implementing a set of features like LINQ other than bureaucracy;  it's additive, you don't have to break anything existing at the moment to provide it.

Mark N replied on Wed, 2008/01/30 - 9:14pm

(D)LINQ is great ... if you only want to use SQL Server. (I know, it is no longer DLINQ).

Mark N replied on Wed, 2008/01/30 - 9:15pm in response to: ebresie

No.

dolphin us replied on Wed, 2008/01/30 - 11:37pm in response to: mknutty

Fortunately that is not an inherent limitation of the language/runtime. You can freely create your own LINQ providers; someone even created LINQ to Amazon:

var query =
  from book in new Amazon.BookSearch()
  where
    book.Title.Contains("ajax") &&
    (book.Publisher == "Manning") &&
    (book.Price <= 25) &&
    (book.Condition == BookCondition.New)
  select book;

And to expand upon nullstyle's comment, even at the language level there are very few breaking changes from C# 2.0 to C# 3.0 (I don't recall exactly; there might even be none). New keywords in C# 3.0 such as 'from' and 'where' are contextual: they are only keywords in certain cases that are all illegal in C# 2.0. So existing code such as 'int from' will port perfectly fine.

As for the inefficiency of LINQ, Rico Mariani has a series of blog posts that suggest a perf impact of 10% or so for his tests. Obviously these will differ in the real world, and I'm sure there are situations where LINQ is too inefficient. But also remember that there are a ton of other situations where performance isn't so critical, and the ease of LINQ (with Intellisense and all) is a significant step forward.

Sean Garrett replied on Thu, 2008/01/31 - 2:43am

Hi 

Having been a Java developer for many years and now a .Net Developer for 3 the one thing Java has over .Net is a well defined Enterprise Architecture. I'm still trying to figure out what the recommended Enterprise Architecture is for .Net. 

LINQ is just a cool framework feature, not somethign that would govern the choice between Java and .Net as to which to use to implement an Enterpise System in.

When I ask an MS .net trainer what the .net Enterpise Architecture was the response was '.Net is the enterpise Architecture' which from my persepective is similar to saying 'The JDK is the Enterpise Architecture'.

Besides I thought embedding SQL in source code was bad? ;) 

--
You're just jealous that the voices talk to me.

Simon Buettner replied on Thu, 2008/01/31 - 6:57am

Dont forget the great hibernate criteria builder from grails.

A snippet from the grails reference:

def book = Book.withCriteria(uniqueResult:true) {
def now = new Date()
between('releaseDate', now-7, now)
like('title', '%Groovy%')
}

 

Mike Brevoort replied on Thu, 2008/01/31 - 10:02am

LINQ isn't going to all of a sudden convince me to pay tens of thousands or dollars in licensing costs.

Sebu Koleth replied on Thu, 2008/01/31 - 6:00pm

>From a language feature perspective, C# and LINQ are evolving faster and adding more important features than Java.

Just because it is new, it need not be good and it need not be as important to each and every project. As people start using LINQ, the kinks will start showing up. The downside is that people who will work in depth on LINQ will not be posting to Java groups complaining about the issues they face.  So Java developer's comments will be from an information vaccuum w.r.t .NET/LINQ

Java has generated a healthy, intellectually advanced community with an emphasis on openness and collaboration even while competing. .NET cannot even come close to it. All the MS claims at .NET inception that it will/can run on any platform equally well and will provide an enterprise platform have not been realized. .NET components meant for enterprise collaboration still adher to MS standards rather than globally accepted standards.

It is not addition of new features that attracts people to a language. Some of the important attracting points are : how easy is it to use the language to create an elegant solution to a problem? how easy will it be to maintain/troubleshoot/enhance the solution? how easy will it be to use this solution on multiple platforms? 

Michael Galpin replied on Fri, 2008/02/01 - 10:25am

I went to a MSFT talk last year on LINQ. It was given by evangelist Anand Iyer, who has done numerous developer talks in the Bay Area. LINQ looks ok in a vacuum, but it quickly looks like a mess inside a C# class. Anand kept having to  play with the editor and the indenting just to make it possible to read the code and grok it. This was a relatively simple example of embedding LINQ within a C# class, but you quickly could see the problems involved in essentially allowing a secondary syntax (LINQ) within a primary (C#) syntax. I could not imagine what it would be like to see a complex use of LINQ in  a real class and be charged with maintaining it.

Uffe Seerup replied on Sun, 2008/02/10 - 11:23pm in response to: chhum

FYI, LINQ does handle groupings and even complex query building naturally. There is both a SQL-like syntax and a methods-based syntax. The first is actually just a shorthand for the latter:

var q = from c in db.Customers where c.City=="London" group c by c.ZipCode

is equivalent to

var q = db.Customers.Where(c => c.City=="London").GroupBy(c => c.ZipCode)

Both will return a list of "groups" each with a Key property holding the zipcode. The individual customers for each zipcode can be accessed by enumerating over the group (it's an enumeration of "Customers"). I don't see any complex workarounds. Also (when performed against the database) it will retrieve all of the customers from London at once (building a query with a where clause) and perform the grouping on this set. Can you explain how this could possibly perform better?

You also misunderstand type inference. C# is still very much a statically typed language. When the compiler encounters var s = "Hello World"; it simply infers that the type of s is string just as if you had written string s = "hello World";

 

Uffe Seerup replied on Mon, 2008/02/11 - 1:03am in response to: ebresie

ebresie wrote:

I'm not well versed in LINQ, so maybe someone can answer me this.  Would some existing JDBC, DAO, XML binding/querying, or POJO capabilities fill the voids currently provided by LINQ?

.NET already has an JDBC equivalent, called ADO.NET. LINQ is actually a core language feature so its perspective is much broader than simply database access. LINQ can be used to select from any object which implements the IEnumerable interface; which includes arrays, all collection types such as lists, dictionaries etc, datasets/recordsets etc.

LINQ is often mistaken for a (sub)library called LINQ to SQL; which is for accessing SQL Server databases (projects are underway to implement LINQ to MySQL/PostgreSQL/Oracle). When used this way LINQ leverages a new feature called expression trees. When a method expects an expression tree as parameter the compiler will pass an concrete syntax tree instead of the result of the expression. This allows the method to inspect and even build new expression trees. This is the way that native C# expressions are translated into actual SQL syntax.

LINQ to SQL is also a lightweight object relational mapper. It lacks many of the features of more mature ORMs (such as Hibernate) but it does allow for some really simple query syntax.

Using the same syntax LINQ can be used to select from (and even build) XML documents/fragments. The "conditions" can then include references to members such as "Descendants", "Ancestors", "NodesAfterSelf" etc.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.