Java for Javascript programmers

part 1/2


August 19th 2019 | ~6 min read

When I first got started with development I primarily focused on javascript. I was exposed to other languages such as php, python, java, c, and haskell in school but whenever I had the chance I'd use javascript. For me it made sense - I liked the syntax, I could run it basically anywhere I wanted and it had support for multiple programming styles.

I've heard a lot of people say that once you lean one language or maybe at least one functional and one object oriented language that learning a new one is easy. To some extent thats true - learning new syntax is trivial but I think being able to express 'hello world' is a pretty far cry from being able to write performant, idiomatic code that fluently utilizes common libraries and language specific tooling.

Recently I've had the opportunity to write production code in a couple of new languages, namely Python and Java. I found java a little harder to get started with than some other languages I've picked up so I thought I'd compile some of the insight I gathered going through the process is case there are other js programmers out there who need to pick up java.

Q: What are some common uses of this language

Java has been around slightly longer than javascript and is used in almost as many places. Java has a strong hold in large enterprise companies, and at one time or another has been used at companies like Amazon, facebook, twitter etc. Its a general purpose programming language but can be used to make android apps, create REST API or program games for example.

Q: Why do people like it?

People often sight Java's type safety, and its support for Object oriented programming. Coming from a node background I found its support for multi threading was a nice feature.

Q: Where can't I use it?

At one time java could be run in the browser but thats no longer supported by most browsers. In some situations starting up java can be slow so its probably not good for serverless applications (ie aws lambda, google cloud functions ect)

Q: What broad programming paradigms are possible / common in this language?

Java is really opinionated relative to js in terms of programming paradigm. For example functions can not be declared outside of classes. Recently the Java streams apis provide limited support for features common in functional programming languages such as map, reduce and lambda functions but the syntax in my opinion is a little clunky.

Q: Whats the JVM?

One of Java's design goals was platform independence. In js browsers largely take care of this problems for us as long as we transpile our js down to the lowest version of js that is still in common use. Java on the other hand comes with a system called the Java virtual machine. Its a program that reads compiled java code (byte-code) and works with the operating system to run java programs. The JVM works on multiple OS and thats how java achieves platform independence.

Q: Is memory management manual?

Nope - memory is garbage collected like in JS however unlike js you can manually request the JVM collect is garbage. This means that unlike c you won't be directly freeing memory when you're done with it. Memory leaks are still possible. If you want more control over memory usage you may need to drop down to a lower level language like c or rust but if thats not an option you can also look into using object pools which work in both js and java:

Q: Whats the type system like?

The type system will trip you up if you're not already familiar with typescript for example. Its static meaning ('4' + 5 will throw errors) and you'll need to explicitly declare the types of things most of the time. It prevents a whole category of hard to debug issues but also adds some hoops to jump through. In my experience it made using 'console.log' to sanity check my code as I develop a little harder so you may want to use a IDE with a debugger and Intellisense (ie info about types and methods available durning auto complete / hover over).

Q: What IDE do I use?

A lot of people like use Intellij to program Java. It has some nice features but can feel a little 'monolithic' if you know what I mean. Personally I still prefer VS code as I don't have to relearn a whole new set of keyboard commands and can keep using my vs code plugins. Here's some info on setting up vs code for java if you're interested.

next time

We've only scratched the surface here but this article is getting a little long so Im going to break it up into multiple parts. In part two we'll cover:

  • How are project files structured / organized?
  • Whats the preferred package manager?
  • Is there a preferred build tool?
  • Whats libraries are available for unit testing and how do I run them?
  • Is there version manager(s) / virtual environment tooling available?
  • What are some of the 'core data types' that get used heavily in idiomatic code
  • Take note of the time complexity of common operations on these data types
  • Are there performant immutable versions of these data types available?
  • What abstractions are available for running asynchronous operations?
  • What abstractions are available for handling concurrency?
  • What abstractions are available for managing side effects?
  • What are the core concepts necessary to understand that languages performance characteristics? ie is it garbage collected or are there concept's like the JS event-loop which need to be understood
  • How would I make a micro service in this language?
  • How would I make a UI in this language?
  • How would this language change the way I implement basic algorithms (search, graph traversal etc)
  • What are some common tools / clients for interacting with Databases from this language?
  • What are some of the high profile open source projects / libraries?
  • What are some good examples of 'expressive' code in that language
  • What are some good examples of larger code-bases in that language and how are they structured.