views:

1109

answers:

9

I have recently come across the languages Groovy and Scala which are built on the JVM. But I dont know much beyond that. Are those languages going to overtake Java at some point? Do these languages serve for any special purpose? Which of them is faster and more powerful? For what type of applications should I choose Groovy/Scala? Will it be helpful for me if I study Groovy/Scala now ?

+5  A: 

Groovy is an untyped[1] language (int i = "hello" is valid Groovy[2]), Scala is typed, and has type inference.

Groovy looks like Java, Scala doesn't, though it's not a world away.

Groovy runs more slowly than Scala.

Groovy is influenced by Java and Ruby, Scala is influenced by Java, Haskell and ML.

Neither are going to overtake Java. Both are general purpose. The constructs in Scala are likely to appear in other languages and so it is worth studying (for-comprehensions later appeared in C# as LINQ, for example). The only construct that's in Groovy but not in Java that is likely to appear in other languages is closures, which are already in pretty much every language other than Java. Arguably null-safe operators too, as those are already in C#.

If choosing a language to study I'd go for Scheme, Haskell or Python. Scala comes close to those but is more complex. Groovy is not worth studying; there's little of educational value to be found.

[1] I have rolled this edit back because I did actually mean untyped. Groovy is an encoding of the untyped lambda calculus, not the typed lambda calculus, with the exception of implementations of Java interfaces. Checking of values at runtime such as instanceof or .getClass().equals are not type-checking, but value checking. TAPL[3] calls such 'types' tags rather than types, and I rather agree.

[2] I have rolled this edit back because int i = "hello" is valid Groovy. It compiles, though yes, it fails at runtime. Similarly, lots of valid Java programs fail at runtime, but the efforts made to filter those out are minimal in Groovy.

[3] Types and Programming Languages, Benjamin C. Pierce.

I'm not sure the edits made follow the stackoverflow rule "always respect the original author".

Ricky Clarkson
`int x = 'hello'` is NOT valid Groovy...
tim_yates
Not valid in the sense that it will fail at runtime? Then this is not valid Java: `throw new Error("Oops");`
Apocalisp
No, not valid in that you can't put a String into a declared `int` variable. You can do: `def x = 'hello' ; x = 1`, but not if you declare `x` as an `int`
tim_yates
This shows how people comment about a thing even if they don't know it. Ebbu do your own research to find out what is best for you, don't trust solely on biased opinions.
Felipe Cypriano
Because it will fail at runtime, just like `x = null; x.foo;` in Java, or `public void f() { f(); }`.
Apocalisp
It's syntactically valid but not semantically valid, in a way that cannot be established by Groovy until runtime? Happy? Programmers in dynamic languages are comfortable with the fact that most bugs don't manifest until runtime. Strong unit tests are the solution.
slim
"Strong unit tests" in the form that users of dynamically typed programming languages use them are a capitulation to languages that leave programmers without the tools to write correct code; tools that language technology *is* currently capable of supplying. Remember, testing doesn't prove programs correct while static type checking does prove the absence of whole classes of common errors.
Randall Schulz
My angle is that you need good unit tests whether the language is statically typed or not. Given that you need the tests anyway, some of the risk of not having compile time type checking is mitigated.Static type checking does indeed deal with whole classes of common errors, but it also prevents whole classes of very useful and convenient solutions to common problems. Testing beans intended for DI, for example, involves much less boilerplate code using Groovy's duck typing.
slim
And Scala has a statically type-safe equivalent to duck typing.
Randall Schulz
Can Scala replace Groovy's ludicrously useful Expando class? Not denigrating Scala -- Scala is good. Just not writing off dynamically typed languages either.
slim
Scala has type-safe implicits, I guess they will do what you want.
soc
+9  A: 

Are those languages going to overtake Java at some point?

Overtake it in terms of what? Popularity? Nobody knows that. If you mean in terms of expressiveness or modularity, then they already have.

Do these languages serve for any special purpose?

Both are general-purpose programming languages.

Which of them is faster and more powerful?

I don't know much about Groovy, but Scala is exactly as fast as Java. I'm told that Groovy is slow due to its untyped nature. Slower than Java by a constant factor of about 10 to 50. However, adding invokedynamic to the JVM might improve things on that front.

For what type of applications should I choose Groovy/Scala?

I would not use Groovy for any application whatsoever. If I wanted an untyped language on the JVM, I would go for Clojure. I use Scala for day-to-day development, and you can write anything you would ordinarily write in Java. It is completely compatible with Java libraries, so your code can look very similar (although terser).

Scala is particuarly well suited for writing combinator libraries and software that uses them, since its type system allows the kinds of higher-order abstractions that this requires.

Will it be helpful for me if I study Groovy/Scala now?

Helpful to what end? I'm not convinced that Groovy will help you get anywhere, but Scala definitely makes me more productive than Java.

If you really want to have your mind expanded, and become a more proficient programmer with any language, then pick up Haskell.

Apocalisp
Groovy is not slower than Java by a constant factor and certainly not by 10 or 50 times. It depends on what you are doing but in my tests it is on par to a factor of 2 or 3 at most. Also, groovy is not 'untyped', it is dynamically typed. Clojure is functional and not a replacement for Groovy. If you don't know much about groovy, let those who do answer...
Chris Dail
Last time I checked clojure didn't have a web framework with the active usage and support that Grails does so if you want to write web applications Groovy is a better choice then Clojure if you want a development style similar to Rails.
Jared
What's with all the Groovy hating? Any evidence to back up your remarks?
Don
To be completely pedantic, there's really no such thing as "dynamically typed" in type-theory. It's a CS marketing phrase. The proper name for languages like Groovy and Clojure is "untyped". Sadly, this is a linguistic battle that has already been lost.
Dave Griffith
Evidence: http://www.codecommit.com/blog/java/groovys-performance-is-not-subjective(Granted, a bit out of date.)
Rex Kerr
A "dynamically typed" language makes no distinction between type errors and nontermination. Ergo, it is untyped.
Apocalisp
If Groovy is "untyped", how come "instanceof" works and object.getClass() gives meaningful results? There are types. They are assigned at runtime.
slim
What then is the difference between a type and some other property of the object? The answer is none, because those are not types. A program is a proof, and the hypothesis that it proves is its type.
Apocalisp
+3  A: 

I have recently come across the languages Groovy and Scala which are built on the JVM. But I dont know much beyond that. Are those languages going to overtake Java at some point?

I dont think so. They go hand in hand with Java. It would be absurd to say one would "overtake" Java and "take over the world".

Do these languages serve for any special purpose? Which of them is faster and more powerful? For what type of applications should I choose Groovy/Scala? Will it be helpful for me if I study Groovy/Scala now ?

Scala is statically typed,object-oriented and compiles down to the same bytecode as Java.
Groovy uses Java syntax, it supports weak typing, where variables donot have to be defined before the first use and no type declarations should be made. Groovy also compiles directly to Java bytecode. So IMO Scala and Groovy complement Java, not completely replace it.Both Groovy and Scala have many extensions that could ease day to day development tasks, extensions to work with servlets, SQL, XML etc.

Coming to Scala against Groovy, to quote James Strachan (creator of Groovy):

Though my tip though for the long term replacement of javac is Scala. I'm very impressed with it! I can honestly say if someone had shown me the Programming in Scala book by by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I'd probably have never created Groovy.

so I think Scala fares better than Groovy and is definitely worth a look if you're planning to learn.
Source: http://macstrac.blogspot.com/2009/04/scala-as-long-term-replacement-for.html

Elsewhere on SO:

Zaki
Enough with the James Strachan quote. If you want to use it then set the whole context. Yes, both James and Bod are the founding fathers of Groovy and fostered in the early years, however they are no longer associated with the language nor the project; James left almost 5 years ago. He is now happy developing other projects, some of them use Scala as the main language.
aalmiray
I dispute "no type declarations should be made". Type declarations in Groovy are optional, but it's often good style to do so anyway, and IDEs can use the information. Variables do usually need to be defined, either with a type or with the `def` keyword, so scope is clear and typos are caught.
slim
+5  A: 

I can't tell you much about Scala, but I can tell you that both Groovy and Scala have strengths. A dynamically typed language like Groovy can be a very productive environment to work in - it leads to quite a Ruby-ish feel within the Java world, and that's exactly why something like Grails is possible.

Then again, other times strong typing is exactly what you want. It's horses for courses.

I would warn against learning Groovy instead of Java. Groovy is a very productive language, but when you hit trouble, it's usually a strong knowledge of Java that will get you out. Groovy is a great tool to make Java programmers more productive; it is not an easy language that can save you from having to know Java.

slim
I agree. Groovy is a powerful addition to Java, but knowledge of what happens in the background will eventually prove useful.
miek
+10  A: 

What I've come to love most about Groovy are the small things that make everyday operations fun and easy (compared to Java). While you could easily cover a book chapter with all these details, my personal favorites are probably the easiness of handling collections, reduced need for boilerplate code (I/O operations, text handling, etc.), and all the general straightforwardness achieved by metaprogramming and the extensions made to the JDK (hence "Groovy JDK").

For me, Groovy has largely replaced the need to use Perl or Python for scripting purposes. The Groovy version of the Perl Cookbook is a great reference for elegant solutions to common problems.

miek
Groovy's expressiveness and versatility has also made it possible for great development tools such as Spock (http://www.spockframework.org/) to exist.
miek
A: 

Groovy and Scala are scripting languages. They compile to byte code that runs on the JVM. You would typically use them for other purposes than you would Java.

The big advantage is speed, brevity and readability. For instance here is some java code which implements a bag:

public class Bag {

   private Map<Object, Integer> counter = new HashMap<Object, Integer>();

   public void add(Object obj) {
      add(obj, 1);
   }

   public void add(Object obj, Integer count) {
      Integer value = counter.get(obj);
      if (value == null)
         counter.put(obj, count);
      else
         counter.put(obj, value+count);
   }

   public Integer getCount(Object obj) {
      return counter.containsKey(obj) ? counter.get(obj) : 0;
   }
}

And here is the same implementation in Groovy:

class Bag {
  def counter = [:]
  def add(term, count=1) {
    if (counter[term] == null)
        counter[term] = count
    else
        counter[term]+=count
  }
  def getCount(term) {
    return counter[term] != null ? counter[term] : 0;
  }
}

So you can see how intuitive the Groovy code is, you don't need to define anything regarding types or default parameter values.

That said, Groovy is essentially Java, just wrapped around a different compiler, class loader and with many added methods and members. For that reason it is unlikely that it will ever replace Java, not only that but because it IS Java it is bound by the same limitations and rules and doesn't provide any functionality which is really unique.

Java can always be made faster, more memory efficient and more robust than Groovy if you are willing to put in the time and effort.

So where would you use Groovy? I think typically in places where readability, flexibility and speed of development is more important than performance, extensibility and a large user base. For instance in deployment you can use the Groovy equivalent of Ant called Gant which allows better flexibility and better readability. Other places might be in a web application framework such as Grails.

Scala is a different story, but I've gone on long enough, I'll let someone else answer that.

Hope you find this helpful... BTW, if you are looking at JVM languages I would also check out Clojure which is an interesting language from the functional programming realm

Asaf
You're wrong about Scala; it is not (only) a scripting language, and you could use it (and many people are) for most projects where you could use Java.
Jesper
Jesper, you are correct. I didn't want to get into the Scala language as it is a completely different thing. While you can do anything in Scala that you would in Java I would wager it will not "replace" or "overtake" it anytime soon. Scala's strong points I think are for building frameworks such as Lift or for functional programming where Java especially sucks.Still, I think if you are going to write client-server applications in the next 5 years your best bet is to learn Java and not Groovy or Scala. This is just my personal point of view though...
Asaf
I'm reasonably proficient in both Java and Scala, and I would definitely (and have) use Scala to build a client-server application. Mainly because I'm a lot more productive in Scala.
Magnus
+1  A: 

Hold on there. So you think Groovy or Scala is going to overtake Java? Are you a new developer or engineer?

As a Java Champion, Java developer for now twelve years or more, I think Java will still be lingua franca on the JVM. However, there are many languages that compile to byte codes in order that they can run on the Java Virtual Machine like Scala and Groovy.

Groovy can be seen dynamic typed. In that you programming with runtime types.

def s = "A fool"

s = [ 1, 2, 3 ]

s = 3.141596527

s = "A fool that follows them?"

In Java and Scala, the above does not compile statically to byte code by the language design. In other the static type must be defined!

var s: String = "A scala string" s = 12.3456 // Scala compiler error

The future is definitely Java + X, and guess what? You get to define whatever you think X is going to be.

There are lots of directions to consider X, among the following:

  • A Static or Dynamic language
  • Domain problem space that you want to get involved in (e.g. JavaFX for rich user interfaces)
  • Stay within the JVM or perhaps not (how about straight Python or Ruby or C# )
  • Functional language or object oriented language or Hybrid
  • Environment and Infrastructure (do you need to deploy to Tomcat or WebLogic or the cloud)
  • Do you need Eco system around your language Open Source available frameworks

Then there are non-technological reasons like industry, sector, geographic, and of course money

These factors are going to seriously define your X *factor* in this decade!

HTH

peter_pilgrim
+6  A: 

Just for completeness, and answering Asaf, here's the bag implementation in Scala.

class Bag {
  private val counter = new HashMap[Any, Int] {
    override def default(key: Any) = 0
  }
  def add(obj: Any, count: Int = 1) = counter(obj) += count
  def getCount(obj: Any) = counter(obj)
}
Martin Odersky
+1  A: 

And a different (better?) implementation of Bag in Groovy than the one given by Asaf would be:

class Bag {
  def counter = [:].withDefault { 0 }
  def add( term, count = 1 ) { counter[ term ] += count }
  def getCount( term )       { counter[ term ] }
}
tim_yates