Now Reading
[Geekery] Programming 101: Eclipse & Java Crash Course

[Geekery] Programming 101: Eclipse & Java Crash Course

We’re going to cover quite a bit today, so be sure to carve out some time for this one.  Also, it’s best if you follow along, so if you don’t already have Eclipse installed, see last week‘s article for a link to the download page.

My goal today is to show you how to get started with Eclipse, and then show you the building-blocks of programming: variables, conditional statements and for loops.  These three things form the basic tool set that you’ll use in every program from here on out.  Variables are how you store and access information.  Conditional statements are how you control the program and allow it to make decisions.  For loops are the most basic way to run through a set of data points, running calculations or making decisions along the way.

Let’s get to it

Go ahead and fire up Eclipse.  You’ll see a splash screen, and then you’ll get a dialog box:


This dialog wants to know where to store your code.  Pick a different location if you don’t like the default provided.  Select “Use this as the default and do not ask again”, and then click OK.

Once Eclipse loads, Click File -> New -> Java Project.  This is how you’ll make a new project every time you want to make a new program.


You’ll get the Java Project dialog box:


Name the project something simple (I chose “Example”) and leave everything else at the default.  Click Finish to create the project.

You’ll notice a new folder in the upper left corner.  This folder is your new project.  Expand it by clicking the arrow, and you’ll see a yellow box with a crosshairs named “src” and a stack of books named “JRE System Library”.  The box with the crosshairs is the symbol for a Java package.  Packages are essentially folders, are are solely for organizing your files of code.  The JRE Library contains all the pre-made Java stuff that you can use in your programs.  We won’t be doing anything with these right now, but we will later on.

Right-click the src package, select New -> Class.


You’ll get the Java Class dialog.  Name the class something (I’ve chosen “HelloWorld”), select the box labeled “public static void main(String[] args)” and click Finish.


You’ve now created a Java class, and you should now see some Java code:


Replace the line “//TODO Auto-generated method stub” with “System.out.println(“Hello, World!”);”.


In the menu at the top, locate the green play arrow, and click it.


You’ll get a dialog.  Select the box next to “Always save resources before launching”.  Click OK.  This will configure Eclipse to always save your files before compiling and running them.  This means you won’t have to remember to click Save or do Ctrl+s before checking the results of code changes.


You should notice the phrase “Hello, World!” appear in the tab at the bottom of the window labeled “Console”.



You have just written, compiled and run your first program!

Now, let’s go over what the program in more detail.

Breaking it down

The first line of code is “public class HelloWorld{“.

public is a keyword used to dictate who and what can access things.  Public means that anything can see and access the item the keyword is attached to.

class is a part of Java code.  Java is an object-oriented programming language (OOP), which means that all java code lives inside a class.  Think of a class as a container for data, functions and other stuff.

You’ll notice that both of these words are bold and purple.  This is Eclipse’s way of showing us that these are Java keywords.

HelloWorld is the name of our class.  You can use any sequence of characters and numbers and underscores to name stuff, but you always have to start with a letter, never a number.  You can not name things the same thing as a keyword, ie, you can’t name a class “public”.  Also, classes start with a capital letter, by convention.  If you tried to name your class something that wasn’t capitalized, you probably got yelled at by Eclipse.  🙂

The curly bracket “{” designates that something has opened, in this case, the HelloWorld class.  These must always come in pairs: { and }.

The next line of code is “public static void main(String[] args){“.

This is the standard line that creates the main method, which is the method that gets executed when we run the code.  This method is where the code starts.

The last (important) line of code is “System.out.println(“Hello, World”);”

This is the standard method for printing data to the console, as evidenced by the fact that the string of characters we fed to it showed up in the Console when we ran our code.

Something more useful…

Let’s get our program to do something more interesting than just spit back a string.

First, we’ll declare some variables, then do some math, and finally print the result.  Java has several ways to store numbers, but the two common ones are an int and a double.  An integer is a whole number, no fractions allowed.  A double is a bigger version of a floating point number; a number that can (but doesn’t have to) utilize fractions of a number.


As you see, we get the result of 10 multiplied by 2.5: 25.  Also notice that we get “25.0”, not just “25”.  This is because math operations always use the bigger storage vessel of whatever is used in the operation.  Since doubles are more precise, when you multiply an integer and a double, you get a double as the result.

Conditional Statements (or how programs make decisions)

Ok, so we can do math, but what about making decisions.  How about having the code tell us which of the two variables is greater than the other?  This is done by a conditional statement.  This usually takes the form of an “if-else” type statement:


Since 10 is greater than 2.5, we see “x is greater than y” as our result that gets printed to the console.  Change up the values of x and y, and see how the result switches depending on how the two numbers relate to one another.  Also, take note of what happens when the two numbers are equal…

To catch the specific case when the two values are equal, you can ‘chain’ if-elses together into something like this:


Conditionals are great, but they’re rather pointless when your data is static and unchanging, like our hardcoded variables x and y.  Programs become hugely more useful when the data can be manipulated and stored, used and re-used.

Changing variable values

One way to alter a variable dynamically is to simply save it over itself, like in this statement: “x = x+1;”  That will turn x into a value one more than it was originally.  Of course, programmers are lazy, so there’s two shorthands for doing something like this:

  • “x++;” will increment your variable by one.  This is the equivalent to “x = x+1;”.
  • “x–;” will decrement your variable by one, and is equivalent to “x = x-1;”.

Here’s an example of these short hands in action:


For Loop

So, let’s say we wanted to see which numbers of the range 1-10 are even.  For this, we would use something called a for loop.  For loops are made of three parts: initialization, end test, and incrementation schedule.  I’ll point them out after I show you some code:


You’ll notice the for keyword, followed by what looks like a variable declaration: “int i = 1;”  That’s initialization.  The next bit should look like the inside of our conditional statements.  This test case is executed at the beginning of every loop, and if it evaluates to false, the loop will exit.  The last bit is the incrementation schedule.  Code that’s here gets executed at the end of every loop, and n this case, we’re incrementing i by one every loop.

The code inside the curly brackets will also get executed every loop.

The conditional statement “(i % 2) == 0” tests for i being an even number.  “%” is the modulo operator, so “i % 2” will return 1 or 0, based on what is remaining when the number i is divided by 2.  By putting the test in parenthesis, it will get executed before the rest of the statement, which tests if the result is equal to 0.

NOTE:  A single equal sign assigns value from righto left, when testing for equality between primitives, you use the double equal sign “==”.

So, our loop starts with i=1, then tests for i <= 10 (which is true because 1 is less than or equal to 10).  Then the inside of the loop executes.  1 divided by 2 has a remainder of 1, which is not equal to 0, so our conditional test fails, and the code inside the conditional statement is not executed.  At the end of the loop, i is incremented to 2.

The next time the loop executes, i=2, and the test is true (2 <= 10 is true).  However, when 2 is divided by two, the remainder is zero, and since 0 equals 0, the conditional statement evaluates to true, and the code within is executed.  The string is printed to the console, and at the end of the loop the incrementation schedule is executed and i becomes 3.

And so on and so forth.

 In conclusion…

You have Eclipse installed, and now know how to make a new project and create a class.

You’ve seen variables, conditional statements and for loops.  These form the foundation for every program.

Go forth and conquer the world.

View Comments (2)

Leave a Reply

Your email address will not be published.

Scroll To Top