Get up to 80 % extra points for free! More info:

Lesson 2 - NetBeans IDE and your first console application

Lesson highlights

Are you looking for a quick reference on creating a Java SE project in the NetBeans IDE instead of a thorough-full lesson? Here it is:

  • Download the latest OpenJDK at https://adoptopenjdk.net/
  • Install it and during the installation don't forget to check to create and set the JAVA_HOME system variable:
Java Basic Constructs
Creating a new project in NetBeans - Java Basic Constructs
  • Select the Java - Java Application template:
Creating a new console application in Java - Java Basic Constructs
  • Edit the project name and location if needed and confirm the form
  • Hit the green play button in the toolbar to run the project.

Would you like to learn more? A complete lesson on this topic follows.

In the previous lesson, Introduction to the Java language, we talked about the language itself and went over how Java works. In today's lesson, we're going to focus on the NetBeans IDE. We'll show you how to use it and program a simple console application.

IDE stands for Integrated Development Environment. In a nutshell, it's an application in which we'll write a source code, and then use it to run, test and debug our application.

Installation

Lets start by installing NetBeans of course.

JDK

As first we need to download JDK (Java Development Kit) which is a set of tools needed for Java development. There are more distributions available, we'll use AdoptOpenJDK which is free and available at https://adoptopenjdk.net/. We'll select the latest OpenJDK version, keep the JVM selection on the default HotSpot and click the Latest release which starts the download.

When installing, it's important to check that we want the installer to create and set the JAVA_HOME system variable on the component selection screen. Without this system variable, the NetBeans installer would not know where to look for the JDK and could not work:

Java Basic Constructs

NetBeans

Now, let's download the IDE. It's a abbrev. of Integrated Development Environment and, simply speaking, it's an application in which we'll write our source code, run it and debug it. There are many different IDEs available, e.g. Eclipse. We'll use Apache NetBeans which is available for free at https://netbeans.apache.org/…d/index.html. We select the latest available version and click the Download button. On the next page, we select the version for Windows-x64 in the Installers section on finally select the mirror to download from on the last page, usually the first link is the best choice.

Install it (just keep clicking Next) and run it.

Backup and version control

Programmers usually need a tool that will provide version control and backup of his work. We can't rely on the fact that we could just save the program because we're humans and humans make mistakes. When you lose a few days' or even a few weeks' work, it can be really demotivating. It's good to think about such situations right from the start. I highly recommend Dropbox, which is extremely simple, and automatically stores multiple versions of your files, which makes it possible to revert to previous versions of the project, and also synchronizes with a web repository. Even if you've accidentally deleted your project, overwrote it, somebody stole your laptop or your hard drive accidentally collapsed, your data will remain safe. Dropbox also allows you to share projects with several developers.

You can also use a tool called GIT for the same purposes, but its configuration would require the "while" article. Dropbox is perfect for our current intents and purposes.

Creating a project

Run NetBeans and select File -> New Project in the application menu.

Creating a new project in NetBeans - Java Basic Constructs

In the New Project window, select the Java - Java Application template. In the next menu, name this project FirstApplication. Create a folder for your projects in your Dropbox folder, for example, "java". Use the Browse button and select a folder C:\Users\your_name\Dropbox\java\. We will stick with console applications, command line interface, for a while because it needs minimal knowledge of the object-oriented world, and they are ideal for learning the basics of the language. The window should look like this:

Creating a new console application in Java - Java Basic Constructs

Confirm the form.

NetBeans Tutorial

The window now looks like this (I have resized it to fit :) ):

NetBeans Java window - Java Basic Constructs

We're interested mainly in the middle window in which NetBeans will generate some source code. It may be a surprise to some of you that we are not starting out with an empty window, but then again that is exactly what a template is (easily modifiable preset code)! We'll keep it simple for now, everything will be explained throughout the courses. Some parts are quite advanced, so we'll just accept the fact they're there and go into detail later.

We won't pay any attention to the package and class, just keep in mind that Java uses them to structure programs. The key will be the main() method for us, we'll write our code between the curly brackets below it, i.e. the body. Main() is a reserved word, and Java knows that this method must be executed as first after the application starts (there are more methods, but more on that later). Actually, we'll ignore everything except for the main() method.

The second important element in the window is a green "Play" button in the upper bar, which compiles and runs our program. You can try it, but our program will not do anything, just turn terminate right away. You can also run the program with the F6 keyboard shortcut.

Hello world

As tradition instructs, we'll make the first program most people make when they learn a new language -Hello World. This is a program that displays "Hello world" or some similar text. Again, let me remind you that we'll write commands in the body of the main() method.

To write text we use the following bit of code:

System.out.println("Text");

System is a class. For now, we'll see classes as sets of available commands. In Java, commands are called methods. So System includes methods for console operations. We call the println() method, which displays a text, on the out (as output) object. You can see that we use a dot operator to call the method in the class. Methods can receive input parameters, which are entered in parentheses and separated by commas. In the case of the println() method, the parameter is the text to be printed. In programming, texts are called strings, like a string of characters, and written it in quotation marks. Java sees the quotation marks and interprets it as text so it's not confused with other commands. We should write the commands on separate lines, and after every line, we should put a semicolon. Our main() method will now look like this:

public static void main(String[] args)
{
    System.out.println("Hello ICT.social!");
}

You can run the program by pressing F6 if your system hasn't assigned a different command for the key. You can also run code snippets from our articles by pressing the "Play" button above, you can edit the code as well, just click on it. Try that :) This makes you able to study from anywhere, even without your IDE.

FirstApplication
Hello ICT.social!

Congratulations, you have just become a programmer :) That will be all for today, in the next lesson, Variables, type system and parsing in Java, we'll look at the basic data types and create a simple calculator.

Today's project is attached as a file at the end of the article. You could always download the result below each lesson. I suggest that you create a project using the tutorial, and if something in your project doesn't work, then download it to find a mistake. If you download the lesson code before even trying to make it yourself, you won't learn anything :)


 

Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.

Download

By downloading the following file, you agree to the license terms

Downloaded 209x (15.01 kB)
Application includes source codes in language Java

 

Previous article
Introduction to the Java language
All articles in this section
Java Basic Constructs
Skip article
(not recommended)
Variables, type system and parsing in Java
Article has been written for you by David Capka Hartinger
Avatar
User rating:
20 votes
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn university David learned IT at the Unicorn University - a prestigious college providing education on IT and economics.
Activities