Java: Netbeans IDE

This entry is part 1 of 54 in the series Learn Java

For developing Java program, we will be using Netbeans IDE, an open source IDE.

Netbeans IDE supports JavaFx, a Java UI framework that let us develop Windows Desktop applications.

First, download Netbeans from Netbeans.org.

Open up Netbeans and click on File -> New Project.  If this is the first time you are runing Netbeans, it will ask you to input the default directory for Java programs.

Set the Project Location as Java such as C:\java.

javaapp

 

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author daryl
 */
public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }
    
}

Netbeans will first help us to create the above template.  In the tradition of creating the first application, let us create a Hello World program.

Add the following line to the main program.

        System.out.println("Hello World");

The whole program that can print out the Hello World text will look like below.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author daryl
 */
public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println("Hello World");
    }
    
}

Click on Run -> Run Project and the output will be similar to below.

javafirst

The output Hello World is shown in the bottom of the screen.

In the template, a package called javaapplication1 is created for us. A Java package is a mechanism for organizing Java classes into namespaces.

Java packages can be stored in compressed files called JAR files, allowing classes to be downloaded faster as groups rather than individually.

Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality.

 

 

Java: Build and run java program like an executable

This entry is part 2 of 54 in the series Learn Java

From Netbeans, we can build the project as a executable jar file.  As a java program runs on top of a Java Virtual Machine (JVM), you need to ensure that JVM is installed in your PC.

From the Netbeans, click on Run -> Build Project (F11).

Once the project is built, a folder called dist will be created.  The jar file will be located inside this directory.

You can run the jar file like:

java -jar JavaApplication2.jar

jar

You can use the code below to try out this example
package javaapplication2;

public class JavaApplication2 {

    public static void main(String[] args) {

        System.out.println("Hello. Greeting from the Console");
    }
    
}

Java: Swing

This entry is part 3 of 54 in the series Learn Java

Our tutorial here is meant to learn the fundamentals of Java programming and if you look at my another tutorials on WPF, you will know that we can develop Windows UI using XAML.

The alternative to WPF in Java is JavaFX.  If you are new to Java programming, I actually suggest that you start learning JavaFX rather than Java Swing.  Swing is still very popular and there are still many programmers using it.  However, Oracle has put it in maintenance mode.  It will be replaced by JavaFX and I will work out a tutorials on JavaX soon.

We still have to learn Java Swing if you want to have a good grasp of Java UI programming and here, let’s use the following simple example to illustrate it.

Code Block
package javaapplication3;

import javax.swing.*;

public class JavaApplication3 {

    public static void main(String[] args) {
        
        JFrame frame = new JFrame( "HelloJava" );
        JLabel label = new JLabel("Hello, I am a Java Program!", JLabel.CENTER );
        frame.add(label);
        frame.setSize( 300, 300 );
        frame.setVisible( true );
    }  
}

In this program, the windows is formed by using the JFrame class.  We create an instance, called frame of the class JFrame.

The same goes to the text or the label that will go into the windows.  This is done by using the JLabel class.

swing

 

Java: Passing arguments to main

This entry is part 4 of 54 in the series Learn Java

When starting a Java program from the command line, we can pass some arguments into the program.

We will use our previous example for illustration.

package javaapplication3;

import javax.swing.*;

public class JavaApplication3 {

    public static void main(String[] args) {
        
        String[] argument = args;
        
        JFrame frame = new JFrame( "HelloJava" );
        JLabel label = new JLabel(argument[0], JLabel.CENTER );
        frame.add(label);
        frame.setSize( 300, 300 );
        frame.setVisible( true );
    }  
}

We can see in the code that there is a string array that is past into the main program.

We look at this line:

public static void main(String[] args)

This is the entrance to the program and in an Object oriented program, we call main a method of the program.

This main method is a static method as shown in the declaration.  We need this to be a static method as this method is not invoked through an instance.

We will look at instance in an object oriented program in our later examples.

To pass one argument Hello into the program, we use the following command:

java -jar JavaApplication3.jar Hello

The Hello argument will be past into the program and printed out as shown in the screenshot below.

argument

Java: integer variables

This entry is part 5 of 54 in the series Learn Java

In a programming language, the usage of a variable is very similar to variable in Mathematics.  We can assign variable with a particular value.

The example below will make use of 3 variables to to perform a simple mathematical function.

package javaapplication4;

public class JavaApplication4 {

    public static void main(String[] args) {
        
        int a = 10;
        int b = 15;
        int total;
        total = a + b;
        System.out.println(total);
    }
}

In this program, we first create integers a and b.  We then sum the variables and assign the sum to the variable total.

integer