For decades, the traditional first program created when learning a computer programming language has been the “Hello World!” program. I like the tradition, so we’ll do the same here.
All Hello World does, is print out “Hello World!” As it turns out, there are lots of ways to do this in Java. They vary as widely as printing to a web page, or using a Swing dialog to pop up the words.
The two most cutting edge ways to do new Java apps are with GWT and JavaFX. GWT is too difficult for first time programmers, so I’ll stick to using the friendlier JavaFX for this tutorial.
This is part of the Java Tutorial series I’m writing for my son.
Installing the JDK: Java 8 update 40
If you already have JDK 1.8.0_40 or newer on your computer, and know how to check it, you can skip this section. If not, then read on.
Java version numbers are confusing to the uninitiated. Way back before the dawn of time (the mid 1990’s), a company called Sun created the language called Java. The first version was called version 1. And, everyone was happy with their choice of version numbers.
Then, Sun came out with a second version of Java, and called it version 1.1. That would be a major version of one and a minor version of one. Most people were still satisfied with Sun’s version naming. After all, 1+1=2.
Soon, Sun got bored of having happy customers that understood the version numbers of Java, so created a third version of Java and called it Java 2, except around developers. Around developers they called their 3rd version of Java, version 1.2.
Yes. There are evil geniuses in the world, and many of them have jobs in marketing.
As of today, the current version of Java is know to the world at large as “Java 8”. To computer programmers, the current version of Java is known as “java version 1.8.0._45”. You really don’t want to know the real number of versions of Java that have been created.
These days a company called Oracle gives the world Java, … for free! That means that you can download it, create programs and sell those programs to Microsoft for $2,500,000,000.00. (Well, that’s what Minecraft sold for. Milage and luck may vary.)
Most Java users have the common version of Java on their computers. That is called Java Standard Edition, or Java SE for short. As a new Java developer, you need the full development version called the Java Development Kit or JDK for short. As stated above, you need version 1.8.0_40 or higher.
To check for JDK 1.8.0_40 or newer on your computer, open up a command line prompt. Depending on your operating system, the command prompt may be available in a DOS window, PowerShell, Terminal, or console window. Find your system’s command line and type the following.
On OS X, a successful response looks something like the following.
If your version test fails in any way, or you just don’t trust your computer to tell the truth, … Let’s face it. Some computers are compulsive liars, … Go to Oracle’s Java developer site and get a shiny new version of Java SE’s JDK.
Don’t worry about the 10 flavors of Java for developers on their site. Ignore Java EE, Java ME, Java Embedded, Java DB, Java Card, Java TV and all the others. You want the Java SE version of the JDK. You can worry about the other versions once you have the vanilla version of Java mastered.
To download the JDK, accept Oracle’s Java license agreement, and select the version of the JDK that is built for your OS. Then, jump through all the hoops your system throws at you before it let’s you install the JDK.
After installing the JDK, you will need to close the window (or application) with your command line, and open a new one. After doing so, your should be able to run the javac -version command from above and get a satisfying result.
All programing languages require code to be saved in plain text files. Many “helpful” programs automatically save anything you write to *.rtf or *.docx formats without telling you. To keep the headaches away, you’ll write your first programs with jEdit. jEdit won’t mess up your Java program files without warning you.
Go to the jEdit home page at www.jedit.org, and install a copy of jEdit.
When you type in your Java code, and save the file from jEdit in a *.java file, you are really just saving a plain text file. jEdit will add all kinds of fancy coloring to your code when displaying it, but the actual saved file only has the Java code. The coloring is called syntax highlighting, and is only there to help you as a programmer quickly see if the code is written properly.
Time to write your first Java program. Type the following code into jEdit, and save as “HelloWorld.java”. The naming and capitalization are very important. The file name must end in “.java” to be the right format for the javac command. The first part of the file name must be “HelloWorld” to match the name on line 7 of the code.
You might wonder what all those funny commands do. Lines 1-4 are imports. They add libraries of code created by other programmers to your code and give your code magic powers. Mostly, those powers allow you to use words like Alert and Application without writing code to explain to the computer what those words mean.
“Import” is your friend.
Line 7 tells the computer that your code file will be named “HelloWorld.java”, and that the compiled version of the file will be named “HelloWorld.class”.
Lines 12-15 tell the computer to open an information dialog and display the words “Hello World :-)” in that dialog.
The rest of the of the code is just standard decoration for JavaFX programs. You can just copy that code into new JavaFX programs, or later on let a program like Eclipse or NetBeans create that code for you.
Before running a Java program, you need to compile the program. Compiling is the process of translating code you can read into code the computer can read.
Open up a command line prompt, and navigate to the folder you saved the “HelloWorld.java” file in. Compile the code with the following command from the command line.
Check the directory. You should now have a file called “HelloWorld.class”. HelloWorld.class is the machine readable code. Technically, it is JVM readable, but from your perspective, it’s machine readable.
Now use the java command to run your *.class file. Run the compiled class with the following command. Notice that the command does not include the file extension, *.class
Look at that! You just created your first Java program. I suggest running it several more times just to ride the euphoric wave that’s currently engulfing you.
1) Rename the class and file, and compile the program with the new name.
2) Customize the message on line 14 to greet you or a friend, instead of the entire world.
3) Insert \n into the “Hello World” message and see what happens.
(for example, “Hello\nWorld\n:-)”)