开发者

Learning Java so I can get at clojure [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 have a history of hating Java, having used it pretty regularly in the late 90's during the 'slow as balls' era. As such, I never really learned it well. From what I understand, Java is actually a pretty good language to use these days. I've been thinking about diving into it because of Jython and Clojure. That is to say, I'd like to program in Java and use inline Jython or Clojure where appropriate. But truthfully, I'll probably just be programming in Jython/jRuby and calling up clojure from there.

Which brings me to my question. I know both of these languages can be called from Java, but is that necessarily good practice? Should I even bother learning java if I just want to 开发者_C百科use Jython as the primary language? Seeing as how that's a large part of my motivations here, I'd like to know that I'm not terribly misguided before jumping in. I'm aware there is a very high risk for projects to become a kludge if done in multiple languages like this.

I'm still learning about the JVM and the like, so I apologize if this question is painfully obvious.


Jython can be viewed as a cross compiler from Python to the Java Virtual Machine. As such, to get the most out of Jython you'll obviously have to learn Python, and probably will need to learn Java.

You can skip some of the Java learning, but at the end of the day, Java and the JVM grew up together. That means that Java code tends to lend understanding of the JVM. It is possible to gain understanding of the JVM without Java, but that's not a path well travelled. Any Jython code that imports a Java library will immediately have you searching Java documentation, so if you avoid leaning Java you're going to learn it piecemeal anyway.

You will have to decide if a piecemeal approach or a formal approach is more appropriate for you and your situation. A lot of deciding which path to take is knowing how you learn best.

As far as the "slow as balls" period of the 90's, that's when I was learning Java. Personally, I feel it is better to describe it as "slow as balls if you did incredibly stupid things with Java". Now I think people have built up a sufficient skill set to avoid translating C directly into Java. That said, I do occasionally encounter the 2000+ line method, so perhaps I'm being a bit rosy in my projection. The entire JVM is laid out in such a manner that good object oriented code runs faster, and if you're constantly trying to go to "other" objects for all the data you need locally, you'll just stack thrash the JVM.

Regardless of opinions, the JVM is now the hot Java item. There has been "other language" support by one means or the other for over a decade now; however, the excitement around Domain Specific Languages seems to have sparked an interest in compilation technologies and the JVM. The other languages benefit from the JVM being an easy target to hit with built-in cross platform support, excellent performance, huge availability of libraries, and generally good documentation. Learning Java and the JVM will help you with a lot of the JVM supported languages, as many of them don't flesh out their library space in favour of hooking into a pure Java library.


I'd say it's worth knowing Java even if you plan on only using other JVM languages. I use JRuby and Scala, and have played around with Clojure. If you are building things to run on the JVM, knowing Java is a bit like knowing C when working natively–you don't have to know C, but if you do, you can write the bits that need speed in C and wrap them in a Ruby or Python library or whatnot.

It's worth knowing the basic principles of how Java works in terms of things like interfaces and annotations and how the classpath works because otherwise you are working with basically a leaky abstraction. What happens when your interop isn't very good? This is especially true if you are planning to do Clojure and Jython!

The other reason to know Java is simply because if you are using code in the Java ecosystem, you have to be able to read and write Java. You need to write a library? Yes, you can probably write it in Clojure, but if you want other JVM language users to be able to use it, you should probably have written it in good, idiomatic Java. Scala is close enough to Java for this purpose; Clojure or Ruby or Python, not so much. Just being able to read and comprehend Java programs is very important too.

The other great benefit is simply that you get more libraries and they are better tested. You need a double-ended queue? Check the Java Collections Framework. Good random number generation? java.security.SecureRandom. UIs? Well, Swing, AWT and SWT are... okay, bad example. Knowing the benefits and shortcomings of these only comes from doing some Java programming and learning the various ways not to suck at Java.


From a couple of years experience of using Clojure (plus many more years of Java...) here is my perspective:

  • You don't strictly need any Java experience to write Clojure code - Clojure is a full language in its own right and you can write perfectly capable programs without using any Java.

  • You will need to set up the JVM environment - the Java environment has some rules about where code gets loaded from (i.e. the "classpath") that need to be followed to get a working environment. Not a big deal, and most IDEs will do it for you, but it can be a hurdle for people completely new to the JVM world. I'd suggest careful following of the setup instructions for whichever IDE/toolset you choose.

  • There are some Java-related concepts that are helpful to understand - for example, Clojure harnesses Java exception handling features with (try ... (catch ...)) etc. so it's useful to be somewhat familiar with the Java approach to exception handling.

  • Ultimately you will probably want to use Java APIs - bacause a huge amount of the value of being on the JVM in the first place is in having access to the huge diversity of libraries and tools that are available in the Java ecosystem. You don't need to write any Java code to use Java APIs from Clojure, but you do need to know enough Java (method signatures, data types etc.) to be able to read the JavaDoc documentation of the APIs and convert this into an appropriate Clojure function call. Often, this is as simple as (.someJavaMethod someJavaObject param1 param2) but sometimes it can be more complex (e.g. when you need to instantiate a subclass of some Java class to pass as a parameter)

  • Java isn't a bad language to learn anyway - while I'll readily admit Java has some weak points (as do all languages!), it's still a great, simple, high performance, cross-platform, object-oriented language that has a lot of value. Even if you only do a few short tutorials and never write anything substantial in Java, I'd still recommend it for the learning experience.

I believe most of the above would also apply to Jython.


I can't speak for Jython, but if you want to really get to grips with clojure, you want to understand its trade-offs compared to Java, especially wrt memory/gc and the basics of Clojure/Java interop. You also need at least an abstract understanding of how the clojure collections are implemented unless you really don't care about performance - that's not to say that clojure is particularly inefficient, but more the opposite: the implementation of its immutable collections is fairly unique and tailored to clojure's stance on persistence and performance and it helps to understand the underlying details when you're trying to improve on performance issues.

For all of that, I don't think you actually need a lot of Java knowledge. Being able to read Java fairly well, a basic understanding of the concepts, and a knowledge of where to find the documentation is probably enough.

I think if you want to do a hybrid Clojure/Jython project the interoperation details are most crucial. That probably means you have to know in some detail how classes, interfaces, some of the standard library and (to a minimal extend) generics work in Java and how to deal with all of those in your chosen languages since the interoperation necessarily reduces to the more basic Java constructs. Some of this is tricky and can be confusing, and in clojure's case at least the documentation often refers back to Java concepts and documentation for obvious reasons, so you have to make sure you read both, closely.


I would definitely learn java and learn it well, not only because Clojure is built on top of the JVM but also to get anything done you will be calling Java libraries all the time, and you may even need to dip into Java occassionally.

On another note it would be expand your mind to understand Java's OO concepts and pain poaints too and this will enhance your undersatnding of Clojure too.


Above all, study the Java libraries. Part of the joy of using the JVM is having access to "it's already been done" libraries, as well as to parts of the core language that accomplish certain tasks with optimum performance on the JVM. In addition, some languages (e.g. Clojure) purposefully dip directly into Java and don't completely discourage it in your own code, so if you want to be able to read others' code Java basics are a must.

As for the rest of "learning Java" (design patterns, concurrency in Java, etc.), I wouldn't waste your time unless/until specific projects requirements demand it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