Introduction to Programming with Java

Hello all, and welcome to the first Java tutorial!

I figured I would start rolling out some simple text tutorials before I get back home and start rolling out the video equivalents.  In this introductory tutorial I am going to go over what you will need to start programming in java, and go over the structure of a basic program.  Lets get started!

What You Will Need

This may seem cliche but I believe it to be completely true.  You will need to have the time and energy to become completely invested in learning.  Just like becoming proficient in math, biology or even a foreign language you will need to spend time and practice!  So the first thing I would do is determine how much time you can invest per day/week/month and plan it out to stick with it.  Everyone learns to program and about programming concepts at different paces.  So keep that in mind, and feel free to ask me any questions if you come across a problem!  Now that we got that out of the way we can get onto the physical needs of learning Java.

There are two ways to program, I would suggest to use an IDE such as NetBeans or Eclipse but you can also go to the oracle website to download java and program using your favorite text editor!  I prefer using NetBeans, so if you want to follow along similar to me then I would suggest downloading that.  IDE stands for integrated development environment and lets the developer manage everything from within the program, and it helps out when you are new to be able to write and compile and run all in one place.

Once you get the IDE installed you can play around with it and look at some of its simple tutorials on how to use it.  If you need some help finding out how to open a new project and open a source file, let me know and I will go more in depth.  I will be able to show it quicker than tell it though, so I will leave that detail to the videos.  But please let me know if you need to know before then.

Now that we are set

here is a sample java program:

class HelloWorld  //The class definition for the HelloWorld class. 

{

           public static void main(String[] args)   //The main function of the HelloWorld class

           {

                     System.out.println("Hello World!");  //Prints the string Hello World!

            }

}

Lets break it down and go over some key elements that will recur with programs you will write in the future.  Every program will start with a definition of the class, in this case HelloWorld.  To give a more general version of the definition would be as follows:

class ClassName{}

And everything that you will program goes into the brackets following the class name.

          Pitfall: you will want to make sure that you have a pair every time you use {} or (), otherwise you will have all kinds of compile errors!

The next thing you will notice is // followed by a string of text.  The // denotes a comment and will be ignored by the compiler and are just there for the programmer to convey a message to them self for future reference, or whenever another programmer might inherit the program.  I would suggest you get use to writing in comment, even for small programs just to get into the habit.  Others will thank you later, and for more complex code you will be happy you commented when you go back to maintain.

Next comes the following bit of code:

public static void main(String[] args){}

This is the main method, and while every class does not need it every program does.  This might not make sense right now because most programs will only contain one class, so for now this is needed for every program.  This is where the bulk of your code will go, and it also needs two braces to contain all of the code.  You can see I added a comment for it as well to help give a quick reference.  As you can tell there is very little code in this main function which I will get to now.

System.out.println(“Hello World!”);

This is a simple print statement that you will use to print text to the screen.  There are two versions of this call, System.out.println() and System.out.print()  Here I use println because it will print a newline, which I will teach you how to add explicitly in later tutorials.  In between the parenthesis goes the text you want to print, as long as it is between two quotes it will be printed!  Here I print the classic beginner program text Hello World!  You can put anything in there, so try it out.  The last thing to notice is that the line ends with a semicolon and you will see that you end almost every line with this except for a few exceptions.  And make sure that all the braces are closed and that is the anatomy of a simple program!  Now if you were to type this into your IDE of choice and compile and run it, it will simply print Hello World! but that is the first step on the grand adventure we call programming!

Again, if you have any questions comments please post in the comments and I will get to you as soon as possible.

Practice Problem

At the end of every tutorial I will suggest a practice problem that incorporates everything that was discusses in this tutorial.  So for this intro tutorial I suggest writing a program that outputs some information about yourself such as your name, age, birthday, hobbies and anything else you can think of!  Be sure to play around with the differences between println() and print() and don’t forget to comment!

Advertisements

Tags: , , , ,

4 responses to “Introduction to Programming with Java”

  1. Confect says :

    Nice beginner programming tutorial. I’m not sure about NetBeans but I found Eclipse’s interface a little odd when I came across it a few months ago, and I have a lot of programming experience, so you may want to do a short tutorial for setting up a basic project and adding a source file, with screenshots and such, for the real newcomers.

    • Brett Martin says :

      I definitely have a plan for that in the near future. I just don’t have access to my usual tools because I am away for family vacation and I don’t want to download a bunch of stuff to a computer that is not mine. But that will be rolling out as soon as I get back. I will add some screen shots to this tutorial and I want to give video tutorials a shot. Thanks for the input!

  2. ALCH3MIST says :

    Useful, I’ve always wanted to get into Java. NetBeans was a lot easier than Eclipse [for me]. Anyway, great work, hope you keep it up. What’s the game you’re working on?

    • Brett Martin says :

      I agree about the NetBeans, most of the time I program on a Unix server though and us Vi so nothing fancy. And I am glad you like it!

      Its not to original of mechanics. It is sort of a fusion between N+ with the jumping and collecting items and Mario with enemies, but instead of scrolling left to right it will be up and down because you are scaling a mountain. I haven’t determined the name of it, but it will be an adventurer looking for missing pieces of artifacts on the mountain.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: