Skip to main content

Modern Java From a Python Perspective

I had a friend visit me today. He's a Java programmer, but he also knows enough Python, Ruby, JavaScript and Scheme to be dangerous. I asked him to show me around his code so I could see some modern Java. I learned Java back in the 1.0 days, but I stopped paying attention back around 1.4. Here are some of the things I saw.

Generics are as I expected.

Annotations are interesting. There was code like:
    @SomeClass
public void method() {
...
}
"SomeClass" acts like a "tag" for "method". Elsewhere in the code, you can define wrapper methods (i.e. decorators) for all the methods that have been wrapped by "@SomeClass". Clearly, this was the inspiration for Python's function decorators. I had simply never seen them in Java before.

Dependency injection is interesting, as I expected it to be. In general, you accept parameters for everything you need, and you don't worry a lot about how things get instantiated. You avoid using the "new" keyword, and instead let the dependency injection framework instantiate things for you. This takes care of circular dependencies. There was a Java file that configured what gets bound to what. Hence, if I ask for SomeFoo, it'll give me an instance of the right SomeFooImpl. Basically, it's a whole layer to add more dynamism.

IntelliJ was impressive. I asked him to write a module from scratch so I could see his workflow. He right clicked and hit "add new class". IntelliJ created a file and asked if he wanted to add it to Subversion. Then he simply started typing in the implementation for the class. Whenever he typed something like FileWriter, it automatically added the right import line. Whenever he used a class attribute that didn't exist yet, it showed up in red until he told the IDE to create the attribute. Then, he had to click on the attribute to add a getter and setter.

I knew that Java IDEs held your hand a lot. I didn't know that the work flow was to simply start using something and then have the IDE generate the boilerplate for you. That was interesting. I liked how nicely he was able to navigate the code base. Best of all, IntelliJ has really nice Vim keybindings, which he was using.

He showed me Struts 2 and his usage of JSP. Those weren't all that remarkable or new to me.

I returned the favor by showing him some of the code from my current project. He said, "Wow, your Python code is really beautiful," which I took as quite a compliment. He also said that there was clearly a lot less boilerplate and that more functionality was achieved in fewer lines.

Clearly, a lot of useful code is written in Java. glassfish, Lucene, HiveDB, and IntelliJ are just a few things that come to mind. I'm not going to give up Python anytime soon, but it was nice to get a peek into another world.

Comments

Anonymous said…
Nice write up. It's a great to be passionate about a language but I get a little worried when I see developers becoming too fanatical about a particular language. Learning or learning about other languages helps solidify one's knowledge about their language of choice. When we compare and contrast different languages we gain a deeper understanding.
philhassey said…
Really nice post :)
lotrpy said…
funny post, r u ONLY using python now?
jjinux said…
> When we compare and contrast different languages we gain a deeper understanding.

Agreed, although I've been spending a lot more time with weirder languages like Erlang and Haskell than I have with Java ;)
jjinux said…
> funny post, r u ONLY using python now?

I was using Ruby for a previous startup. I often use a bit of sh, sed, awk, and make. I often play with other weird languages.

However, I feel most comfortable using Python.
Anonymous said…
Weird. I use ruby myself but I never understood people who had the need for awk, sh etc..

I write the code in Ruby, sometimes a bit C if needed, and the rest will lie in human readable files (either yaml or a simpler and error tolerant format yet as short or even shorter as yaml)

I could see myself picking up Python but I would never see myself picking up Java. I simply dont see any gain in it compared to ruby or python.
jjinux said…
> I never understood people who had the need for awk, sh etc..

Sometimes a tiny bit of sh really hits the spot. When I use awk, it's mostly for one liners. Awk is still lean and fast.

> I could see myself picking up Python but I would never see myself picking up Java. I simply dont see any gain in it compared to ruby or python.

You're preaching to the choir ;)

Nonetheless, I hate not knowing stuff. Hence, if there's something cool going on in some other language, I want to know about it to see if I can apply it to my own code.
Anonymous said…
As some who would qualify as ``dangerous'' (lots of Java, IntelliJ fan, good command in JavaScript, moving to Python), I have to say that I would love to see an IDE to do the same job for Python as IntelliJ does for Java. And being a dynamically-typed language is no excuse for Python not to have good tool support. You should just see how IntelliJ guesses types of JavaScript variables, for instance, and gives adequate completions.
Anonymous said…
As someone who...
Unknown said…
>> He said, "Wow, your Python code is really beautiful," which I took as quite a compliment. He also said that there was clearly a lot less boilerplate and that more functionality was achieved in fewer lines.


