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.


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.


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.


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.


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.


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.


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


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. Let's take a look at putting these concepts together in a sample application. I named the class VariableDeclarations. If you already created the HelloWorld class in Eclipse from the previous tutorial, you can right click on the class and select Refactor -> Rename, and then give the new name of the class. You can also create a new class inside the project if you'd like to keep the existing HelloWorld class as-is. Remember that for now, we're going to put everything inside the main method until we learn how to use classes and methods:

public class VariableDeclarations {
    public static void main(String[] args) {
        int numPages = 300; //valid
        double dollarToPeso = 18.15; //valid
        char letter = 'x'; //valid
        boolean isSunday = false; //valid
        float dollarToYen = 112.42f; //valid
        long $companyEarnings = 4_000_000_000L; //valid, but using $ in the variable name is discouraged, and the L at the end is required since 4_000_000_000 is larger than the biggest int 
        // float dollarToPeso = 18.15; //error, floats values should have the letter f at the end
        // Int numHolidays = 10; //error, java is case sensitive and data types must be lower case (int instead of Int)
        // boolean isSunday = true; //error, isSunday was already declared above, it cannot be declared again;
        // boolean isFriday = 0; //error, 0 is a number, not a boolean value
        // int numBooks = 310.2; //error, 310.2 is not an integer;
        // char anotherLetter = "x"; //error, characters must be in single quotes, not double quotes
        // short daysInOneHundredYears = 36500; //error, 36500 is larger than 32767, the largest possible short
        // int bookPublishedYear = 2,000; //error, numbers must not contain a comma
        // int numAuthors = 3 //error, ; is required at the end

Try removing the // at the beginning of the lines that are marked with error, and see what error messages are displayed from Eclipse. Notice that System.out.println can take different data types and output them to the console as Strings (text). We'll learn how that works in a later post. The output of the program is what we'd expect:


There's more to understand about variables, such as literals, operators, and expressions, which will be covered 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 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, 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 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 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!