Why can we write Groovy code in Java syntax? [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm just new at Groovy and a few years experience on Java. I'm following a simple tutorial said 95% of Groovy syntax are same as Java. So I am wondering is that a good practice to write your Groovy code as Java? And why not?
thanks

You can write Groovy code in Java syntax to make it easy for Java developers to move to Groovy: as this language feature allows them to start by writing code as they know how to, or they can copy/paste existing Java code into a groovy script.
In other words: the groovy language was designed on purpose to allow for this, to help attracting users.
Looking at pros and cons:
The big advantage of writing java-like groovy code is simple: in case you get unhappy with groovy, it is easier for you to move back. And it allows your team members/coworker who haven't learned groovy to understand your work.
The downside: the whole point of groovy is to give you some features that Java is lacking, so to an experienced groovy programmer, "too much java" style might trigger the question: "why using groovy when you write pure Java all the time?"
Beyond that, keep in mind that groovy is really more of a niche language, that never gained a lot of attention (outside the gradle build eco system).
From that point of view, my personal two cents: don't write groovy code in the first place. Even gradle can be used with kotlin these days. Unless you get paid for doing so, rather spend your time and energy with other languages, e.g. kotlin.

"Is that a good practice to write your Groovy code as Java?"
You mean using a Groovy compiler to compile your Java code? Why would you do that?
Although Groovy supports Java syntax, it's mostly intended for simplifying and compacting your code, providing Groovy programming idioms and syntactic sugar. If you're just a beginner, it's ok to combine Groovy and Java code while you're learning Groovy syntax. But ultimately you should use Groovy syntax only.
So the answer to your question: no, it's not a good practise to write Groovy code as Java.

Related

Is mixing Java and Kotlin a bad idea? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last year.
Improve this question
I've been trying to learn Kotlin but I find it to be a lot easier to learn when I can slowly apply it to things I'm working on in Java. Is it a bad idea to mix Java and Kotlin in production?
I know the overall goal is to use one or the other but is there anything wrong with the latter?
Objectively, we can say that compatibility with Java was one of Kotlin's main objectives, according to the official docs:
Kotlin is 100% interoperable with the Java programming language and major emphasis has been placed on making sure that your existing codebase can interact properly with Kotlin. You can easily call Kotlin code from Java and Java code from Kotlin. This makes adoption much easier and lower-risk. There’s also an automated Java-to-Kotlin converter built into the IDE that simplifies migration of existing code.
Many features are designed specifically to ease calling Java from Kotlin or vice versa: for example, the fact that property accessors are implemented in the same way as normal Java accessor methods, and the ability to implement SAM interfaces; and where Kotlin implements things differently, there are often annotations or other ways to use a Java-style implementation.
And in my own experience, there are no issues with mixing Java and Kotlin classes. Many of our projects have both (new classes written in Kotlin, a few old ones converted but many still in Java), and I'm not aware of any significant problems. I also converted a major project from Java to Kotlin, one class at a time — and after each one, everything still compiled and tested and ran perfectly.
Of course, new projects can be written in Kotlin from the start; but if you already know Java, mixing in some Kotlin is absolutely an option, and may be the easiest approach. You can convert parts to Kotlin piecemeal, when convenient, or leave them in Java long-term; pretty much everything just works, and you benefit from the parts that are in Kotlin. (You might even find that the way you write Java code benefits from knowing Kotlin!)

scala vs java for Spark? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Can someone help me understand why people is using scala over Java for spark? I have been researching but haven't been able to find a solid answer, I know both works fine as they both run on JVM and I know scala us functional and OOP language.
Thanks
Spark was written in Scala. Spark also came out before Java 8 was available which made functional programming more cumbersome. Also, Scala is closer to Python while still running in a JVM. Data Scientists were the original target users for Spark. Data Scientists would traditionally have more of a background in Python, so Scala make more sense for them to use then go straight to Java
Here is direct quote from one of the guys who wrote initially wrote spark from a reddit AMA they did. The question was:
Q:
How important was it to create Spark in Scala? Would it have been feasible / realistic to write it in Java or was Scala fundamental to Spark?
A from Matei Zahara:
At the time we started, I really wanted a PL that supports a language-integrated interface (where people write functions inline, etc), because I thought that was the way people would want to program these applications after seeing research systems that had it (specifically Microsoft's DryadLINQ). However, I also wanted to be on the JVM in order to easily interact with the Hadoop filesystem and data formats for that. Scala was the only somewhat popular JVM language then that offered this kind of functional syntax and was also statically typed (letting us have some control over performance), so we chose that. Today there might be an argument to make the first version of the API in Java with Java 8, but we also benefitted from other aspects of Scala in Spark, like type inference, pattern matching, actor libraries, etc.
Edit
Heres the link incase folks were interested in more on what Matei had to say:
https://www.reddit.com/r/IAmA/comments/31bkue/im_matei_zaharia_creator_of_spark_and_cto_at/

Scripting language to compiled language or pure compiled language, which is faster? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Recently I have coded a application in jruby and made .jar file from it and deployed same in tomcat. I have also read an article on jruby which says "Jruby- scalablity of java and easiness of ruby".
Here I want to know does coding like this in jruby, give same scalability and performance like directly coding in java?
Just like this using ruby extension to run c code in ruby gives same performance as directly coding in c.
Thanks
(Disclaimer: this might not be a complete answer but it was too long for a comment)
Although I have next to no experience with JRuby's implementation in particular, dynamic languages implemented on the JVM have their limitations in terms of the speed you can get compared to what you would get if you coded directly in Java.
From what I understand, this comes from the trade-offs client language implementations (JRuby, Clojure, Jython, etc.) have to make, in order to "simulate" their inner workings on the host platform (in this case the JVM). Maybe in the most common scenarios the performance difference is not that bad since the JVM's HotSpot optimizations kick in, but when it comes down to getting Java-like code performance, you might need to dive into the details of the language implementation in order to bypass some of its limitations.
In the case of the Ruby extensions in C, what you are actually executing is native code (machine language) that was originally coded in C, but which you can call from your Ruby code. When running JRuby code, the language you are programming in is Ruby, which has to be compiled to Java bytecode, which runs on the JVM, which translates that into operations in the native operating system. Something analog to Ruby/C would be in this case JRuby/Java, that would be calling some Java code, that was originally written in Java from your JRuby program.
EDIT
This discussion mentions some of the points I have included above and a more detailed explanation on some other interesting points.

Why should a Java developer learn an additional JVM language? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am a java developer and I want to know,
what is the main benefit from learning a language such as Scala or Groovy?
You can get the same benefit from learning another JVM language as learning any new language. It increases your understanding of programming in general and more importantly, it adds another tool to your toolbox.
So the next time you have to solve a problem, you may reach for a nail gun instead of a hammer.
To be more specific, Groovy is a good language for mocking up code quickly, and Scala, while I've never used it, is suppose to great for writing concurrent applications due to it's functional approach. As others have mentioned, the JVM languages can interact with Java code. Which can be useful for adding onto legacy systems or for mocking up pieces of a application quickly.
Some good reasons that come to mind:
They have features that Java doesn't have and that you may find useful in certain circumstances.
They use different programming paradigms, different way of thinking.
Learning other languages opens your mind.
Learning languages makes you conscious about their respective strengths and weaknesses.
They are getting more and more attention and better JVM support (with Java 7).
Actually, maybe ask yourself the reverse question:
Why not learn an addiitional JVM language?
The other answers here have very good points, but there's one thing I'm missing.
A good coder rarely identifies himself as a 'Java developer', a 'Python developer' or any ' developer'. Learning another language (be it a JVM language or not) will make you understand there's a lot more in the world to learn.
If you're satisfied with only one language, it usually means you're oblivious to the problems it has, and that there are many tasks that are better suited for other languages.
This is why the Pragmatic Programmers encourage every programmer to learn a language a year.
The languages you've mentioned practice a different programming paradigm that could help you be more productive. They are also more fun to work with.
Languages such as Scala and Clojure run on the JVM and exhibit great performance in multi-core systems without imposing synchronization requirements.
And of course you are still able to use the full wealth of libraries that are available for Java.
Because then you'll know a new language, which means a broader skill set and another way of looking at problems. But because Groovy and Scala run on the JVM and you know Java, you can integrate existing libraries and code if you want or need to.
from http://groovy.codehaus.org/:
"Groovy is like a super version of Java. It can leverage Java's enterprise capabilities but also has cool productivity features like closures, builders and dynamic typing. If you are a developer, tester or script guru, you have to love Groovy."
So in many cases it does make sense to use Groovy over Java; For instance in Java unit tests!
Scripting language advantages inside jvm. Seamless interaction with compiled java code.
I know with Groovy, you can load scripts (from files) at runtime from your Java application. This allows me to customize the behavior of application actions at a client site without requiring me to recompile code. It's rather lovely.
Sorry more questions then answers..
What would you spend your time on if you are not learning a new language?
Why are you limiting yourself to JVM languages?
Would it be of more benefit to learn test driven development?
What about learning standard design patents?
What are you trying to achieve with your investment of time?
Learning a language is always good, however if you don’t learn them very well and use them you don’t get a great benefit, as you need to be able to “think in a language” so as to broaden your mind.
Using more than one language in a company leads to lots of additional long term costs so you may gain more by learning how to programme better in your main language, only you can decide.

Is there a way to transfer/translate the code written in Java to other languages? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Theoretically this seems possible to me. So can Any one confirm this to me, if it's possible? and if there is such a software that does this?(like Java to C++ or C#)
And in general would it be possible to transfer languages like Java to server-side programing language like PHP?
Translating the syntactical elements of one language and producing another is not trivial but it's not impossible. A good parser can build syntax trees in one language and then emit another. The difficulty of porting code outside the context of simple "Hello World" type applications is twofold:
The libraries of one language will probably differ (e.g. WinForms vs Swing)
Some language features will have to be catered for: (lambda expressions, anonymous methods, different inheritance implementations etc).
It is possible, but the major problem is that Java has a very large runtime library which needs to be made available in the target language in order to be able to do a fully automatic conversion.
For the special case of Java -> .NET, you can use J# from Microsoft to compile it into a .NET assembly which can then be used. Also ikvm.net allows for running a JVM inside .NET.
For PHP I do not believe such a solution exist. You MAY be able to use gcj to create a native library which can be linked in, but I do not believe it is a feasible soultion.
What functionality do you need in PHP?
Visual Studio ships with a Java to C# translator, and even tough it does a pretty decent job, there's still a lot to clean up afterwards.
In my experience you really have to ask yourself if it makes sense to translate code from one language to another. What is the gain? Will the translated code be maintainable? If the answers to these questions point in the wrong direction, translating is probably not the right approach.
Google Web Toolkit does conversion from Java to JavaScript:
http://code.google.com/webtoolkit/overview.html
to answer your question, yup, theoretically this is indeed possible and practically such technology is used every day :)
The interesting thing, in my opinion, is that the Java converters typically convert by taking the bytecode, not the source code. Then it's, say, bytecode-to-ObjectiveC source code. For some converters (at least one opensource one) it's bytecode-to-XML then XML-to-target-language.
For example, the Uniwar application for the iPhone, which has been acclaimed by all and made its way to the appStore's top ten, as been written in Java (JME) and automatically converted from the Java bytecode. Reaching the top ten, even for a few days, means that this is deployed on a lot of machines ;)
In the Real-World [TM], Cobol-Java and, weirdly, Java-Cobol are not unheard of.
For all this to work that said you need a really good converter :)
Theoretically it is possible. But as others pointed out the main problem is to translate libraries.
Some time ago I made Java to Tcl(XOTcl) and Java to Python translators to evaluate the translation posibility. Search by java2tcl and yava2python.
They convert syntax but do not make relevant constructions translations (e.g. Java file operations to Python ones). That would require more development time.
In general my opinion is what such a translation may be possible. But only if your translator covers classes/libraries of the converted project.

Categories

Resources