This morning I woke up and saw the following Tweet:
I'm a bit pissed right now! đĄ Why is the world so mean to me, to all of us developers, really? We've been so friendly! How often have we written "Hello, world"? And has anybody ever gotten as much as friendly nod in return?
— Nicolai Parlog (@nipafx) May 14, 2020
And yes, I agree.
The world has been a pretty shitty friend to us in 2020. Why should all the new programmers/developers have to welcome this messed up world with open arms?
I say: Screw You, World.
Your first program
My programming language of choice at the moment is Java. So thatâs what Iâll be using for this âScrew You, Worldâ.
To get started with Java there are a couple of things youâll need to learn and install to get it running.
Letâs go!
Java JDK
There are a lot of places where you can download a so called âJDKâ, which stands for Java Development Kit. This is needed for you to create and build your first Java program.
For now I wonât be bothering you with all the different releases and versions, we can just go to AdoptOpenJDK.
And download and install the latest version: OpenJDK 14 (latest)
REPL or Class
Now we have two different options, we can start the Java REPL (read eval print loop) to write Java code interactively, or we can store our code in a so called âclassâ-files and run it.
First weâll look at the REPL, this is probably the easiest way to write your first âScrew You, Worldâ.
Java REPL: jshell
The interactive code-writing tool in Java is called âjshellâ and is launched from the Terminal/command line.
To do this, navigate to the JDK (usually there is a $JAVA_HOME
variable for this) and start:
Next youâll be presented with an interactive shell to write your code in:
In this little editor you can write any valid Java code you want. Letâs grab the System
class, get the out
output stream (which defaults to the terminal), and write some text there for the world to see:
And thatâs it. Weâre done.
Using the REPL weâve created a line of code which contains a message to 2020.
Java class files
The second way to write Java code is to use a text editor (or an IDE, an integrated development environment, like IntelliJ) and save the files, compile the files and run the files.
The code above has little to no real structure. In larger programs weâll need to create some order in all the code you want to write and run. To do this Java has âclassesâ.
So open any text editor and write your first class file:
Save this file as ScrewYouWorld.java
. This is the human-readable version of your code.
The next step is to take this code and process it, compile it. This turns the readable java
file into a class
file, which is made out of bytecode
. It is still the same code, but optimized for machine readability.
To do this we call:
Now weâll get a file named ScrewYouWorld.class
.
This class
file is just en empty shell, it has no real code in it yet and there is not even a valid âstarting pointâ. When programming in Java there are usually a lot of different classes that make up the application. The code however has to start somewhere, weâll have to define a starting point.
In Java there is a very specific method
we need to implement, this makes it a starting point. A method
is a block of code we can call and it does something. The one used to start your code is always called main
.
It looks like this:
Letâs break it down. First we have public
, this means any class can access it, which is what we need to start our program.
Next we have static
. There are TWO kinds of methods in Java, static
and non-static/member
methods. For now letâs just say that static
methods are working on a class
level, and non-static
methods are working on the object
level. This is a whole topic for another day. To start we have to make this static, from there we can create new instances.
Next we have void
, this means that the method we are calling doesnât return any results. Some methods calculate something and return a value, like a number or more complex answers.
main
is a specific name, the name of our method.
The information between the parentheses are the method âargumentsâ. These are passed into the method. Just like arguments you supply to a program. In this case it is an array (list) of Strings.
This all becomes rather complicated quickly, but we donât need to know the specifics right now.
Next we can add the line we wrote before to inside the method brackets. Classes add a little bit of structure/scaffolding to our applications.
If we put everything together weâll end up with:
Save this file and compile to bytecode again:
Now we can run this code and see the result. We take the compiled class
and the java
command understand that the main
method should be called:
Congratulations, weâve just used two different ways to write and run your first Java program!
TODO: Add more âScrew You, World.â examples in different languages.