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.
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
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
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,
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.