Thursday, June 22, 2017

Dealing with Data Types

With all of the Java installation and configuration out of the way, it's time to look at some of the fundamental building blocks of the Java language. A main feature of all programming languages is that it should be able to manage data. This could range from something simple such as text entered by the user to complex, structured data such as thousands of parts in a warehouse and the corresponding supplier data. At the end of day though, all data can be broken down into simple values, such as numbers and text entries. For example, a part could be represented as a part/serial number and a name. The supplier could be represented by an ID, name, and address, which further breaks down to street address, city, and zip code. The most basic types of data supported by Java are called primitive types, which will be covered in this section. Later on, we'll see how to combine simple data entries into objects.

Java has 8 primitive types, which are based around numerical and character data. Numerical data can further be broken down into whole number (Integer) types and fractional (Floating point) numbers. The types are byte, short, int, long, float, double, boolean, and char. That may seem like a lot to take in, but a few of these types don't show up very much in actual programs. Typically, int, boolean, and double see the most usage, and the other types have more situational uses. Let's take a more detailed look at each type.

Byte

Bytes are the smallest integer type in Java. It is used to represent whole numbers from -128 to 127. Remember that all data is represented as bits (0s or 1s) on the computer. By using 8 bits, a byte can hold 2^8 = 256 possible values. These numbers are split evenly between positive and negative values, giving the range of -128 to 127. In practice most values can't be restricted to this range. For example, the number of days in a year could not be represented using a byte. Instead, bytes are most commonly used to manage binary data, such as data coming from the network or a a file. We'll see examples of this much later on.

Short

