Advertisements

Getting started with Java programming

The Java programming language is a powerful tool for people to get computers to help perform work on behalf of humans. Since its inception, it had been well adopted by many companies in the creation of many great services and technologies.

This post is for my younger self and people who wanted to get started in learning Java programming but find it hard to get started.

What is Java programming?

According to the dictionary, programming, as a noun, is

the act or process of planning or writing a program.

And with Java as the bridge between the human and the computer, Java programming is the act or process of planning or writing a program for the Java virtual machine.

The Java Compiler and the Java Virtual Machine

At the bare minimum, there are two mandatory tools that we have to use to have the computer perform work for us via Java programming; the Java Compiler and the Java Virtual Machine.

The Java Compiler looks at the English-like codes that we write and generates Java bytecode for the Java Virtual Machine. The Java Virtual Machine takes the Java bytecode from the Java Compiler and runs it on the computer.

Basic workflow of Java development

Based on the characteristics of the Java Compiler and Java Virtual Machine, we can go through the basic workflow to get a running Java program:

  1. Write the source code with the Java syntax in a text editor and save it with the .java extension.
  2. Compile the source code with the Java Compiler, this will produce at least a .class file for each .java file.
  3. Run the .class file(s) produced by the Java Compiler with the Java Virtual Machine (JVM). The JVM will then interpret the .class file(s) and perform the work that is described within.

Getting the J2SE Java Development Kit (JDK)

To begin exploring the wonders of Java programming, we need to get the J2SE Java Development Kit. The Java Development Kit contains the Java Compiler and the Java Virtual Machine mentioned earlier. At the time of writing, JDK 8 is the latest stable version available for download.

Download and run the installation file that is suitable for your computer and you should be able to begin your learning journey.

After you had installed the JDK on your machine, you should be able to find a couple of programs inside the bin folder of the folder where you had chosen to install your JDK.

For instance, after installing JDK 1.8.0_91 on my windows machine, I can find the folder C:\Program Files\Java\jdk1.8.0_91 on my hard disk. And inside that folder, bin folder which contains the following list of applications:

  • appletviewer.exe
  • extcheck.exe
  • idlj.exe
  • jabswitch.exe
  • jar.exe
  • jarsigner.exe
  • java-rmi.exe
  • java.exe
  • javac.exe
  • javadoc.exe
  • javafxpackager.exe
  • javah.exe
  • javap.exe
  • javapackager.exe
  • javaw.exe
  • javaws.exe
  • jcmd.exe
  • jconsole.exe
  • jdb.exe
  • jdeps.exe
  • jhat.exe
  • jinfo.exe
  • jjs.exe
  • jli.dll
  • jmap.exe
  • jmc.exe
  • jmc.ini
  • jps.exe
  • jrunscript.exe
  • jsadebugd.exe
  • jstack.exe
  • jstat.exe
  • jstatd.exe
  • jvisualvm.exe
  • keytool.exe
  • kinit.exe
  • klist.exe
  • ktab.exe
  • msvcr100.dll
  • native2ascii.exe
  • orbd.exe
  • pack200.exe
  • policytool.exe
  • rmic.exe
  • rmid.exe
  • rmiregistry.exe
  • schemagen.exe
  • serialver.exe
  • servertool.exe
  • tnameserv.exe
  • unpack200.exe
  • wsgen.exe
  • wsimport.exe
  • xjc.exe

Among the many applications inside the bin folder, javac and java will be the ones that we will use the most in our Java development work. The javac application is the Java Compiler, while the java application is the Java Virtual Machine. The Java Virtual Machine is the application that will breathe life into our Java program.

Creating a simple Java program

Typically, in Java programming, we will need to create an entry point for the Java Virtual Machine to do what we want it to do. Let us write a simple program to understand the typical entry point the Java Virtual Machine will look for.

Suppose that we want to build a Java Program that will print a simple message to the command line shell where it runs from, we can write the following code in our text editor:

public class SimpleMessageProgram {

    public static void main(String[] args) {
        System.out.println("Good day stranger!");
    }

}

and name it as SimpleMessageProgram.java. This will complete step 1 of the basic workflow which we had covered in the earlier section.

The next step is to compile the source code that we had written with the Java Compiler. To compile the source code, we can type the following command into our command prompt:

javac SimpleMessageProgram.java

After compiling the source code that we had written with the Java Compiler, we should be able to see SimpleMessageProgram.class in the same directory.

Finally, to see our Java Program in action, we can run the following command into our command prompt to get the Java Virtual Machine to run our Java Program:

java SimpleMessageProgram

Note that we had excluded the file extension.

We should see the message "Good day stranger!" in our command prompt after the Java Virtual Machine had ran our Java Program.

Understanding the program code

Line 1 tells the Java Virtual Machine that there is a section of code that it should inspect. Since we had named our file as SimpleMessageProgram.java, we would need to mark that section of code with 'public class SimpleMessageProgram', and a pair of braces - '{}'. This is known as a class definition.

Line 3 tells the Java Virtual Machine that we have a main method for it to run. We define the main method with 'public static void main(String[] args)' and a pair of braces - '{}'. That main method is the entry point for the Java Virtual Machine to do what we want it to do. Since we just want to print a simple message, we will create our instructions within the pair of braces of the main method. Line 4 is how we can print out the message "Good day stranger!" to the command prompt.

Advertisements

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people.

Advertisements