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
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
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.
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.
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:
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.
Also you can run your program by clicking the green arrow button on 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.
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.
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.
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.
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 >>
Related hubs...
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 5
One of the useful classes that handle inputs from a user in Java is the Scanner class. The Scanner class is located in the util (utility) package of the Java library. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 2
In this lesson you will learn how to create your first Java project and how to interpret the NetBeans code editor window. - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 9
In this lesson we shall learn how to use the Switch statement in Java programming. Switch statement is a selection statement, that means when used, it select one value among many values. Switch statement can also be used instead of IF .. ELSE ... - Programming In Java - A Step By Step Tutorial For Beginners: Lesson 8
IF .. ELSE statement has two parts, one to cater for when condition is TRUE and another for when condition is FALSE.
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.