Shorts (and I don't mean the clothing) are used to represent whole numbers from -32,768 to 32,767. Shorts are composed of 16 bits, which gives 2^16 = 65,536 possible values. As before, half of the numbers are negative and the other half are positive. In practice, lots of values still don't fit into this range, such as the number of items in my local grocery store. For this reason, the short datatype doesn't see much use.

Int

Ints are 32 bit whole numbers, which gives a total of 2^32 = 4,294,967,296 possible values. The range of ints are from –2,147,483,648 to 2,147,483,647. This is large enough in most programs. Ints are commonly used in control statements and loops, which will be explained in future posts.

Long

Longs are 64 bit numbers ranging from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (Approximately 9 quintillion). Since ints are usually large enough, you only need to use longs when dealing with large numbers. If longs still aren't big enough (for example, measuring astronomical distances), Java provides a BigInteger class, which is not a primitive type and incurs more overhead. However, it's unlikely that BigInteger is needed in most applications.

Float

Floats are used to represent decimal numbers using 32 bits. They have a range from about 1.4e–45 to 3.4e+38, and can be both positive or negative. Although that seems like a very large range, keep in mind that there are only 2^32 = 4,294,967,296 possible values that can be represented using 32 bits. This means that there is a loss of precision when storing numbers in floating point format. For example, due to how binary numbers work, it is impossible to represent the number .1 using a finite number of bits. The means that .1 might be stored as .0999994 when using floating point numbers. In general, floats have about 7 digits of precision, so decimal numbers with more than 7 digits may have roundoff errors. This can be a big deal in certain applications, such as banking systems, which require a high degree of accuracy when dealing with numbers.

Double

Doubles are basically the same as floats, except they use 64 bits instead of 32. This increases the amount of precision from 7 digits to 15. This makes double a better choice over floats most of the time. Surprisingly, some computers can perform mathematical functions faster on doubles than floats. This is because some processors deal directly with doubles and simulate floats by converting them to doubles. This isn't always true though, and it almost never makes a noticeable difference in speed. I recommend sticking with doubles over floats for the increased precision (at least until you really understand how floats and doubles work). Similar to BigInteger, there is a BigDouble class to represent decimal numbers that require more than 64 bits. Again, BigInteger and BigDouble are not primitive (built in) types, they are supported as an "extension" to the core Java language. BigDecimal usually isn't needed, but may come in handy when doing financial transactions.

Chars

Chars are used to store a single character using 2 bytes of memory. Characters are usually simple letters or numbers (A-Z, a-z, 0-9, etc), but could also be foreign symbols, such as Chinese or Japanese symbols. Chars are put inside of single quotes, such as 'a' or '源'. Since a byte is 8 bits, it's possible to store 2^16 = 65,536 different letters using a char, which is enough to cover all of the English letters and most of the foreign language symbols. Older languages, such as C, used 8 bits to store a character, which caused issues when working with foreign languages. There's a few edge cases such as certain language symbols or emojis that take more than 2 bytes to store, but they can still be used in Java.

Boolean

A boolean is a value that is either true or false. It may seem like booleans only take one bit to store since it can only have 2 possible values. However, the smallest unit of memory that can be addressed (looked up) in most computers is a byte (8 bits), so that is typically the size of a boolean. There are ways to represent boolean values using only 1 bit of memory, but with memory being so plentiful on modern computers, it's unlikely you'll need to do this. Booleans have their own set of unique operators. Integers/doubles can be added, multiplied, etc. Booleans can be combined with other booleans used and, or, not, and a few other operators to create more complicated boolean logic.

Declaring Variables


Now that we have a basic understanding of the basic data types in Java, it's time to look at declaring variables. A variable is simply a named reference that contains a data value. Variables are stored in memory and can be referenced later to retrieve their value. It's similar to using the memory button on a calculator to store the value of a previous computation. Let's look at some examples.

To declare a variable, use the format data-type variable-name;, where data-type is the type of variable (int, long, boolean, etc) and variable-name is the name you give to reference the variable later on. Variable names must should start with a letter and contain only letters, digits, or the underscore character _. Variable names can also start with a $ or _, but that is considered bad practice. Here are some examples of variable declarations:

int numPages; //valid
double age; //valid
boolean isSunday; //valid
float $amountEarned; //valid, but using $ is discouraged
char 1LetterWord; //error, variable name cannot start with a number
integer numPages; //error, integer is not a valid data type, use int instead
short if; //error, if is a reserved word
int numPages //error, semicolon is required at the end

As you can see in the examples, it is common to start the variable name with a lower case letter and put the first letter of subsequent words in upper case. This is called camel casing. Since variable names cannot contain a space, this convention makes it easy to identify where a word starts and ends in a variable name. Variable names should not start with an uppercase letter, as that convention is used for class names, which will be discussed in a later post. Also, variable names cannot be a keyword (also called a reserved word). These are words reserved by the Java language and include fundamental programming constructs things such as datatypes, conditionals, and loop control words (int, if, for, etc). Also, notice the ; is needed at the end of each variable declaration. Semicolons are used to terminate a statement, which is a complete unit of execution. You can think of a statement as a sentence and the ; is the period used to indicate where one sentence ends and another starts. Not every line of Java code is a statement and ends with a ;. Knowing when the ; is needed mostly comes with experience writing programs.

Defining Variables


Declaring variables isn't too useful by itself, because the variables haven't been given a meaningful value. In some cases (for example, class fields) variables are automatically given a default value (0 for numbers and char, false for boolean). In other cases (method level variables) there is no default value given to a declared variable and it is an error to use it without providing a value first. Definitions solve this problem by giving the variable name and value at the same time. The format for defining a variable is data-type variable-name = value; Let's look at some examples:

int numPages = 300; //valid
double dollarToPeso = 18.15; //valid
char letter = 'x'; //valid
boolean isSunday = false; //valid
boolean isSunday = 0; //error, 0 is a number, not a boolean value
int numPages = 310.2; //error, 310.2 is not an integer;
char letter = "x"; //error, characters must be in single quotes, not double quotes
short daysInOneHundredYears = 36500; //error, 36500 is larger than 32767, the largest possible short
float dollarToPeso = 18.15; //error, floats values should have the letter f at the end
float dollarToPeso = 18.15f; //valid
int numPages = 2,000; //error, numbers must not contain a comma
int numPages = 2000 //error, ; is required at the end

Note that it is an error to assign a value that is incompatible with the type of variable. In the example above, numPages was an integer, but was given a value of 310.2, which is a double. Also, it is an error to assign a value that is outside the allowable range for that variable type. The daysInOneHundredYears variable was given a value of 36500, which is larger than the biggest short (32,767) and results in an error. Finally, notice that Java does not allow numbers to have commas. Instead, it is legal to use underscores _ to separate groups of digits in newer versions of Java (7+):

int numPagesInLibrary = 20_000_000; //legal in Java 7+

For now, this is a good starting point to using data types and variables in Java. There's more to understand about variables, such as literals, operators, and expressions, which will be convered in the next section.

Sunday, June 11, 2017

Using Eclipse IDE

In the last post, you learned how to create and run a basic Java application using a text editor and the command line. Although it's possible to create all Java applications with this approach, using an Integrated Development Environment (IDE) will greatly boost your productivity over using a standard text editor. IDEs have features such as automatic error checking, code formatting, syntax highlighting, integrated debugging, and auto complete. This makes writing Java programs much easier. In this post, we'll look at creating the Hello World program using an IDE.

The two main Java IDEs used in the industry are Eclipse and IntelliJ IDEA (also just called IntelliJ for short). IntelliJ has a free community edition and a paid ultimate edition, which requires a yearly fee of about $300-$500 USD. The community edition doesn't include a lot of the features in the ultimate edition, which is too expensive for most people. We are going to use Eclipse throughout this series. IntelliJ Ultimate includes a free trial version on its website https://www.jetbrains.com/idea/download if you're interested in trying it out. There are also a few other Java IDEs, such as NetBeans and BlueJ, which are less widely used.

To install Eclipse, go to the downloads page https://www.eclipse.org/downloads, and click on the "Download Packages" link as shown in the following screenshot:



The current version is Eclipse Neon, but new versions are released every year in June. Eclipse Oxygen is scheduled for release in late June, 2017. After going to the Download Packages page, you should see options such as these:



The two options of interest are Eclipse IDE for Java EE Developers and Eclipse IDE for Java Developers. The Java EE Developers version has everything in the Java Developers download, plus a few features for writing web applications. Either version should be sufficient for this series. To launch eclipse, simply unzip the folder that was downloaded and double-click on the eclipse program (icon with a purple circle). You may want to create a desktop shortcut for easy access in the future.

After running eclipse, you should see a prompt asking where you want to create the workspace. A workspace is a group of related projects that you are working on. For very large applications, you may need multiple workspaces, but usually one is enough. The following screenshot shows the directory for a sample workspace:



I recommend using a location such as "C:\Users\[your username]\workspace" on Windows and "/Users/[your username]/workspace" on Mac or Linux. It's a matter of preference, but what's important is that it's easy to remember where the workspace is. You'll also see a checkbox that says "Use this as the default and do not ask again". I recommend checking that so you don't see workspace prompts when launching Eclipse in the future.

After the workspace is set up, you'll be taken to the welcome page, as shown in the following screenshot:



The welcome page has several options to create a new project, update settings, etc. You can do all of this from the main page, so don't select those options. You'll also see a checkbox that says something like "Always show Welcome page at startup". I recommend unchecking that, so you don't see the startup page every time you launch Eclipse. To exit out of the welcome page, click on the "x" on the welcome tab at the top of the screen. After a moment, you should see the main workspace view, similar to the following:



Don't worry if the layout is a little different on your machine. I recommend having the Package Explorer view on the left, the Outline view on the right, and the Problems and Console view on the bottom of the screen. If you don't see those, you can select Window -> Show View -> Console/Package Explorer/Outline from the top menu, and drag that view over to the correct location on the screen. There are dozens of different views in Eclipse, and it's possible that you don't see the view you want on the list. In that case, you can select Window -> Show View -> Other, and then type the name of the view you want in the search box. In Eclipse, a view is simply a window or tab that you can use to perform various actions. Package explorer allows you to see the structure of the project, outline allows you to see the class structure, and console allows you to view the output from your program. We'll take a deeper look at views and other Eclipse options in a future post. For now, it's time to create our first program in Eclipse.

To do this, select File -> New -> Java Project from the main menu. You should see a dialog similar to the following:



Projects are used to organize different programs within your workspace. In this case, we want to create a project that we can use to learn Java basics. Under project name, type "JavaBasics". Project names should start with an uppercase letter, and must not contain spaces. You can keep the "Use Default Location" option checked, which will create the project underneath your workspace. For the JRE section, select the first radio button. You should see a version such as JavaSE-1.8 in the dropdown. Leave the Project layout and Working sets sections with the default values and click Finish. After a moment, you should see the JavaBasics project display in the Package Explorer view. Next, it is time to create our HelloWorld class.

Click on the JavaBasics project in the Package Explorer view, and select File -> New -> Class from the main menu. This will bring up the following dialog:



The source folder should be set to JavaBasics/src by default. That is the directory where the class will go under. By convention, Java source files go under the src folder. Leave the package option blank, and ignore the warning saying "The use of the default package is discouraged". We will learn about packages in a later post, so we will keep things simple and leave the package blank, even though it's not good practice. Under Name, enter "HelloWorld". This is the name of the class we are creating. Also, make sure that the public static void main (String[] args) method stub is checked and the others are not. This is important, as it allows Eclipse to automatically generate the main method, which saves us time and helps us avoid errors if we forget to add that method. Finally in the main view, we should see a HelloWorld.java tab with code similar to the following:




As we can see, the HelloWorld class and main method was automatically generated for us. How convenient! The "//TODO Auto-generated method stub" line tells us that we can put our code inside of there. The "//" character sequence is used to start a comment, which is a human-readable description that gets ignored by the Java compiler. This can be useful for other developers to understand your thought process when writing a piece of code. I recommend deleting this comment, because other developers should already know that Eclipse can automatically generate methods.

Another important feature that Eclipse provides is called autocomplete (also called intellisense). As the name suggests, Eclipse will try to give helpful guesses about what you are typing to save effort. For example, the command System.out.println("[some message]") is used in many programs to display a message on the console. Instead of having to type System.out.println each time, you can simply type sysout and hit "command + space". This will cause eclipse to automatically generate the System.Out.Println() command. Try this out by typing sysout on a blank line inside the main method, and check that Eclipse generates the full System.Out.Println() output. Very convenient! In case there are multiple options matching what you typed, Eclipse will display a dropdown with the list of possible autocomplete options. For example, try typing syso and hitting "command + space". You should see a popup similar to the following:



Simply use the up and down arrow keys to select the right option and press enter for Eclipse to do the autocomplete. In this case, we want to select the sysout option, which should be the first one in the list.

To finish our HelloWorld program, enter some System.Out.Println() statements in the main method. You should have something similar to this:

public class HelloWorld {

public static void main(String[] args) {
System.out.println("Hello World!");
System.out.println("This is my first program from Eclipse!");
}

}

To run the program, expand the JavaBasics project in the Package Explorer view, and expand the src and default package entries. Right click on the HelloWorld.java class and select Run As -> Java Application fromt the menu. If everything goes well, you should see this output in the console view:



Congratulations! You have created your first Java program from Eclipse. We've barely scratched the surface on what the Eclipse IDE can do, but we can see already see the autocomplete and code autogeneration functionality saves us time and effort. One last feature I want to point out in this post is that Eclipse can automatically detect errors and tell us what is going wrong. For example, try deleting the ";" at the end of the System.out.println("Hello World!"); line. Eclipse will underline the mistake in red squiggly line, and we can hover over that (or the red X at the beginning of the line) to see what the error is:



In this case, eclipse tells us that ";" is needed to complete block statements. You'll get better at identifying and understanding what these error messages mean as you continue to write more programs. After adding the ";" back, we can see that the error message goes away, and we're able to run our program as before. We have covered a lot of ground in this post on how to install Eclispe and run our first application. It's time to start learning about basic Java programming in the next section!

Wednesday, May 31, 2017

First Java Program

With the Java JDK installed from the last post, we are finally ready to create our first Java application. The only other thing we'll need is a text editor, which comes preinstalled on every major operating system. Windows comes with Notepad, Macs come with TextEdit, and Linux comes with a variety of editors, such as Vim, Emacs, and Nano. While these editors are a great starting point, they have some limitations. Notepad, TextEdit, and Nano don't come with many features needed for everyday programming. Vim and Emacs have a fairly steep learning curve for new users. You may want to try out a programming text editor, such as Visual Studio Code, Sublime Text, or Atom. A programming text editor is specifically designed for writing software applications, and it comes with useful features such as syntax highlighting, line numbering, easy file navigation, etc. I may give a detailed comparison of the different programming text editors in a future post, but for now try installing them and viewing the online documentation if you're interested. These editors are also good when using other programming languages, such as JavaScript, so you will be able to learning a valuable skill.

Once you've chosen your editor, we're ready to begin writing our first Java program! First, create a new directory, which will contain all of our Java code. I called mine "Code" for simplicity. Next, create a "HelloWorld" directory inside of the one you just created. Hello World is the name of a simple program used when learning a new programming language. The goal is to simply print the words "Hello World" to the console. If you're interested, check out http://wiki.c2.com/?HelloWorldInManyProgrammingLanguages to see the HelloWorld program in other languages. Now, create a file called HelloWorld.java inside the HelloWorld directory. Java programs are just text documents with the extension .java at the end. The following diagram shows the directories and HelloWorld.java file that I created on my Mac:



I created a subfolder called "JavaSamplePrograms" to hold these tutorial programs. Don't worry about the HelloWorld.class file for now, we'll come to that in a minute. Once the HelloWorld.java file is created, open it in a text editor and copy the following code into there:

public class HelloWorld
{
    public static void main(String[] args)
    {
        System.out.println("Hello World!");
        System.out.println("This is my first program!");
    }
}

This may look a bit overwhelming at first, but we'll take a deeper look at the program in a minute. And by the end of the next few posts, you'll have a good understanding of the basic concepts behind this program.

To run this program, open up a command line and navigate to the directory where you created the HelloWorld.java file. This can be accomplished by using a command such as "cd C:/Code/JavaSamplePrograms/HelloWorld" on a Windows machine. Now type "javac HelloWorld.java" and hit enter. If everything goes well, you should now see the HelloWorld.class file in the directory. This is the bytecode that our Java program got compiled into. If you get an error that the javac command wasn't found, that means the jdk wasn't installed properly. Check the previous post for details, or try searching for "javac command not found" online. If you get an error saying that something is wrong in the program, double check that everything was copied exactly as it appears above. Finally, we need to run the compiled HelloWorld.class file. To do this, type "java HelloWorld" and press enter. You should not enter "java HelloWorld.class", as the .class portion is implicit. If you do, you will get an error. After running the HelloWorld program, you should see the following output on the console:

Hello World!
This is my first program!

The following diagram shows how I created and ran the HelloWorld program on my computer. The output of the programming is outlined in red:



Congratulations! You have successfully compiled and ran your first java program. Although this seems like a lot of steps for a first program, there are tools to simplify creating Java applications. The main tool is called an Integrated Development Environment (IDE), which has a nice code editor, file explorer, console for running programs, etc. We'll take a detailed look at using an IDE and how it makes programming easier in the next post.

Now that you created your first program, let's take a brief look at how it works. This will be explained in detail over the course of the next few posts, so don't worry if things aren't really clear at this point. The first line:

public class HelloWorld

Says that we are creating a new Java class called HelloWorld. Java is an object oriented programming language, and the way we define these objects is by creating classes. We'll take a much deeper look at classes in a later section. For now, just know that nearly all Java files that you create will contain a class. The next line is an "{", also called an opening curly brace. This punctuation is used to start a block of code, which is a section of code that serves a single purpose. In this case, the "{" says that everything inside of it is part of the HelloWorld class. All "{" characters must have a matching "}" closing brace character that says where the block ends. In this case, the last line of the program closes out the block. It is an error to open a block using "{" but forgetting to close it using "}". IDEs help by automatically adding a closing brace every time an opening brace is entered.

The third line of the programming is the most overwhelming:

public static void main(String[] args)

Is used to add something known as a method. Methods are used inside of a class to do something, such as adding two numbers, displaying some text on the console, or predicting who will win the next Super Bowl. Any piece of functionality you can think of will usually go inside of a method. The method must also be given a name. In this case the name of the method is main. You should usually give the method a descriptive name, such as addTwoNumbers or predictSuperBowlOutcome, so it is clear to other programmers what the method does. The main method is special, as it tells the Java Runtime Environment (JRE) that this is the entry point of the application. In other words, when you run a Java program, it looks for a method named main and runs all of the code inside of that. Your program must include a main method, or else the JRE won't know how to start the program, and you'll get an error. There's a lot else going on in this line, but for now just know that the main method is what gets run when you start a Java program. In the first few posts, we'll write everything inside the main method, and later on we'll learn how to structure our code by adding our own custom classes and methods.

Line 4 is another opening brace, which says that everything inside of it belongs to the main method. Line 7 closes out the main method. Lines 5 and 6 are where all of the action takes place. They both do similar things, so let's take a look at line 5:

System.out.println("Hello World!");

Is used to display the text "Hello World!" without the quotes on the console. System.out.println is a method provided to us by the Java language to display text on the screen. Without that method, we'd have to understand how our operating system works and how to get it do display text on the console. That would be pretty difficult to do. This is one way object oriented programming makes our code easier to understand. We don't need to know all of the underlying details about how a method works. In this case, we know the what (print "Hello World!" to the console), but not the how (call the operating system and get it to display the letters H, e, l, l, o, etc). So for now, all you need to know is that in order to display some text on the console, you call System.out.println(), and give it the text to display inside of quotes.

With that, we have a high level understanding of how our first program works. Don't worry if a lot of it is still unclear at this point. Java programs have a lot of terminology that we need to understand before we really know what's going on. These concepts will become much clearer in the next posts. Also, there are tools that make it much easier to write and run Java programs. We'll take a look at one of these tools in the next post and see how it makes our job easier.

Saturday, May 27, 2017

Java Installation

Before we can begin writing our first Java program, we need to make sure that the Java Development Kit (JDK) is installed on our machine. Remember from the last blog post that there are two main applications that are responsible for running Java code, the JDK and the JRE. The JDK takes the Java program that we write and translates it into an intermediate language called bytecode. The Java Runtime Environment (JRE) takes bytecode and converts it to machine language, which is run on the computer. Since the JDK includes the JRE, we only need to install the JDK. There are several ways to install the Java Development Kit, which are described below.

Method One - Install JDK from Oracle Downloads Page


The most common method used to install the JDK is by going to the Oracle downloads page. To do this, search for "download jdk" and select the first result, which should take you to the Oracle JDK download page. Oracle is the company that maintains the Java programming language. You should see a section on the page similar to the following after going there:



Please note that Oracle recently changed the installer page, and you may see a section like this:



In this case, click the "Java Download" button to get redirected to the actual installer page.

Before downloading the JDK, you must select the "Accept License Agreement" button and choose the version of the installer for your operating system. The x86 and x64 options are based on whether your machine is 32 bit (x86) or 64 bit (x64). Most modern computers are 64 bit, so you should be able to select the x64 option. If you are using Linux, you will need to unzip the installer using a command such as "tar -xvzf jdk-8u131-linux-x64.tar.gz". For Mac and Windows users, simply run the .dmg or .exe file that was downloaded. After running the installer, you should see an installation wizard similar to the following:



Follow the instructions on the wizard to complete the installation. If you're using Windows, you will also want to update the PATH environment variable, so you're able to call java from the command line. The Oracle documentation gives the steps needed to do this:

1. Click Start, then Control Panel, then System.

2. Click Advanced, then Environment Variables.

3. Add the location of the bin folder of the JDK installation for the PATH variable in System Variables. The following is a typical value for the PATH variable:

C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.8.131\bin

The entries in the PATH variable are separated by a semicolon, so you can add ";C:\Program Files\Java\jdk1.8.131\bin" to the existing entries. Make sure that you replace jdk1.8.131 with the version that you installed.

Now that the JDK is set up and configured, you can test whether the installation was successful by using the terminal (also called command line). The terminal can be opened on Windows by pressing "Windows Key + R" and typing cmd. The terminal is under Applications -> Utilities on Mac. In both cases, you may want to create a shortcut for easy access. Once the terminal is open, type "java -version" without the quotes and press enter. You can also try "javac -version" to see if the java compiler was installed correctly. You should see output similar to the following:



If you encounter any errors during the setup, double check that all of the steps were followed, or try watching a video about the JDK installation for your operating system.

Method Two - Install JDK using SDKMAN


SDKMAN stands for Software Development Kit Manager, and it is used to easily install tools needed for Java programming. SDKMAN works well on Mac and Linux computers. For Windows computers, there is a Powershell version available, but it may be easier to install Java using Method One above. To install SDKMAN, go to the installation page at http://sdkman.io/install.html, and follow the instructions. Once the installation is complete, you can check out the Usage page to get a feel for the basic commands. In particular, the command "sdk install java" is used to get the latest version of the JDK and "sdk current java" is used to verify that the installation was successful. Pretty easy, huh! The following screenshots show how the installation process might look using SDKMAN:





In my case, I already had JDK version 1.8.0_101 installed, so SDKMAN updated it to the latest version 1.8.0_131.

At this point you should have the JDK installed on your computer. We're all set to begin writing our first Java program in the next post!

Saturday, May 20, 2017

Java History and Motivation

Before we can start writing Java programs, it is important to understand the history and theory of programming. A computer program is simply a list of instructions that tells the computer how to accomplish a certain task. As an analogy, if our task is to drive from Chicago to New York City, we would break that down into a list of directions (start the car, pull out of the driveway, turn left onto 10th Street, turn right onto highway 90, etc). Although many routes exist from Chicago to New York City, we are only interested in ones that are fast and easy to follow. Similarly, there are many different ways to write a computer program, but the best programmers will write programs that are fast, easy to understand, etc.

All computer programs end up as a sequence of bits (0 or 1). Computers use electricity as the way to transport information. If the voltage on an electrical switch is greater than a certain value (usually 5 volts), then the bit evaluates to a 1. Otherwise, if the voltage is less than 5 volts, then the bit evaluates to 0. The image below shows an early computer, where programs are entered by pressing the red and purple switches at the bottom of the console. Each switch represents a bit with the value 0 (unpressed) or 1 (pressed):



The list of 0s and 1s that a computer is able to run is known as machine language. Although it is still possible to write computer programs using machine language, programs become very difficult to understand if they are more than a few lines long. The solution is to write programs in a human readable language (such as Java) and use a tool called a compiler to transform the program into a sequence of 0s and 1s to be read by the computer. We'll see many examples later on about what a human readable program looks like.

C was the first mainstream programming language that allowed people to write high level programs that got transformed into machine language. It was created by Dennis Ritchie in the 1970s and is still one of the most popular programming languages today, along with Java and a few others. Although C was a major step forward in writing good computer programs, it had several drawbacks. One of the biggest problems was too much complexity for large programs. C is a structured programming language, which is a type of programming language that focuses on the instructions needed to complete a task. For example, if we want to build a house, a list of tasks such as the following could be used:

1. Pick a location where the house can be built.
2. Survey the land and make sure that the house can be built there.
3. Purchase the property where the house will be built.
4. Get the necessary permits, get insurance, create a budget.
5. Create a blueprint detailing what the house will look like.
6. Build the house, including the foundation, walls, roof, electricity, plumbing, flooring, etc.

The are many more tasks than what's listed, and it's very difficult to understand everything on your own. How do I lay a brick foundation? How do I install the roof? How do I wire up the electrical outlets and install plumbing? Writing a program like this using structured programming is going to be difficult to follow. The solution is to just understand the high level details involved and how they fit together. I can hire an electrician to take care of the wiring, and I can hire a plumber to take care of the plumbing. The idea of breaking down a task into high level details is known as object oriented programming. Using this approach, I can focus on the parts of building a house that I need to (let's say creating the blueprint and financing the house) and let other people focus on the details of how the electricity and plumbing will work. We will take a detailed look at object oriented programming in a later post.

C++ (pronounced C plus plus) was the first programming language that popularized the object oriented programming model. It was created in 1979 by Bjarne Stroustrup, and it became popular in the 1980's. Although this was another major step forward in programming languages, there were still some major drawbacks. C++ was written as an extension to the C programming language, which made it easy to fall back on structured programming when writing applications. Many C++ programmers came from a C background, and brought bad habits from C over to C++, even though there were now better ways to do the same thing. Another major problem of C++ was that it wasn't platform independent. This means that once a program is compiled into machine code, it will not run on other computers. A platform is determined by the type of device running the program and the operating system (there are a few other factors, but those are the main ones). So, if I write a program in C++ on my Mac, the machine code will not run on a Windows computer or Android phone. This is a big limitation today, because web development is very popular, and being able to run the same program on different devices without having to make changes is a great productivity boost.

This brings us to Java. Java was created in 1996 by James Gosling to overcome many of the deficiencies in C and C++. The problem of platform independence was solved by compiling the code into an intermediate language, known as bytecode. Bytecode is a list of simple instructions that can be further compiled into machine code and run by the computer. The application that takes bytecode and runs it as machine code is known as the Java Runtime Environment (JRE). Since almost all computers and devices come with the JRE preinstalled, you can share your program's bytecode with someone else using another device, and it will run without any changes. The application that takes a Java program and coverts it into bytecode is known as the Java Development Kit (JDK). The JDK includes the JRE, and allows a programmer to write an application, convert it to bytecode, and run the corresponding machine code all in one go. The diagram below shows how a java program is converted to bytecode and run as machine code:


Don't worry about the difference between bytecode compiler and bytecode interpreter in the diagram. The point is that the bytecode will be transformed into machine code, which is run by the computer. Another benefit of compiling a java program to bytecode is that a large application can be written using many programming languages. As long as a programming language can be compiled to bytecode, it will run on the JRE. Many languages, including Python, Ruby and Scala, can be compiled to bytecode. This allows multiple teams working on the same project to use their favorite programming language and integrate it with code from another team in a different language.

Just as Java is a large step forward from C++, there are a few shortcomings in the Java language as well. I will show some of those in later posts. Many languages, such as C# (pronounced C sharp), were inspired by Java and created to address some of the deficiencies. Despite those attempts, Java has remained a reliable and powerful language, and is the programming language used most in the industry. Now that you understand the history behind Java, it's time to prepare for writing our first program in the next post!

Monday, May 15, 2017

Welcome and Motivation


Welcome to Java trails, your guide to becoming a better software developer! The goal of this blog is to provide a comprehensive overview of the Java programming language. It is an exciting time to learn Java programming. Java is currently the most popular programming language, and it is the one most used in the industry. In fact, the TIOBE Index (https://www.tiobe.com/tiobe-index/), which measures programming language usage, lists Java as having one of the top two spots since before 2000. Learning Java will provide you with a solid programming foundation that will be useful for years to come. You will also have a marketable skill that will allow you to find highly sought after jobs in the software development industry. In addition, you will learn how to write clean, elegant code, which can be applied to other languages. This blog can be used by both students who want to learn how to write Java programs, as well as professionals who are learning Java for the first time or coming from other languages.

I will start by focusing on core Java programming concepts (assignments, flow control, object oriented programming, collections, core APIs, etc). Along the way, you will learn the tools that software developers use to maximize productivity and write the best code. Once you have a solid understanding of the basic programming techniques, you will be ready to move on to more advanced topics such as creating Java web applications.

Why another blog?


You may be wondering why another blog on Java programming is needed when there are many other tutorials and books on the subject. I found that most of the existing Java resources were lacking in one or more areas:

1. Depth. Most blogs contain enough information to get a basic understanding of Java, but they do not provide enough coverage of the language (threads, garbage collection, string parsing, etc.) that allows you to really understand what is going on and write professional code. My goal is to provide a solid foundation of all aspects of the Java programming language and not gloss over important concepts.

2. Clarity. Many academic resources and programming references are notorious for being too complex, when the ideas they are explaining are actually much simpler. Consider this paragraph from the Java Language Specification:

The floating-point types are float and double, which are conceptually associated with the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations as specified in IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985 (IEEE, New York).

The IEEE 754 standard includes not only positive and negative numbers that consist of a sign and magnitude, but also positive and negative zeros, positive and negative infinities, and special Not-a-Number values (hereafter abbreviated NaN). A NaN value is used to represent the result of certain invalid operations such as dividing zero by zero. NaN constants of both float and double type are predefined as Float.NaN and Double.NaN.

From this description, you might understand that float and double variables are used to represent positive and negative decimal numbers. But I bet you had to look hard to figure that out. Having a technical specification is good for academics and language implementers who need precise definitions, but it is a poor resource for people learning a new language for the first time. I am able to explain complex subjects in a focused, simple style that will make the learning process much easier for you.

3. Up to date information. Java has been around since the early 1990s, and there are tons of books and tutorials that you can find on the subject. A common problem is that blogs do not get updated, and books get out of date as Java continues to add new features and mature as a language. As a result, you may think that you understand certain concepts only to find out that there are newer and better ways to do the same thing. My goal is to keep you informed about the latest changes in the language and update existing articles as new features get added.

I believe that learning Java programming will be a fun and rewarding experience given the proper instruction. So what are you waiting for? Let's get started on our journey to becoming a great Java developer!