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 ?
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".
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.
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:
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.
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.
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
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
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)
}
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 ] }
}