Java is widely known to be used in Enterprise Software Application, with long history and proven quality trough many years which is many university use Java programming language in their course. Sadly, after so many years people said that Java is bad for introductory programming language, they said that Java is full with scary things for a beginner.

As Java developer, sadly I agree with that statement. Java is not for a beginner or someone who want learn to code for the first time. I know someone will suggested JavaScript, Ruby, or Python because how easy and popular those language to build a website or API. There is a myth that Java is too complex, heavy weight and will reduce your productivity.

The contrary, Java actually is the most easiest, rich feature and productive one for developer and hundreds of thousands of applications worldwide are written in Java. You could do almost anything with Java, mobile application (Java ME and Android), enterprise application, even for Internet of Things (IoT) and there is still plenty advantage of Java programming language.

Knowing some of you still thinking how bad Java as programming language, I would like to share about how actually Java is really easy and simple for developing a website or an API. Of course I admit even almost twenty years Java still have many ugly and weird things but the good things is people behind Java keep working hard to improve the quality of Java programming language.

IMPORTANT

This is not for someone who just started learn to code, this need at least basic in programming language, know about client-server models since I will show how easy to build website or API with Java. Also it will be good if you understand the different between static and dynamic typing of programming language and basic knowledge of Object Oriented Programming (OOP). Oh, and keep in your mind that I’m using Unix Operating System which is there will be a lot of command line.

You said this is to0 much? well, learn about programming is not done when you can print Hello World and programming is not easy as write Hello World. It’s a long and arduous journey, technology changing very quickly sometimes what you know will obsolete in next three years so you need to keep learning and practice. I don’t have any intention to scare you, after all I started with some people already said that Java is full of scary things for beginner, right :)

Prerequisite Coding with Java

Before you could start to write Java code, you need a Java Development Kit (JDK) which can be downloaded in Oracle Website. I know with so many things that look like important to be downloaded, especially when you see there is Java SE, Java EE, Java ME you must be have a lot question. Right now you just need to download a JDK on Java SE section.

Now, on Java SE section there is JDK and JRE for simple explanation if you just want to run Java project choose JRE, but if you want to build Java project choose JDK. JRE is like a minimalist package, but since you want to learn about Java, download the JDK. After installing JDK, you need to configure the PATH so it can be recognized well on your machine.

Setting Java PATH in Windows

  • Right click on your My Computer icons
  • Click Environment Variables and then choose Advanced Tab
  • Add on your PATH value, just copy paste your java directory path after semicolon (;). You need to copy your directory until bin directory, for example C:\Program Files\Java\jdk1.8.0_45\bin; and don’t forget to add semicolon (;) in the end of line.
  • Now create JAVA_HOME on System Variables, in this section you don’t need to copy your directory until bin, you just need to specify like this C:\Program Files\Java\jdk1.8.0_45.

path

Setting Java Path on Linux

Open your command line interface (CLI) and make sure where is your JDK directory with which java, there will be vary depend on your Linux Distribution, for example on Slackware 14.2 64 bit, command which java will show /usr/lib64/java/bin/java, from here you just need to copy the directory source like this /usr/lib64/java. Please replace the java directory with your own directory.

export JAVA_HOME=/usr/lib64/java
export MANPATH="${MANPATH}:${JAVA_HOME}/man"
export PATH="${PATH}:${JAVA_HOME}/bin:${JAVA_HOME}/jre/bin"

Everything should worked now, to check if your configuration is done just execute command java and javac.

javac -version
java -version

Java Code Structure and Syntax

package id.swhp

public class HelloWorld {

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

}

Let’s started with knowing Java code structure, yes to print a simple Hello World you need to write those boiler plate code. I know Java is not sexy like JavaScript, Ruby, or Python, but don’t judge programming language from it’s syntax. You need to dig deeper about what can Java offer to solve your problem efficient and elegantly. Well, you still need to understand Java code and syntax after all, so here I will give simple explanation from code above.

Oh, please remember I’m not using any fancy Integrated Development Environment (IDE) and so you too. Choose your favorite text editor like sublime, atom, notepad++ or even vim. Rule number one and the most important thing is always named your class with your Java file name. The different name will cause a compile error.

package id.swhp;

Sometimes you want to organize your Java files into a directory either to keep everything organize or you want to organize them by their responsibility, domain, or whatever your team has decided. So, package is like keyword to tell you that HelloWorld.java is located in directory /id/swhp. This is not necessary when you just only have one Java files in root directory.

public class HelloWorld {
}

Unless you came from declarative or functional programming I bet you understand about class in OOP. Yeah, Java was designed to comply with OOP and you will see that Java heavily depend on class and object.

public static void main(String[] args) {
}

This is the most complicated part to explain, because if you want to build a console based project or desktop based using Swing this will be necessary but if you build a Web Archive (war) project you won’t see this code anywhere. For simple explanation every time Java started it will look this main method as the starting point to execute the line of code, no matter how many Java files you made at least one class should contain this main method. You will get an error if there is no main method on your Java class.

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

Finally, to print the text into console you need to write System.out.println() is same as console.log in JavaScript or print in Python puts on Ruby.

Let’s Test Your Java Code

Now, after you understood a little about Java code structure and syntax it’s the time to compile and run your Java code. Yes, Java is compile based programming language unlike JavaScript, Ruby or Python. Before you could run your code you need to compile your .java files, and to do that you just need to execute command javac following with the name of Java files.

javac HelloWorld.java

The good thing about compiled programming language you will see an error earlier when something is not correct, for example you put a String value on integer variable, you missed a semicolon (;) or maybe you typed a wrong variable name. When the compiler not found any error you will see a .class files which is the result of compiled Java files. To execute the .class files you need to type command java following the name of files without any extensions.

java HelloWorld

Then you will see the messages Hello World! on your console.

The Conclusion

No, it’s not over yet. This is just the beginning I still have plenty things to show you about the good side of Java for building a Web Archive (war) project. It will come on the next article, this is just like the title is an introductory to Java.

    To a resolute mind, wishing to do is the first step toward doing. But if we do not wish to do a thing it becomes impossible.

    — Robert Southey

Still have a lot question about Java, don’t worry there is a lot of resources to learn Java for beginner, and I will recommend book Head First Java by Kathy Sierra and Bert Bates for beginning.