Java improved its 'Hello World' experience

Smoother on-ramps with new Java Enhancement Proposals

No bootcamps are choosing Java.

I was recently thinking about how we can get more people to program Java. While I think we still got work in many areas (more about that in future blog posts), I think the biggest influx of new Java developers could be found in bootcamps.

While often overlooked when talking about “onboarding new people to our industry”, bootcamps are a great pathway to join our industry. The training program of bootcamps are often much shorter than formal education and focus on the practical aspects of coding. This is ideal for those who want to learn how to code after having worked in another job and/or have limited time due to other aspects in life.

The sad reality is, bootcamps will almost always teach either Javascript (Node) or Python instead of Java. This is not surprising. Let’s be fair, when it comes to onboarding new people into our ecosystem, Java typically hasn’t been the most welcome of languages. Just ask any programming teacher.

When I’m teaching a new programming language to people, I love to give them a first “WIN” pretty early on, typically a “Hello World"-experience. However, in classic Java, to run even the simplest program, you need:

- To install the JDK (and set Path variables…)

- Write a file (ex. Main.java) that looks like this (while trying to ignore all the new terminology and keywords)

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

- Compile the file using the command line javac Main.java, which creates a Main.class file.

- Run the compiled class java Main (and somehow… you don’t specify .class here?)

That sure is a lot of steps to just print a simple string! Maybe this is why bootcamps and self-learning individuals will almost always choose either Javascript (Node) or Python over Java. The “Hello World”-experience for those languages is just much smoother.

At least, this used to be the case in older versions of Java! The Java Language Architects recognized this problem and have been softly introducing new features to bridge the gap. So, let’s have a look at what the “Hello World”-experience can look like in modern Java.

JEP 330 - Launch Single File

To run Java programs, you typically need to compile them using javac. This immediately introduce a 2nd command (javac instead of java_ to the “things you need to know to do Java"-box… and new learners haven’t even seen a single result yet!

So let’s remove the concept of “explicitly compiling” for a second so learners can get their “WIN”. With the introduction of JEP 330 in Java 11, it is possible to run a single Java source code file directly with the java interpreter!


pexels-photo-3761504.jpeg

Developer experiencing a 'WIN'


Of course this isn’t magic, there is still some compiling going on, but it is done in-memory when running java. This also means no “.class” file will be generated, which is another thing new learners don’t have to worry about yet!

All details of this JEP can be found at: https://openjdk.org/jeps/330

With this already much improved path to “WIN”, let’s look at the real monster under the bed.

Cognitive Load

As a teacher, I build up my lessons by gentle introducing new concepts one-by-one, each building on the other. The Java “Hello World"-experience doesn’t facilitate this very well. Just have a look at this.

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

As a teacher, you have some options here.

  1. You could explain absolutely everything that is in this file. You’d have to mention classes (Object Orientation), methods with parameters, what static means, how to declare Strings, describe what [] means… and then there is the public access modifier. Needless to say… that’s a lot of theory before getting our “WIN”.

  2. You say: “Ignore all the fluff, just copy this… the part we want to focus on is the System.out.println”. As a learner, I’d already be highly distracted and not listening to the teacher anymore. All my brain wants to do is to analyze what those things are the teacher just told me to ignore!

The way Java requires you to write “Hello World” right now just sets you up for some high cognitive load. There are much more fundamental things to learn about coding (ex. variables, subroutines, if/else statements!) before the notion of Classes and OOP should be introduced in the curriculum.

Luckily, help is on the way!

JEP 445: Flexible Main Methods and Anonymous Main Classes

When the newly proposed JEP 455, gets implemented, we can get rid of a couple more things!

First off, due to Flexible Main Methods the String[] parameter of main methods can be omitted. Secondly, this JEP also allow the main method to be neither public nor static, removing the need to talk about access modifiers early on.

class HelloWorld {
    void main() {
        System.out.println("Hello, World!");
    }
}

The example above is already much better than what we had before, but it still introduces the “class” keyword. Luckily, the JEP didn’t stop there! With Anonymous Main Classes, they made the class declaration implicit. This reduces the code needed for “Hello World”, and the “WIN” for our students, to just:

void main() {
    System.out.println("Hello, World!");
}

While some people were arguing this is not worth the valuable time of the Java Language Architects, I believe that couldn’t be further from the truth. Making Java more accessible for new developers is what will keep this language and surrounding ecosystem thriving.

All details of this JEP can be found at: https://openjdk.org/jeps/445

With this much smoother on-ramp falling into place… we just need to spread the word on this.

Spread the word: Java has changed for the better!

I started this blog post by pointing out that no bootcamps are choosing Java. As a Java enthusiast, I do believe that the winds are blowing in the right direction to make a change. With a greatly simplified on-ramp into the language and the many other changes to the language (nicely summarized in Frank Delporte’s blog over on FooJay), I believe now is the time!


java.png

So here is what we could do: Reach out to some bootcamps and talk to them about Java.

  • Ask them why they haven’t chosen Java for the curriculum and show them the newest enhancements to the language.
  • Share with them the many great resources we already have (such as Angie Jones’ free online course, or many of the Foojay posts) and convince them that Java is worth teaching.
  • Why not present a short Java introduction session yourself, and share your love and passion for the language?

Bootcamps are a great way to get into our industry in a non-formal way. With these new JEPs, now is the perfect time to introduce bootcamps and self-learners to a much friendlier Java!

We can wait until they discover it themselves… or we can take a more active approach to onboard new people into our community. Me personally, I prefer a more active approach.


Until the next time...lots of 💖
Tom