Information Technology & Game Design Year 1

This page was created for first year AOITGD students from the Woonsocket Area Career & Technical Center.

Introduction to Java: "Hello World"

Untitled-4
Software: Java
Platform: Windows

Tutorial Description: 
In this tutorial, we'll be learning about what exactly Java is and how to set up the Eclipse IDE. We'll also be taking a look at the primary Java constructs, as well as coding a basic "Hello World" application
.

Introduction

Welcome to the exciting world of Java! Created by Sun Microsystems in 1995, Java is an object-oriented programming language that can be used to make all sorts of awesome things: interactive systems on Blu-ray Discs, all sorts of chat clients — even video games can be made from Java! Before we get started, let's address some common concerns.
1. "But programming is so hard!"
It's okay! Although programming can be difficult, we're going to learn all about Java and object-oriented programming in a way that's fun, easy, and accessible. We're going to keep things interesting with awesome projects, and learn to code not only in a way that will work for the specific examples in these tutorials, but also enable us to think on our feet and get creative quickly!
2. "So, I'm going to learn JavaScript, right?"
Actually, in these tutorials we're going to be learning about Java, which is not the same asJavaScript. Although JavaScript is also super cool, it's mostly used in web development or as a supplemental scripting tool for other existing programs like Adobe Photoshop. Java and JavaScript do use a lot of the same naming conventions, but that's where most of their similarity ends. It's okay if you don't understand any of this — we'll get to it eventually. For now, just know that in these tutorials we'll be working in Java and not JavaScript.
3. "What are the benefits of learning Java over some other language?"
There are literally thousands of programming languages to choose from, and almost everyone who programs has one or two programming languages that they prefer over any other. It doesn't really matter what language you choose to learn first, but there are definitely benefits to learning Java. First, Java is one of the most popular programming languages on the planet according to the TIOBE Index (which monitors activity in the programming community on a monthly basis). Second, because so many programming languages are Java-based, learning Java now means that you'll have a much easier time learning many other programming languages later. Third, Java is one of the most frequently requested programming skills, so it's going to look really good on your resume for everything from college applications to job interviews in the future.
4. "So where do I get started then?"
We're going to get coding really soon, but the first thing that we need to do is get an IDE installed on your computer. IDE stands for Internal Development Environment. The IDE is where you're going to be writing and compiling all of your code. Eventually, you'll even be able to use your IDE to build standalone Java applications. There are many different IDEs to choose from, includingEclipse and NetBeans. These tutorials will be written using Eclipse for OSX, but you can work on Java in any operating system (Mac, Windows, Linux, etc.) and in any IDE that you feel comfortable with. Just keep in mind that all of the screencaps that you'll see are from Eclipse, which make it the recommended IDE for these tutorials. If you should choose to work in another IDE, the same code will work just fine, but the IDE interface could look quite different.
You're also going to want a Java Runtime Environment installed. Eclipse has a table on their website that allows you to figure out which Java Runtime Environments you can install based on your operating system. Please consult the table (here) and then do a Google search to find the appropriate software, ensuring that you have the correct JRE (Java Runtime Environment) installed. Then, take a moment to download and install Eclipse (or your IDE of choice) and continue.

Setting up Your Workspace in Eclipse

Once you've finished installing Eclipse, it's time to open it up and establish your primary workspace. The workspace in Eclipse is a directory where all of your projects will be saved. There is a default workspace location that Eclipse will create, or you can set up another directory by clicking on the Browse button.
Click to Enlarge
Click image to enlarge
Once your workspace directory is established, click on the checkbox next to "Use this as the default and do not ask again" (circled), and then click on the OK button in the lower right corner of the window. Clicking the OK button will cause Eclipse to begin setting up your workspace and show this super-sweet loading screen:

Anatomy of Eclipse: A Quick Guide

Once Eclipse has loaded, you should immediately be dropped into your new workspace. If it's your first time opening Eclipse, you may also be presented with the New Project wizard, as shown below.
Although the New Project wizard in most programs is useful, for now it's just going to get in the way. Click the Cancel button, and then continue.
If, upon opening Eclipse, you're presented with a slew of icons, find the button that says "Go to the Workbench" and click it.
Click to Enlarge
Click image to enlarge
And now it's time for a quick Eclipse anatomy lesson! Let's look at the workspace from left to right. First, on the far left, we've got the Package Explorer. You'll notice that although there is a handful of packages in the image above, the Package Explorer in your version of Eclipse doesn't have any. That's because we haven't made them yet, but we'll start on that soon. Next, in the top center portion of the workspace, you'll see the Project (class) Editor. This is where we're going to be entering our code. To the right of the editor window, you'll see the Outliner. A Java project can be made up of any number of classes and subclasses. The subclasses can even have subclasses of their own! This structure of classes, subclasses, sub-subclasses, and sub-sub-subclasses is set up in a hierarchy, the structure of which will be shown in the Outliner. We'll be discussing hierarchies further in later tutorials, so for now this is a fine amount of information to grasp.
Below the editor window and Outliner you'll find a series of tabs — Problems, Javadoc, Declaration, and Console. For now we'll be working mostly in the Console tab, which we'll be using for text input and debugging, and the Problems tab, which we can use for debugging in a different way.
Click to Enlarge
Click image to enlarge
If you don't see the same tabs as shown in the previous images, don't worry! You can open any of the tabs that by using the following click pattern from the Window menu:
Window → Show View → Whatever tab you'd like to open

