The previous three chapters introduced basic concepts generally, with some examples in Python, and some in Java, though you weren’t expected to follow along and use that code yourself. But it’s different now. The following chapters have more in-depth language-specific language-specific implementations of things, building off the fundamental concepts explained in previous chapters. The general stuff was a build-up so that you could get big picture concepts before delving really deep into any particular thing. Feel free to use the code examples in this chapter (or anywhere in this book). You don’t need to ask for permission, just use it. I highly recommend installing the tools that are mentioned in the coding chapters and then writing and running the code that is mentioned in this book. Reading the book alone is not enough.
Software to download and install:
Java: https://www.java.com/en/download/
I personally recommend downloading Java 8, because I mention JavaFX GUI stuff later in this chapter, and it’s easier to start with if you use Java 8. Java 8 comes with a lot of stuff that was taken out in newer versions of Java, though you can still download and use them separately, but it’s more of a hassle, especially for beginners. So just stick with Java 8 for now.
Pick one of these IDEs to use:
Eclipse (open source): https://www.eclipse.org/downloads/packages/
IntelliJ (there’s a free version, called the community edition, and a paid version called ultimate): https://www.jetbrains.com/idea/
BlueJ (very limited features but it’s often used for education due to its simplicity): https://www.bluej.org/download/release-notes.html
If you have a very slow computer, try this:
Geany: https://www.geany.org/download/releases/
Please note that Geany is just a text editor, not a fully-fledged IDE. IDEs have more tools built into them. However, IDEs often have higher system requirements.
Which IDE is right for me?
Geany is the best if you have a super slow computer, but it’s just a text editor rather than a fully-fledged IDE. You will need to do command line compilation if you use Geany.
BlueJ is good if you want something that is easy to use, above all else.
Eclipse is nice and free.
Some people like IntelliJ more than Eclipse.
For my introductory Java classes, my professors recommended BlueJ, so that’s what I used. When I took my third semester of Java (a data structures course), my professor in that class recommended Eclipse. Eclipse and IntelliJ are used in professional environments. BlueJ and Geany are not.
Java file types:
.java – source code files. What you will be writing your code in. Example: Main.java
.class – compiled Java bytecode files that get run by the JVM (Java Virtual Machine). If you open it, such as Main.class, you will notice that it’s not human-readable.
.jar – a Java Archive file, which is basically a way to package up your Java app in a neat and tidy way, making it easier for other people to use your software. Many IDEs have a feature that lets you create a jar for your project. You can run jars from the command line using the following command:
java -jar jarname.jar
Some jars might need to be run using Java 8, which is an older version of Java that a lot of Java software still uses.
You can also run a non-jar java application like this in a terminal or command prompt window, but it first needs to be compiled:
javac myprogram.java
The above line will compile it. You can use the following line to run it after compiling it:
java myprogram
Please note that the command line commands listed above will only work if you’re currently in the directory that the java project is in. On Windows, if your Java project is located at C:\Users\Joe\Desktop\MyApp, then you need to use this command to change the terminal/command prompt window to the correct directory:
dir C:\Users\Joe\Desktop\MyApp
If you’re on macOS or Linux, you’d use a command like this:
cd /home/joe/Desktop/MyApp
Change the path for your user account name and wherever your project is actually located.
For more information about command line stuff, see chapter 8.
A Java IDE will typically have a “build” button you can click to compile and run the program. Sometimes you need to configure a build first, which involves telling the IDE which file to run. So if you call your program Main.java, you’d have to set up the build config the same way. If you use an IDE rather than a mere text editor, then you won’t need to do the above compilation and run commands.
Boilerplate in Java:
// put import statements above the Main class
// only have one class per .java file, like Main.java,
// SomeClass.java, etc.
public class Main {
public static void main(String[] args) {
// main method in Main class is where you write your code
System.out.println(“Hello world!”);
}
}
Printing in Java:
System.out.println(“Hello world!”);
Single-line comments in Java:
// this is a single-line comment
Multi-line comments in Java:
/* This is a
multi-line
comment in Java*/
Java packages (kind of like namespaces):
package myCoolPackage; //put before class
Java imports:
To use extra features without coding them all yourself, use import statements. There might be stuff in the default library or API that you can use, but you still need to explicitly put it into your program, rather than having it load everything into your Java program all at once. Instead, you only import what you need. It can be easy to get lazy and use wildcard (*) imports, but it’s not necessarily a best practice. People do it anyway though. Some more sophisticated IDEs will detect when you need to import something and might offer to do it automatically for you, whereas simpler editors like BlueJ or vim won’t do things like that. I think it’s best not to rely too much on automatic tool features when you’re starting to learn Java. First, you need to know what’s happening, and later, you can look into convenience stuff. But it can sometimes abstract away certain concepts that you should really be learning.
Here are some (but not all) import statements from my file steganography tool’s MainFX.java on GitHub:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
Of course, if you wanted to be more concise, you could do this, using the wildcard character *:
import javafx.application.*;
import javafx.scene.*;
import javafx.stage.*;
However, wildcard imports are considered bad practice. It’s easier and takes fewer lines of code, but it also means that you’re importing things in your program which you might not actually need, which can make it slower or take up more space. It can also potentially be a security concern because you’re adding lots of extra stuff to your program, which might increase its attack surface. So the best thing to do is only import what you actually need. Some IDEs can help you automatically add imports when you try to use something that you haven’t already imported, but it’s good to be aware of how to do it on your own rather than relying on features that are specific to a certain kind of IDE, as you will be eventually working in environments where you have to use specific tools that you might not already be familiar with.
Be sure to put imports at the top of a program, such as before your Main class starts.