I think this is why there aren't many IDEs for Python as Java IDEs. Python needs tools, but they need to be different. IPython is one good example.
lotrpy said…
@jj
sorry, I am not a native english speaker, what's the mean of "You're preaching to the choir" ?
@ngn
My favorite Python IDE is wingide, especially for the auto complete and debuger. maybe it's not as powerful as IntelliJ , no clues here.
Anonymous said…
'Preaching to the choir': promoting a belief to people who already hold it.
lotrpy said…
i understand "'Preaching to the choir'" now,thanks for the reply
jjinux said…
> I think this is why there aren't many IDEs for Python as Java IDEs. Python needs tools, but they need to be different. IPython is one good example.

Agreed. I don't write my code in an IDE with autocompletion. I *run* my code as I write it in IPython, which *has* autocompletion.
jjinux said…
> sorry, I am not a native english speaker, what's the mean of "You're preaching to the choir" ?

It's like telling President Bush that he should love America.

I already love Python. No one needs to tell me how good it is ;)

> My favorite Python IDE is wingide, especially for the auto complete and debuger. maybe it's not as powerful as IntelliJ , no clues here.

WingIDE is generally considered the best IDE for Python. It's good. However, IntelligJ probably has an order of magnitude more features, such as refactoring support.
Anonymous said…
To have some nice autocompletion, auto adding import lines... etc. try Pydev which is a nice Eclipse plugin for Python development.

There's also Pydev Extensions which is payware, but seems to complete what Pydev lacks.

Give it a spin.
jjinux said…
> Pydev

Thanks for the tip. I always feel overwhelmed when I try out Eclipse. One of these days, I think I need to take a tutorial, or something. I'm sure it's nice once you get up to speed.
jjinux said…
Heh, this post made it to DZone. Thanks guys!
Dependency Inject is very cool stuff. As Joshua Bloch said, it is the future of Java.

The framework I showed you yesterday is called Google Guice:

http://code.google.com/p/google-guice/

And thanks again for lunch =)
Brandon L. Golm said…
I have to say that this talk of IDE and toolchain misses something critical. You need autocomplete in Java because that language is so hideously verbose. Think about the rest of the tool-chain and similar thoughts follow (like that more code fits on a screen and isn't as ugly, so it's easier to deal with; more fits in your brain so there's less need for tool support, etc). The IDE solves a problem of a language's, not general coding problems. That's, IMO, why they aren't as popular in other languages. You just don't need eclipse in Python. Though, maybe I'm overstating this all a tiny bit.
Alex said…
Those Java IDEs look impressive until you start using them for real project. That's where nightmares start. I'm not saying they are evil but... they are. I've seen so many dummy Java class constructors, getters/setters, etc. People get used to code that their favorite IDE produces. They don't know the language any more. They learn to click buttons to produce code.
Anonymous said…
> You should just see how IntelliJ guesses types of JavaScript variables, for instance, and gives adequate completions.

my vim guesses types and gives adequate completions
EY said…
There is actually a big difference between Java annotations (@SomeClass) and Python decorators (@SomeCallable).

With Python decorators, you can actually modify the function by passing it to a callable which can wrap it in another function. With Java annotations, @SomeClass is just a simple struct of primitives (strings, numbers, arrays) that can be queried using reflection, but has no impact on the function itself -- annotation classes have no executable code.
@ey

I showed jj annotation in the context of using them with AOP Alliance method interceptors.

Guice does a really nice job in making AOP much more convenient to use.

Good overview of Guice and Interceptors

Popular posts from this blog

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python . Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening. The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for B

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

Creating Windows 10 Boot Media for a Lenovo Thinkpad T410 Using Only a Mac and a Linux Machine

TL;DR: Giovanni and I struggled trying to get Windows 10 installed on the Lenovo Thinkpad T410. We struggled a lot trying to create the installation media because we only had a Mac and a Linux machine to work with. Everytime we tried to boot the USB thumb drive, it just showed us a blinking cursor. At the end, we finally realized that Windows 10 wasn't supported on this laptop :-/ I've heard that it took Thomas Edison 100 tries to figure out the right material to use as a lightbulb filament. Well, I'm no Thomas Edison, but I thought it might be noteworthy to document our attempts at getting it to boot off a USB thumb drive: Download the ISO. Attempt 1: Use Etcher. Etcher says it doesn't work for Windows. Attempt 2: Use Boot Camp Assistant. It doesn't have that feature anymore. Attempt 3: Use Disk Utility on a Mac. Erase a USB thumb drive: Format: ExFAT Scheme: GUID Partition Map Mount the ISO. Copy everything from