Showing posts with label basics. Show all posts
Showing posts with label basics. Show all posts

Friday, November 14, 2014

Hooray for Boo..leans

I've mentioned booleans before, and it's critical to understand both boolean variables and boolean logic.  A great deal can be accomplished with booleans, for good or for ill.  You can make your programs sing or you can make yourself cry depending on how well you understand what you're doing.

I'm going to use the good old 'if' statement a lot in this post, but the same concepts apply to any conditional, such as while loops.

About the simplest possible boolean tests would be:

if (true) {

or

if (false) {

In fact, they're so simple that the java compiler should entirely remove either the test (in the first case) or the entire code block (in the second).  But logically, the first conditional should simply always execute whatever statement follows, and the second should just bypass the statement.

A more likely scenario would be this:

while(true) {

This is what is known as an infinite loop, which is handy when you mean it and devastating when you don't.  Create a while loop with a boolean condition that, due to some error, never switches to false, and you've got yourself a program that looks like it has 'locked up', when really it's happily executing the same thing over and over and over again.

Of course, most of the time you're going to be using logic, not constants, to determine how a conditional statement behaves.  This sort of thing is extremely common:

if ( someValue > 5 ) {

This expression, using the 'greater-than' conditional operator, evaluates to true if someValue is 6, or 22, or 25,101,793.  Just so long as it's more than five.  If it's five (or less) , it evaluates to false.  Say, that could come in handy...

Of course, you can also check for a value being less than another value with '<', or equal to another value with '=='.  You can get quickly more complex if you wish:

if ( value1 * 3 < value2 / 2 ) {

Is a perfectly valid expression.  Of course, you'd have to work out what values will do what, but it's not too conceptually difficult to understand that this will pass if value1 is 1 and value2 is 36, but fail if value1 is 1 and value2 is 2.

Boolean expressions can also be subjected to boolean operators, to evaluate more complex conditions.  For instance, let's say you've offering a discount to people who have been customers longer than five years AND have total orders of more than 1,000 dollars.  While you could write that as nested if statements:

if ( yearsAsCustomer > 5 ) {
    if ( totalOrders > 1000 ) {
        discountPercent = 1;
    }
}

(Note, I never said it was a GOOD discount)

You can simplify this to:

if ( yearsAsCustomer > 5 && totalOrders > 1000 ) {
    discountPercent = 1;
}

The '&&' operator joins two boolean values into one, and gives back true if and only if both of the values are true.

Boolean logic is generally summed up in a table like this:


OperatorValue1Value2Result
&&truetruetrue
&&falsetruefalse
&&truefalsefalse
&&falsefalsefalse


Note that last line:  && does NOT a check that both values are the same, it wants both values to be true before it gives up a true value.

The other operators are OR (||) and EQUALS (==).


OperatorValue1Value2Result
||truetruetrue
||falsetruetrue
||truefalsetrue
||falsefalsefalse

The OR operator is friendly.  As long as one of the operands is true, it gives you true.

OperatorValue1Value2Result
==truetruetrue
==falsetruefalse
==truefalsefalse
==falsefalsetrue
I think this one should be rather easy to understand.  If both operands have the same value, you get back that value.  Equals is a bit of a special case though, because you can use it with non-boolean operands as well.   (5 == 5) evaluates to true, as does (true == true).

So technically, you can do this (although my compiler warns me that I'm being silly when I do it, it reluctantly compiles and this can be run:

if ((5==5) == (true == true)) {
    System.out.println("Imagine that");
}

Note my use of parentheses to prioritize expressions.  I wanted to be absolutely sure that 5==5 and true == true both evaluated first, and I didn't want it to look like I was comparing 5 == true.  Now, the compiler follows standard rules for prioritizing mathematical operations, but extra parentheses can be very useful for we poor humans.

Expressions with booleans can be nested arbitrarily deep, although once you've got more than a few terms, they can be a bit difficult to work out.  Try to always give some thought to the next poor sod who has to modify your code, especially if that poor sod might be you again.

if ((isACustomer && (years > 10 || (years > 5 && sales > 1000))) || runningSpecialIncentives) {

Might be logically correct, and work just fine.  But do you want to add another condition to it first thing in the morning before you've had a cup of coffee?  Do you want to test it when someone is breathing down your neck?  This is probably a candidate for breaking up.  You can always create new booleans to act as placeholders:

boolean meetsQuotas = years > 10 || (years > 5 && sales > 1000);

Which would give the more readable:

if ((isACustomer && meetsQuotas) || runningSpecialIncentives ) {

Not only is it more readable as code, it almost makes sense as English.

The final boolean operator is !

No, that wasn't an expression of surprise.  The exclamation point is the 'NOT' operator.  It turns around the sense of any boolean expression, turning true into false and false into true.  Unlike the others I discussed above, which are binary operators (they work with two values), ! is a unary operator.  

!false == true

and

!true == false

So you can write:

if ( ! meetsQuotas && specialIncentives ) {
    System.out.println("We're giving a discount due to the special incentives program, but this guy is a cheapskate.");

That's about all I have to say about booleans for now.  Learn them, use them, love them.

This Should Throw You For a Loop

Hopefully you digested the information from my last post about while loops and you're feeling eager for more.  Fortunately, the rest of what you need to know about loops should come easily if you're already comfortable.

There are two other basic loop structures available to Java developers.  These are the for loop and the do/while loop.  As with the while loop, you could conceivably accomplish any looping task with either of these, but usually one or another will jump out at you as being best suited to the task at hand.

Since we've already looked at while, let's look at do/while.  We'll start with making a simple loop that duplicates the function of the first one from the while loop post:

boolean keepRunning = true;

do {
    System.out.println("I am in a loop.");
    keepRunning = false;
} while ( keepRunning );

Note that it really is the same inside the loop, only the location of the 'while', along with an added semicolon, and the addition of the word 'do' make it any different.  So why do we even have it?  The reason is that a while loop is an entry condition loop, whereas do/while is an exit condition loop.  When you reach a while loop, if the condition for which it tests is false you will never execute it at all, whereas a do loop will always execute at least once.

The difference is better illustrated if we start with 'keepRunning' set to false.

boolean keepRunning = false;

System.out.println("About to enter loop.");
while ( keepRunning ) {
    System.out.println("I am in a loop.");
}
System.out.println("Done with loop.");

Will generate the following output:
About to enter loop.
Done with loop.

Whereas:

boolean keepRunning = false;

System.out.println("About to enter loop.");
do {
    System.out.println("I am in a loop.");
} while ( keepRunning );
System.out.println("Done with loop.");

Will generate this:
About to enter loop.
I am in a loop.
Done with loop.

As with a while loop, and ignoring the possibility of multiple threads, if you ever want it to exit something needs to happen to make the exit condition evaluate to false.

That about sums up the differences between while and do/while.  Use whichever one makes the most sense based on the code you need to execute and the variables you have available to you.  You'll get a feel for it over time.  Without having done any surveys or run any metrics, I'd say that the regular while loop is used several times more often than do/while.

The other loop structure you need to know about, and perhaps the most commonly used of all, is the for loop.  It works like this:

for( starting condition ; loop conditional ; loop increment ) {
   //do something
}

Lean back and take a deep breath, it's not that bad.  To prove it, let's duplicate the code for looping five times (for is most commonly used for counting or iterating over some multiple part data structure).

First, a refresher.  In my previous post I put this up as how to use a while loop to generate five lines of output:

int runCount = 0;

while ( runCount < 5 ) {
    System.out.println("I am in a loop.");
    runCount ++;
}
This loop is an absolutely perfect candidate to use a for loop.  Here's the same thing expressed as a for:

for (int runCount = 0 ; runCount < 5 ; runCount ++ ) {
    System.out.println("I am in a loop.");
}

This is cleaner, more concise, and just plain shorter than the same loop expressed as a while.  It also illustrates some syntax differences of which you need to be aware.

First of all, note that 'runCount' is available ONLY within the loop parentheses and in the body of the loop.  Once you reach the closing bracket it goes out of scope.  This differs from the while loop and is a potential 'gotcha':

This is perfectly valid:

int runCount = 0;

while ( runCount < 5 ) {
...
}

System.out.println("The value of runCount is now " + runCount ); 


But this is not:

for ( int runCount = 0 ; runCount < 5  ; runCount ++ ) {
...
}

System.out.println("The value of runCount is now " + runCount ); 

You will receive a compiler error because runCount is completely unknown by the time you reach that attempt to print it out.  You could of course write it like this to get around the problem:

int runCount;

for ( runCount = 0 ; runCount < 5  ; runCount ++ ) {
...
}

System.out.println("The value of runCount is now " + runCount ); 

Note that one can pretty much mechanically translate between while and for loops.  Just move the first and third terms inside the parentheses to before the while and just before the closing curly brace.

The for loop is a natural fit for processing arrays and Collections, both of which I will be discussing soon.  You can use the value of the index variable (runCount in this case) inside the loop for all sorts of reasons, providing a counter, spitting out line numbers, whatever you need.

By convention, when you create an index variable in a for loop, and have no particular name for it, we tend to just use the letter i.  You'll see this kind of thing a lot:

for ( int i = 0 ; i < 10 ; i++ ) {
  System.out.println("Loop iteration " + i );
}

Well, to be fair, you'll see something that starts that way but actually DOES stuff a lot.

It is also possible to embed one loop inside of another.  You can't use 'i' again, so we move on to 'j' (then 'k', 'l', etc. if we want to go nuts.  Try not to go nuts).

This is an extremely basic example of that:

for ( int i = 0 ; i < 5 ; i++ ) {
    for ( int j = 0 ; j < 5 ; j++ ) {
        int product = i * j ;
        System.out.println (i + " X " + j + " = " + product );
    }
}

These few lines of code will generate 25 lines of output:

0 X 0 = 0
0 X 1 = 0
0 X 2 = 0
0 X 3 = 0
0 X 4 = 0
1 X 0 = 0
1 X 1 = 1
1 X 2 = 2
1 X 3 = 3
1 X 4 = 4
2 X 0 = 0
2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
3 X 0 = 0
3 X 1 = 3
3 X 2 = 6
3 X 3 = 9
3 X 4 = 12
4 X 0 = 0
4 X 1 = 4
4 X 2 = 8
4 X 3 = 12
4 X 4 = 16

The various loops are critical to your understanding of software development.  Get comfortable with them, then get more comfortable with them.

As always, if you have comments or questions, please post them.  I'd love to hear from you.

View code here

Going Loopy a While

Another key thing you need to understand is looping.  Repeatedly performing identical operations is extremely common in any programming language, and Java supports a pretty full set of looping structures.

Perhaps the simplest loop to understand is the while loop.  Let's write a simple one:

boolean keepRunning = true;

while ( keepRunning )  {
    System.out.println("I am in a loop.");
    keepRunning = false;
}
System.out.println("I am no longer in a loop.");

Now, the above code was the most pointless of all possible while loops, since it executes its code just once and stops.  Specifically:

First, we created a boolean variable and set it equal to true.  We are using this as a flag or condition for the loop.

Then we created a loop, which will execute over and over again so long as keepRunning is still true.  The while statement always expects a boolean expression in the parentheses, and executes one statement so long as that expression evaluates to true.  Curly braces along with any code inside of them count as one statement, and I strongly urge you not to skip them just because some looping or conditional structure you're using only has one line.

So the while statement executes, sees that its condition is true, so it enters the loop.

The system generates the following unexpectedly exciting output to the console:

I am in a loop.

And then it sets keepRunning to false.  The first iteration of the loop is now finished, as we hit the closing brace.

Now the program flow goes back up to the while statement.  keepRunning is now false, so the while statement will bypass the next statement (which, as I mentioned, is everything within the curly braces).

So now we pass control out of the loop and generate the following even more interesting output:

I am no longer in a loop.

That's a lot of words to describe something that is conceptually very simple, which is basically true of many programming structures.  We create instructions and that computer really has to scurry along doing a lot of different stuff for us.  Ah, the power!

Of course, a loop that doesn't loop hardly qualifies for the name, so let's do something just a bit more complex:

boolean keepRunning = true;
int runCount = 0;

while ( keepRunning ) {
    System.out.println("I am in a loop.");
    runCount = runCount + 1;
    if ( runCount > 4 )  {
        keepRunning = false;
    }
}

Try to predict what will happen here.  Follow it through, it's not really that complicated.  I'll wait.






OK, there's not going to be a quiz, I'll just show the output:

I am in a loop.
I am in a loop.
I am in a loop.
I am in a loop.
I am in a loop.

Do you understand what happened here?  If so, you can skip the next bit.


We started off with two variables, keepRunning and runCount.  Let's examine the status of these during each iteration of the loop.

keepRunningrunCountWhat does loop do?
true0Prints I am in a loop.Increase runCount to 1
true1Prints I am in a loop.Increase runCount to 2
true2Prints I am in a loop.Increase runCount to 3
true3Prints I am in a loop.Increase runCount to 4
true4Prints I am in a loop.Increase runCount to 5 then sets keepRunning to false
false5Nothing! We never execute at this point, having failed the while condition

Make sure you understand the flow of that loop completely.  When it makes perfect sense to you, and you understand why everything is happening, you're ready to move on.  to the next section

I wrote that loop in pretty much the longest possible way, because I wanted the flow to be explicitly laid out and easy to follow.  Structures such as boolean flags and counters do get used, but it's rare to have two of them for a job this simple.  Most good programmers try to keep their code terse and tight, without extra fluff.  In this case, we have fluff.

If I were to write the above for myself, and I absolutely had to use a while loop to do it, I'd certainly make use of the following shortcuts:


  1. The '++' operator.  This increments a value.  There's also a similar '--' operator.  I could replace 'runCount = runCount + 1' with 'runCount ++'.  My fingers will thank me, as will those reviewing the code and really expecting to see that.  Just a caveat, the increment operator can appear before OR after the variable, and if it's alone on a line it does not matter how you use it.  But if you are using it in a larger expression it matters.  If you use it as a prefix operator by putting it before the variable, the variable is incremented and then it is evaluated.  If you put it after, the variable is evaluated and then it is incremented.  SO:

    If you write:

    int i = 5;
    int product = ++i * 5;

    Then product will be 30, because it would turn to 6 before being multiplied.

    Whereas if you write:

    int i = 5;
    int product = i++ * 5;

    Then product will be 25, because it would still be 5 when multiplied.

    In either case, after the second line i will have the value 6.
  2. I would take advantage of the boolean nature of comparison operators.  In other words, I can use the value of:

    runCount > 5

    anywhere I need a conditional.  In short, I don't need 'keepRunning' at all.
Instead, I could write my loop this way:

int runCount = 0;

while ( runCount < 5 ) {
    System.out.println("I am in a loop.");
    runCount ++;
}

This is cleaner, shorter, and quite clear once you understand all the bits and pieces.  Indeed, it may be clearer, as there's less to track, and the exit condition of the loop is explicitly stated right where it's used.


You have no doubt noticed by now that I have been starting everything at 0, not 1, which may feel a bit strange.  This is just something you will have to get used to, as it is extremely common for developers to do things that way.  This is most likely due to the way arrays (and pointers in some languages) function, where the first element is zero.  We'll talk about arrays in another post.

Naturally, we sometimes get the counters a bit wrong, perhaps we compared against the wrong value, or used an equals check when we should have used a less than check.  Most of the time, we get this wrong by one.  Always check your logic and indexes thoroughly, particularly before it feels really natural to you.  We call these off-by-one errors fencepost errors.  The reason for this comes down to this:  Imagine you go to your local Home Labyrinth store because you want to put up a hundred feet of fence.  The fence panels are 10 feet wide and you need 10 of them.  You also need posts to go between each panel, and because you haven't had your coffee yet (they open early and you're feeling ambitious this Saturday) you also buy 10 posts.  Everything is going great until you reach the end and realize you need an eleventh post at the far end.  (Usually this happens three minutes after the store closes, in my experience).

There are several other loop structures as well, which I'll discuss next time around.  I want you to get this simplest one locked down firmly before we move on.  It would be possible to only use this one kind of loop and write almost any program, but some of the others are better suited to certain tasks.

View code here

Thursday, November 13, 2014

Variables and How To Use 'em

We've touched upon variables here and there in the previous few posts, and it occurred to me that I should probably clarify their purpose and use.

Programs would be remarkably useless if all you could do was hard code actual values into them.  So what we do is use symbolic representations of numbers, characters, words, etc.  This is similar to algebra, in that we often use 'x', 'y' and other such letters to represent numbers.  They act like placeholders.  Maybe it's better to think of a variable as a shoe box.  You can keep something in the box, and put a label on the box, too.

Let's start off with some very basic examples:

int one = 1;

No, you probably won't use 'one' as a variable name very often, but it's valid Java to do so.  We've created a label called "one" and assigned it the numeric value 1 in one fell swoop (or swell foop, if you prefer).  So we have a shoe box, with a label on it that says "one", and that has the number 1 inside of it.

Now here's why 'one' is a bad variable name:

one = one + 1;

Now one is equal to 2, and we've got the beginning of a comedy routine.

We can perform all kinds of basic math operations on integer variables this same way.

Multiplication:
one = one * 2;

Division:
one = one / 2;

Subtraction:
one = one - 1;

Assuming that all five lines of code above were run in a program, what would one now equal?

Well, it started out as 1, then we added 1 so it became 2.  Then we multiplied by 2 to get 4, divided by 2 to get 2, and then subtracted 1 to get back to 1.

Following chained sequences of events is an important skill for the programmer.  You need to be able to understand what each line of code will do to each variable and keep track of these things as you essentially simulate the program's run in your mind.  This can get very involved, and there's no shame in writing down the state of each variable after each step in a table format.

Of course, 'int' is just one data type, and a limited one at that.  It can't represent decimals easily for instance, and it's fairly hopeless at representing a word processing document.  But it's a start.  I don't intend to provide a complete reference to all data types here, but some basic examples include:

int - Represents an integer (whole number). Examples include 5, 18 and 42
double - Represents a double precision decimal number.  5.1842 could be a double
boolean - Represents a true or false value, which is really useful for conditionals and loops.  Must be either true or false

All of these types share the distinction that they are actual basic data types (atomic types), not objects.  There are objects in the Java library which can be used (fairly interchangeably) in their places.  The equivalent object representations for int, double and boolean are Integer, Double and Boolean.  We'll discuss those in another post, but for now I just want to point out that as a general rule (and you should follow it) object types will begin with an upper case letter, and that will help you to know what you're dealing with.  Of course, over time you'll get very used to the limited menu of atomic types and recognize them quickly.

 There are many mathematical operations one can perform with these data types, some to change them, some to test them.  We addressed some basic arithmetic operators above.

Sometimes you need to test variables for their values.  You can determine equality, or which number is greater by using comparison operators.  The first comparison operator you probably want to know about is equality.  We use a double equal sign for this (==).  This must not be confused with the assignment operator (=)!  Many logical errors in programs have cropped up from confusing these two, and they can sometimes be hard to find.  When quickly scanning through a couple of pages of code, the difference between:

if (n = 5)

and

if (n == 5)

can be hard to spot.  A trick that works surprisingly well for some developers is turning this around.  Basically, "if (5 == n)" is precisely equivalent to "if (n == 5)", but writing "if (5 = n)" is an error the compiler can catch because you cant assign a variable to a constant, only the other way around.  This is sometimes referred to as Yoda style or Yoda syntax.  Interesting is it not?

Technically, these comparison operators wind up evaluating to boolean values.  So it's perfectly OK to do something like this:

boolean nEquals5 = (5 == n);

We will go into booleans in more depth at another time, but for now I just want to point out that you can combine booleans with AND, OR and NOT operators (&&, ||, !) to evaluate more complex questions.

Hopefully, that's enough to keep you from getting too confused as we get into some examples going forward.  If you have questions, please feel free to post them, and I'll try to answer as quickly as I can.  The areas where people have difficulty are where I want to concentrate.

View code here

Wednesday, November 12, 2014

Functions

Functions, and their close cousins methods, are fundamental to software development.  Syntactically, there is no real difference between the two.  They really only differ in terms of the context in which they operate.  Technically, functions should not depend on anything except the information they are given.  If you call a function called 'add' with 2 and 3 as parameters, it should always return 5 regardless of what else might be going on inside your system.  A method called 'add' might only be given one parameter, and be expected to operate in a context where it is aware of some other number to which the parameter will be added.  Please note that it quite possible to write your code so that it does not abide by these rules, but it's probably not in your best interest to do so.

Suppose we want to write that function mentioned above, 'add'.  Furthermore, let's suppose we want it to be made available to any class that wants to call it, and that it is expected to do only integer (whole number) arithmetic.  We might write it like this:

    public static int add ( int number1, int number2 ) {
        return number1 + number2;
    }

This is of course simplistic, and probably better accomplished in-line where you need to perform the addition, but that's the nature of an example.

This function should be easy enough to understand once you understand the required syntax.  It starts off by being declared 'public', which means any other class can call it.  By declaring it 'static' we've indicated that we do not wish to have to create an object in order to call the function.  It has to give back an integer value to the code that calls it, so we put that up there, too.  Then we have the name of the method, followed by parentheses.  Inside the parentheses is a list of parameters (which can be empty).  Parameters are just variables given to a function from an outside source.  Note that every time it's given the same set of parameters it will give back the same result.  There are no hidden variables or shared secrets that will affect its operation and change the return value.

If I want to use add in this same class I can just write:

    public static void main(String[] args) {
        int answer = add ( 2 , 3 );
        System.out.println("The answer is " + answer );
    }

This will call 'add', which will see number1 as having the value 2, and number 2 as having the value 3.  Then add will then return 2 + 3, which is of course 5.  Because it's a return value, we can use the equal sign to set 'answer' to the returned value.  So 'answer' becomes 5, and we print out:

The answer is 5

Naturally, most useful functions will do a bit more than this, and they can in fact get very complicated, calling other functions and methods to do all sorts of tasks.  Returning a value is strictly optional, but you must declare the 'return type' for every function you write.  If you do not wish to have a return value, you can declare the return type as 'void'.  If you do declare a return type that is not void, you must ensure that you return an appropriate value when your function ends.  This would be an error and would fail to compile:

    int badAdd( int value1, int value2 ) {
        if ( value1 < 100 ) {
            return value1 + value2;
        }
     //Fails to compile!
    }

I've introduced another new concept here, a conditional statement in the form of 'if'.  This is another key concept, the idea of taking some action only if a specific condition is met.  In this case, if the value of 'value1' is less than 100 we'd execute all of the code between the brackets immediately following the if.  Technically the brackets are not required if you only have one line of code, but now forget I ever said that and just use them.  Many hours have been lost to misunderstandings that crop up from failing to use brackets where they can be.  Don't waste minutes (or worse, fail to notice a problem) by saving a half a second of typing and a couple of characters of disk space.

There are ways to get around returning a value in case of error conditions, which I will discuss under the topic of Exceptions at a later date.

View code here

Project Organization Part 1: Directory Layout

In our first post we set up a simplified project called HelloWorld.  That is of course, a very standard first program in virtually any language, designed to provide a minimal functional program to provide a bit of user output.  That's all well and good, but in the interest of getting something built I glossed over or just plain ignored some aspects of project setup.

First and foremost, we allowed the source code and the compiled .class files to sit in the same location.  I was tempted to avoid that even for the first post, but eventually decided it was best to just get something done and tackle that in a separate post.  So here we are.

As a general rule, I greatly prefer to set up my projects with two main directory trees, one for source files and one for build products.  Typically I'll use the names src and build for these directories.  You will find that once you start using an IDE this structure will tend to be built automatically for you, which will make it transparent and natural.

So let's take HelloWorld and convert it to a better structure:



Now we've got a couple of buckets for our files.  For now we'll ignore packaging and just rearrange the files.  Since class files can be regenerated at any time, we'll just get rid of HelloWorld.class.  We will then move HelloWorld.java to its new home.


Now let's rebuild our class file to make sure we can still operate.  Go into the build directory and run javac, but this time you'll have to enter a relative path in order for the system to find it.  After that you can, if you so choose, do a test run to make sure it still works correctly.




I mentioned earlier that we're not going to discuss packaging, but I do want to quickly mention that we still have not gone far enough with moving our files around.  The reason for this involves the way that Java loads classes, and has no real implications until you start assembling slightly larger programs.  Suffice it to say that we're not done yet, but we can wait a while before we get into that.  Next time around, we'll talk about functions, which will be a key concept you must understand before we get around to creating objects.

Getting Started with Java (Hello, World)

I spend some time on the subreddit /r/javahelp, and see many of the same questions and errors crop up again time after time.  People who are trying to learn how to program get caught up in the syntax, and wind up down the rabbit hole.

So let's take a look at the very basics of Java.

The basic unit of Java programming is the class.  We define a class by creating a text file (with a .java extension) according to some strict rules, then we compile the .java file to a .class file using the javac command.  Better yet, we use an Integrated Development Environment, but I will discuss that in a future post.

Your source code files, which is what we call the .java text files, can be created with any text editor.  Notepad will work fine for these early examples, but there are certainly better ones out there.  Even for your first efforts, you will want a clean work area free of clutter, so it is best to create a new directory somewhere on your hard drive for each project on which you will work.  I prefer to base everything under a "\Dev" directory so that all of my work is in one easily identifiable place.  Since my main system at home uses a solid state drive plus a storage drive, that means my work area is inside of "D:\Dev".  I'm going to write everything here as though I'm using the C: drive, though, since that's more likely to be what you are using.

We want to have a directory for our project, which will be where we go to do all of our work on any given task.  Finally, while this may not seem very important right now, it's best not to cluster your source files right there in the project directory.  So I like to have a 'src' directory in which to save my source code files.

So, since by convention our first project is "Hello World", we wind up saving our .java to the following directory:

C:\Dev\HelloWorld\src

There is one additional complication relating to source, which is what we call "packages".  I'll defer discussion of this for the time being, but they are set up in your workspace as directories off of the src directory above.

So, that being said, what does Hello, World look like in Java?  It quite literally cannot get any simpler than the following example:

HelloWorld.java

Everything here is necessary to the task at hand.  Let's go through it line by line:

public class HelloWorld {

Every java class needs to be declared.  While there are a few other possibilities for this line, this is the most common way in which you will declare your classes.  The word 'public' means this class can be seen (and ultimately used to do work) by any other class that knows about this project.  We are defining a class, and giving it the name 'HelloWorld'.  By convention we always start our classes with an upper case letter, and follow that with what is called "Camel Case", which means lower case words with upper case initial letters.

Finally, there is an opening curly bracket, which matches the curly bracket on the very last line.  Curly brackets (or braces) are going to be your close companions as you work with Java, so get used to them.  They are always 'balanced', meaning that each opening bracket '{' has to be matched by a closing bracket '}'.

public static void main(String[] args) {

Next up, we have 'main'.  Any command line Java program will start at main, and main is always declared the same way.  The only thing you can really change here is the name 'args', which is arbitrary.  Command line switches and parameters will be found in args, and although we aren't making use of them in this program, we still have to mention the possibility.  Again, we have an opening bracket.  Any 'method' (or 'function') you create in a Java class will have a set of brackets around its code.

System.out.println("Hello, world!");

Here's the only line that actually does something in our little demonstration.  It writes the literal value Hello, world! to the console output.  All Java programs (and indeed, all programs in most languages have 'standard output' and 'standard input' available by default for simple user interaction.

}
}

These two lines are just needed to balance the brackets.  The first closes the definition of "main", the second closes the definition of the class HelloWorld.

Presuming that a Java Development Kit is available and configured correctly, this can be built with the 'javac' command.:
Compiling HelloWorld
If we get no error messages, we should now have the file "HelloWorld.class" sitting here as well.


Now we can run it with the 'java' command:
Running HelloWorld

In a future installment, I will discuss further organization of the project space.  There are good reasons not to mix all the .java and .class files in one place.  Integrated Development Environments make all of these tasks simpler, and I definitely recommend obtaining IntelliJ IDEA or Eclipse or NetBeans (or all three).

View code here