In this Java tutorial we will create and run our first Java program. Remember that the best way to start learning any programming language is through practice. So lets get started!
Note: Remember that in order to create and run java programs you need to not only download java, but have it installed on your computer as well. Also you will need an IDE, for example Eclipse! If you haven’t downloaded java or an IDE yet check the links below. In addition if you haven’t seen my Learn Java Programming tutorial and you either need a recap or are confused with java still, you should check that out first!
Creating and running a Java project
First thing we need to do is create our project file. Open up Eclipse and follow the steps below!
Select an area where you want to install the workspace. This is the area where all of your projects will be located. I’d also select the Use this as the default and do not ask again. Since you don’t want this popping up every time you load Eclipse.
This is the default eclipse screen you’ll see once it loads up. You can close this with the X near the top left corner beside the text Welcome.
Now we have a clear screen of what eclipse looks like. You can rearrange the size if you want but for this I’m going to keep it how it is.
You can always reset the default layout if you drag or close something by mistake! Just go to Window tab at the top and click Reset Perspective.
Select File from the top left corner then New followed by Java Project.
Under Project Name we will call our new project MyFirstProgram and select Finish in the bottom right corner.
So now we have our first project files created and are ready to move on and create some code!
Select the icon that looks like a C followed by Class near the top middle of your screen. We are doing this because Java programs require classes, as a way to control information. Everything in Java is used by a Class of some sort.
Type in “basic" as it is the class name we are going to use for this example. Also select the check box for public static void main(String args) as this will be the class that actually runs the program itself.
When I say actually runs the program by itself, I mean that it is the first class that the program will load and read from when you launch your program. From there it can load and call other classes without the argument we selected above.
Now you’ll see the default eclipse class code when using “public static void main(String args)".
If you didn’t use this option you would only have public class basic with brackets after it.
But as I said earlier, this is the class that initiates the program so it requires it!
Type in the following line.
System.out.print("My First Java Program!");
This prints the text you see in the quotations. It is just a simple example of a print statement.
Click the button that looks like a play button, it is directly under the N in Navigate.
As you can see in the console window below, it has printed My First Java Program!
That is it, you have created your first Java program!
Now try developing something more advanced. For example try adding in a if statement that produces output based on user input!
I’ll be going through tutorials that involve things like that later on, good luck!