A Quick Note about Errors

Before we get started, lets talk for a moment about errors. We are often taught from a very young age that errors, or making mistakes at all, is a huge problem. We are trained to avoid mistakes, or if we've done something incorrectly, we should be embarrassed and feel like Godzilla just stomped on the buildings of our hopes and dreams.
Within programming, you're going to have errors. Sometimes it'll be one error, sometimes it'll seem like a million. It's ultra-important, however, not to let yourself get discouraged. Errors are just part of the programmer's way of life. We learn from them. They make us better at what we do. Sometimes you'll even see an error that will cause you to rethink an idea completely and make it a thousand times better! Just remember: errors happen, but it's nothing to worry about — it's just a part of the process.

Creating a New Java Project

In order to start coding, we're going to need to create a new Java project. To do this, click on the small caret in the upper-left corner of the workspace, marked below.
Click to Enlarge
Click image to enlarge
The menu that drops down when you click on the caret is the "new" menu. Each of the items in the "new" menu is something that we'll eventually be able use to create new programs. Right now, we're going to be creating a new Java project, so click on the topmost menu item, "Java Project," as circled above.
Tips & Tricks
Remember the New Project wizard that we talked about earlier? You can access it by clicking on the button directly to the left of the caret that you clicked to get the "new" drop-down menu.

Click to Enlarge
Click image to enlarge
Next, a prompt will pop up providing you with some options for your new project. Because this is going to be a simple program, all we need to do is give our project a name and then click the Finish button. Today we'll be writing a simple "Hello, World!" program. Name your project something like "Hello World" and then click the Finish button to continue. The pop-up will take a moment to build your package, and then you'll be able to see your new package in the Explorer, as shown below:
Tips & Tricks
You can open up your package so that it looks like the above image by clicking on the triangular drop-down buttons to the left of the package name.

Creating A New Class

Next, we're going to need to create a new class. A Java package is made up of one or more classes that contain methods (functions or commands). These methods are made up of statements: sentences in the Java language that provide parameters and expressions and dictate how a method is executed.
To create a new class, first ensure that the Hello World package is selected, then click on the "new" dropdown menu and create a new class.
Click to Enlarge
Click image to enlarge
This will cause the New Class wizard to pop up, as shown in the image below.
There are lots of things that we can do with the New Class wizard, but at this stage it's actually better not to use them — selecting any of these isn't going to save much time right now, and you might miss out on some core concepts if you skip straight to the shortcuts. We'll get into an explanation for each of these as we need them.
As it stands there are three things that we need to do before we finish creating our class. First, let's name our package. Eclipse discourages you from using the default package, so let's name our package "helloworld." It may seem strange to name your package in this way, but Java uses something known as naming conventions.

Naming Conventions

Naming conventions are used to make your code easier to read, and are structured based on the casing (e.g., uppercase, lowercase) that you use. There are four standard types of naming conventions:
  1. lowercase: all letters are lowercase
  2. UPPERCASE: all letters are uppercase
  3. CamelCasing: the first letter of each word is uppercase, all other letters are lowercase.
  4. mixedCasing: the first word is all lowercase, and the first letter of each consecutive word is uppercase.
There are also specific naming conventions for specific identifier types (like packages and classes). We'll go through the identifier naming conventions as we need them. For now, let's discuss the naming conventions required for class and package naming.
  • Packages: packages should have names that are all lowercase.
  • Classes: classes should be named using CamelCasing.
Now it's time to complete the setup of our new class. Name your class HelloWorld, ensure that all of the check boxes below the class name field are empty, and click the Finish button to create your class. Once constructed, your completed class will look like this in the workspace:
Click to Enlarge
Click image to enlarge

Coding a HelloWorld Program

When learning a new programming language, coding "Hello World" is a rite of passage. In fact, it's almost always the case that your first program in any programming language is some variation on HelloWorld. We're going to do this by adding the following to our new class in between the existing curly brackets:
Source Code
  • public static void main (String [] args){
  • System.out.println("Hello, World!");
  • }
The code that we'll be adding functions as follows:
  • public static void main (String [] args) — this is known as the main method. There is almost always a main method in at least one class of a Java package.
  • System.out.println("Hello, World!"); — this tells the compiler to print the text "Hello, World!") to the console as output.
Your completed code will look like the code in the following image:
Click to Enlarge
Click image to enlarge
That's all the code we need for our Hello World program! Now it's time to run it! Click on the Play button circled in the image below to compile and run your program, and you'll see the typed text "Hello, World!" printed on the console.
Click to Enlarge
Click image to enlarge
Congratulations! You've just taken your first step towards becoming an awesome programmer!

Want More?

Challenge Yourself!

Now that we've made a program that prints "Hello, World" to the console, see if you can get it to print something else, like "Hello, AwesomeTime!" or "I'm the coolest ever!"

What's Next?

In the next tutorial, we'll be further discussing our "Hello World" program, and learning more about how to work with the basic Java constructs, as well as how to make notes and turn code on and off with comments! We'll also and learn how to declare variables and code a program that will solve basic math problems.