Updated date:

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 3

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.

Table of Contents

<< Lesson 2 | Lesson 4 >>

Lesson 3: Writing Java Program

programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-3

In Lesson 2, we saw how the Java code window looks like and we were able to interpret the various parts of the Java code. In this lesson we’ll learn how to write our first running Java program.

Java has a lot of inbuilt functions that we can use to accomplish most of our common tasks, we only need to import these functions in our program and use them while others are added automatically by Java when it starts.

Remember the window shown below? If you can’t, please re-visit Lesson 2 where we learnt about the Java environment, the Java code editor window, how to create a Java project and how to interpret Java project window and the code editor window.

Java code editor window without comments

Java code editor window

Java code editor window

To write our first program, we are going to display the text “This is my first Java program” on the Java output window (console window).

To do this, we’ll use the most common Java output function, the System.out.println( ) function, so type the following code inside the “main” method.

System.out.println( " This is my first Java program " );

As you type the full stop after "System", notice NetBeans will try to display a list of available options, this capability of a programming language is called IntelliSense.

Java IntelliSense

Java IntelliSense

Scroll down the list to get the “out” and double click or press Enter key. When you type the dot after “out”, again the list appears.

programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-3

Again, scroll down the list to get the “println” and double click or press Enter key. What this function will do is to display the text that is inside the quotes (“ ”) on the output window, so type the text inside the quotes. Remember to end your code with a semicolon (;). Your code window should now look like the one shown below:

programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-3

Save your program by clicking File >> Save, or File >> Save All on the menubar or click the Save icon on the toolbar at the top the screen. There are various ways you can use to Run your program in NetBeans. The easiest of all is to press F6 on your Keyboard. You can also Run a program by clicking Run on the menubar and selecting either Run Main Project or Run File.

Run from the menubar

Run from the menubar

Also you can run your program by clicking the green arrow button on the toolbar.

Run from the toolbar

Run from the toolbar

Another fast and best way to Run your program is by Right clicking your file on the Projects window and selecting Run File from the menu options. This will ensure that you are running the right source code file.

Run by right clicking the file on the Projects window

Run by right clicking the file on the Projects window

Using either of the above given ways of running a program, run your program and if there are no errors you should be able to see the program output on the output display window.

Successful run

Successful run

Congratulations! You have written your first Java program.

If you need to share some of your programs with others, NetBeans can let you clean and build your project with all the necessary files for export by creating a .Jar file (Java Archive) for you. On the Run menu at the top of the screen, select Clean and Build Main Project. Java will clean your code, include the necessary files and put all your work in a folder called dist.

programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-3

Opening the dist folder will display two files; a JAR file and README text file. The JAR file is your program and all the additional necessary files while the README file contains instructions on how to run the program and how to export the program file.

programming-in-java-a-step-by-step-tutorial-for-beginners-lesson-3

In the next lesson we shall see how we can work with variables in Java and how we can use variable to manipulate data stored in the computer memory. See you in Lesson 4


<< Lesson 2 | Lesson 4 >>

Want to make money online?

Comments

Danson Wachira (author) from Nairobi, Kenya on April 08, 2013:

Hi amorfo ,

You have interest and i know you can make it in programming. Thanks for the visit and comment.

amorfo on April 07, 2013:

thank you for this one, it's difficult because it's in english but i am trying do it... again thank you very much

Danson Wachira (author) from Nairobi, Kenya on February 17, 2013:

Hi Stephen,

Am glad that this tutorial was helpful, thanks for the visit and comment.

Stephen on February 15, 2013:

I was looking for a tutorial like this, you have done it well and it is easy to follow.

Related Articles