People communicate with each other using different languages. These days people often spend more time communicating with computers, smartphones and tablets than with each other. We casually say to each other, “I’ve installed a cool application that can do this and that”. In other words, people use applications to communicate with computers. So someone has to write these applications. Those who write computer applications are known as programmers or software developers.
Programmers write applications like games, calculators, and text editors using different programming languages. Without applications, computers, smartphones, and game consoles would have very limited use for most people. Computing devices usually come with an Operating System (OS) installed such as Windows, Mac OS, iOS, and Android, but imagine an iPhone that has no applications at all or a laptop that can only show the OS logo. Who would want such a device?
Programmers create applications using different programming languages such as Java, Python, C++, JavaScript, C, and many others. Some of the languages are good for producing small programs that require very little memory space and can be embedded in computer chips. Some programming languages are great for programming Web applications that run inside the Web browsers. Some programming languages are great for running on the server and handling thousands of users’ requests. There are languages that are fine tuned for creating beautiful graphics. Similar to human languages, programming languages have a set of words (a.k.a. keywords) and the rules for using them (the syntax).
In this book you’ll learn the syntax of the Java programming language, which can be used for programming most types of applications. And for many years the Java programming language has been one of the world’s most popular computer languages. In fact, as I write this, estimates are that there is an estimated 10 million professional Java programmers in the world, but what makes it so popular?
For one thing, the same Java program can run on different devices: from desktop computers running Windows or MacOS to handheld devices such as smartphones and tablets. As a matter of fact, Java programs don’t care what device they’re running on because they run inside a kind of virtual hardware called a virtual machine. We’ll talk about the Java Virtual Machine a bit later in this chapter.
Java can be embedded in tiny microchips that control various home appliances or industrial devices like the ones that power smart homes where the heat or light can be programmed remotely from your phone. These Internet-connected things can be programmed in Java.
Programming languages can have some general characteristics and we say that Java is an object-oriented language. Its program elements (classes) represent objects from the real world. For example, you can create a class called Car
and give it attributes that real cars have, like doors, wheels, and so on. Next, you can create another class based on the Car
class, like Tesla
, with all the features of the Car
class (doors, wheels) plus an electric motor.
And the software required to write Java is free. With so many million of professional Java programmers in the world, you’ll find lots of sample program online so if you’re run into a problem, you can either find the solution by searching or ask in one of the popular online forums like Stack Overflow, and someone will probably answer your question!
But enough talk. Let’s start learning Java.
To start programming in Java you need to download the Java Development Kit (JDK) from the company called Oracle, which makes releases and updates of Java versions. The full name of the software to download is Java SE Development Kit. We’ll download the Java SE 8, which was the latest version of Java available while writing this book. Just visit Oracle’s Web site. Accept the license agreement and select the proper link depending on the Operational System (OS) installed on your computer.
Note
|
While Java programmers need JDK installed, the users of your applications need to have Java Runtime Environment (JRE) installed on their machines. JRE is included in JDK, but can be downloaded separately as well. |
If you use Windows OS, download the file named jdk-8-windows-i586.exe. Since I work with Apple computers running Mac OS X, the file jdk-8-macosx-x64.dmg is my Java installer. Run your installer program, agree with all default answers (just press the button Next) and you’re almost ready to go.
In Mac OS, Java is installed in the folder /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk (there could other digits after zero, which indicates the update number). On Windows computers it’ll be installed under c:\Program Files\Java\jdk1.8.0, where c: is the name of your hard drive or SSD. If you use Mac OS, your installation is complete.
If you use Windows, you’ll also need to add Java to the environment variable Path
. Click on the menu Start, Control Panel, System and search for Environment Variables. You’ll see all system variables that exist in your OS As shown on the screenshot Windows OS - System Variables. If you have administrator’s rights onto your Windows PC, you can set user variables that will apply only when a certain user logs into your PC, or system variables that will apply to all users.
Press the lower button Edit and add the bin directory from your JDK at the beginning of the Path
variable value. For example, add c:\Program Files\java\jdk1.8.0\bin; if this is where your JDK is installed:
Now the Windows installation of JDK is complete! I’m sure you’re eager to start writing your first program now so let’s do it.
If you have an idea what to program, typically you’ll need to perform the following three steps:
-
Write the program in Java and save it in a file.
-
Compile the program to translate it from Java language into a special byte code that JVM understands.
-
Run the program.
In this chapter we’ll compile and run the Hello World program in the Command window if you use Microsoft Windows or from Terminal if you’re on Mac OS.
In Windows, to open the Command window click on the Start menu and enter the letters cmd in the field that reads Search programs and files . On a Mac, open the Spotlight window by clicking on the looking glass icon at the top right corner of the screen, type the word Terminal and hit the Return on the keyboard.
First, create a folder named practice by entering the following command:
mkdir practice
This will be the place where we’ll store all of our Java programs.
Starting from Chapter 2, we will use a special application called Integrated Development Environment (IDE) to write, compile, and run programs. But to get a better understanding of how things work, let’s use any plain text editor (e.g. Notepad, TextEdit et al.) to type the code for our first Java program. Afterwards, save the code in a text file with a name ending in .java.
For example, if you want to write a program called HelloWorld
, enter its code in a text editor.
Here is the program that prints the words Hello World! on your computer’s screen:
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
Save the above code with the name HelloWorld.java in the folder practice. Keep in mind that you are not allowed to use blank spaces in Java file names.
I’ll explain how and why this program works a little later in this chapter. For now, if you followed each step correctly, the program will print the words Hello World on the screen that we will go over in step 3.
Programmers often use the words the source code to refer to a program. So it’s safe to say the we wrote the source code of the program Hello World. Unless you’re an experienced Java programmer, you can’t just assume that this program is written correctly. Let’s proceed to Step 2 to find out.
Now you need to use Java compiler to turn the source code of Hello World program into byte code that JRE understands. The program named javac is Java compiler, which is installed on your computer as a part of the JDK and can be located in the bin folder in your JDK.
Change the directory to practice with the command cd practice. If you have never used the command cd before, read the online instructions on how to do it on Windows or on Mac OS. Now compile the program using the following command:
javac HelloWorld.java
You won’t see any confirmation message that your program HelloWorld
has been compiled successfully. Having no messages means there no problems occurred during compilation. Type a command dir on Windows or ls on Mac and you’ll see the list of files in folder practice. This is how it looks in my Terminal window:
First, I’ve entered the pwd command to make sure I’m in the practice folder. You should see a new file named HelloWorld.class
. This proves that your program has been successfully compiled. Your original file HelloWorld.java
can also be found there, and you can modify this file later to print Hello Mom or something else.
If the program has syntax errors, for example you forgot to type the last curly brace, Java compiler will print an error message. Now you’d need to fix the error, and recompile the program again. If you have several errors, you may need to repeat these actions more than once until the file HelloWorld.class
is created.
Now let’s run the program. In the same Command or Terminal window enter the following:
java HelloWorld
After running this command, you’ll see the text Hello World! printed in the Terminal window as shown in Running HelloWorld. Have you noticed that this time you’ve used the program java and not javac? This is how you start JRE by providing it the name of the application to run (HelloWorld
in this case).
Keep in mind that Java does not treat capital and small letters the same, which means that if you originally named the program HelloWorld
with a capital H
and a capital W
, do not try to start the program helloworld
or helloWorld
– the JRE will signal an error. The file name should match the class name as well. Typically, each file of your application contains the code of one class.
Now let’s have some fun trying to guess how to modify the code of the program that prints Hello World. I’ll explain how this program works in the next chapter, but for now attempt to guess what has to be done to say hello to your pet, friend or print your address. Go through all three steps to see if the program still works after your changes. If you don’t have a pet, you can always say hello to my puppy - his name is Sammy. Are you up for the challenge to write a program that prints Hello Sammy! on the screen?
In the next chapter you’ll learn how to write, compile and run your programs in a more convenient environment than just a text editor and a Command window.