Introduction: First Java Program for Anyone
This simple intructable will give you a quick look at what program is like. It is very basic and easy to follow, so don't be afraid to click this, and learn a little. Maybe you will find that this is something you love!
Step 1: Go to This Website
There is usually a lot more to programming, but the simplest way to start is using an IDE (Integrated development environment), for this small project, we will use a simple online developer.
Step 2: This Is What the Page Should Look Like
The important parts on this page are the coding area and the output. The area labeled "your code" is where you type out the program. The black area is known as the console. It is where your program will put output. The blue button that says "execute" will compile then run the program, feel free to push it now and see what the console outputs. Can you see why the output is what it is?
Step 3: Hello World
This is the first program that every programmer writes: the famous, "Hello World." This program simply outputs "Hello World" to the console. Just copy my picture into the code area and watch as it runs. A few things I will point out: the the System.out.println(string) prints out a string to the console. A string is a variable type that means words; there is also "int" for integer, "bool" for Boolean (ie true or false), and many other variable types.
Step 4: Adding a Little More
In this step we will mess around with adding another string and concatenating it to the output. The "+" symbol is used to concatenate, in the system.out.println we are concatenating a string and two string variables. Notice the "\n" before the string, this is called a return, it tells the program to go to a new line, similar to if the enter key was pressed.
Step 5: Numbers
In this step we will mess with a int variable. Int variables hold numbers, printing the variable allows the user to output many different things, with one variable. Notice using another system.out.println will also return the output to a new line.
Step 6: Counting
Now lets say we wanted to make the program count from 1 to 100, this program does that, but when you run it, all you see is the "100." can you see why? The reason for this is because the program first counts, then outputs what the variable is, so the program loops until the variable x is equal to 100, then moves on to print the output.
Step 7: Counting Fixed
Alright lets move the print into the loop, and only count to 10 so that the output doesn't get to filled up. Now when we run the program you will notice that it outputs all the numbers 2 - 10 missing 1. The reason for this is because x is already incremented once before being output. lets fix this in the next step, feel free to see if you can before moving on.
Step 8: Counting 1 Thru 10
This is an example of just one way to fix the program. If you got it working on your own, congrats! printing before we increment allows the variable to be 1 and print, then increment. If you where to run it just making that change you would see it only print 1 - 9, so putting in the "=" in the while loop allows the program to run 1 last time once the variable is at 10.
Step 9: If Statements
This change makes the program only print when x is an odd number. The math behind this is fairly simple. Taking the variable and applying mod (%) 2 will return a 0 if the number is even, and a 1 if the number is odd. This is because mod works by dividing the number and returning the remainder, any even number you divide by 2 has no remainder, and any odd number would have a 1 remainder. The exclamation point "!" stands for not, so != is read as "not equal." Thus, when the variable x mod 2 does not return a 0, or is odd, print the variable.
Step 10: Go Crazy
That is all for this small little example, hopefully you found it somewhat entertaining, and maybe even had some fun! As you can tell there is a large difference from this simple programming to the large programs we use on a daily basis. Feel free to have some fun on this website, see what you can create, and go crazy with it!