Programming in Java Netbeans - 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
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.
- 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.