Java is a general-purpose, OOP language designed with the idea of Write Once, Run Anywhere (WORA), meaning that the Java code would work virtually in every computer without the need of re-compilation like C or C++. In order to obtain this unique portability, the computer needs to have the JVM (Java Virtual Machine) installed. Nowadays, Java is the most used programming language in the world, used for almost anything, from embedded programming to large software development, web application development and supercomputers. Java has 3 main implementation named Java EE, Java SE and Java ME meaning respectively Java Enterprise Edition, Java Standard Edition and Java Micro Edition (used in the abbreviated form as J2EE, J2SE and J2ME). Java, firstly developed at Sun Microsystems which merged into Oracle Corporation, was designed with 5 main principles in mind:
-
Simple, object-oriented and familiar
Java makes programming simple using its build-in classes, object-oriented programming (high level of code abstraction) and its syntax is derived from C and C++, which makes it simple for programmers to adapt.
-
Robust and secure
If you develop using Java, you need your software to be reliable. This is achieved using compile-time AND run-time checking, memory leaks don't exist because of Java's garbage collector. If you want to develop client-side application using Java applets, the language itself doesn't allow you to create malware, viruses, or get your hands in the system's files.
-
Architecture neutral and portable
As stated before, Java code is a WORA code. It doesn't rely on the architecture, so you don't have to recompile the code so you can use it in another computer. The Java Compiler creates bytecode, not machine code. Bytecode runs on the JVM and has nothing to do with the machine where it runs, which makes it architecture neutral, unlike C and C++.
-
High performance
Performance is what divides good programming languages from better programming languages. Through automatic garbage collector we get rid of any memory leaks. Some parts of the software can be written into lower level languages and compiled into machine code, which makes the software run faster than interpreted softwares.
-
Interpreted, threaded and dynamic
The Java Interpreter makes the link time shorter, giving you the benefit of faster development, unlike compile-link-test used in C and C++. Also, Java lets you create concurrent applications, using the Thread class, which gives your application higher performance and a shorter execute time.In order to start developing with Java programming, we should firstly obtain JDK from the Oracle website.
By setting up the development environment, you will have to install the JDK (Java Development Kit) and JRE (Java Runtime Environment) and probably even an IDE (Integrated Development Environment).
At this time, the latest Java version is Java 8. You can download it for free here. Make sure to download and install Java SE JDK (which is required to develop) and not JRE (which is required to run Java apps). Keep in mind that JDK includes JRE.
In order to check if Java is installed properly, we open the command-line tool (CMD in Windows, terminal in Linux and Mac OS) and type:
java –version
We can see something similar shows on the terminal:
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode, sharing)
You can now start developing Java applications, by writing some code, compiling it, and then executing it. You could use an IDE, or a plain editor and command-line compilation. Popular Java IDEs are NetBeans, Eclipse IDE, IntelliJ IDEA, etc.
If you don't want to use an IDE you need to configure the JDK (add it to path). See this article about adding Java to PATH. In order to check if the configuration is done properly, you should close and reopen the terminal and type javac –version
. This should show on the terminal something similar to:
javac 1.8.0_45
Now that the JDK is configured properly, you can start programming Java.
This first program will only print a string of text in the terminal. Java also supports Graphical User Interface (GUI) programming, but we'll come to that later.
You can write our code in the IDE's text editor or in our favorite text editor (as long as it is not a word processing software like MS Word). If you use a text editor like gedit, Notepad++, emacs or else, you should save your code in a file called <filename>.java
. Every Java code has this *.java
format. Write this program in the editor:
/*
* We are ready to create our
* first Java program
*/
public class FirstJavaProgram {
public static void main(String args[]) {
// this line of code prints in the terminal
System.out.println("We are learning Java programming");
}
}
Save it as FirstJavaProgram.java
. To compile it, open the terminal, go to the directory where FirstJavaProgram.java
is saved and execute javac FirstJavaProgram.java
. This creates a new file named FirstJavaProgram.class
, which is our code compiled into bytecode. To run this program execute:
java FirstJavaProgram
And in the terminal this string of text shows up:
We are learning Java programming
This means that the program works properly; the compiler didn't show up any error or warning. Now I'll will explain the program line by line.
The first three lines are:
/*
* We are ready to create our
* first Java program
*/
This is a multiline comment. Everything between /*
and */
is a multiline comment that is used for documenting the code.
The next line, public class FirstJavaProgram
creates a class named FirstJavaProgram
. In every Java program, there must be at least one public
class which is the main class of the program. The name of the class and the file storing the code should match. If we declare a public class named Calculator
, we should name the source file as Calculator.java
, and the compiled class would be Calculator.class
.
The next line is the main function. The main function is the entry point of the program; that is, the program execution starts at the top of the main function and ends at the bottom of the main function. The public static
part will be clear when dealing with OOP principles and the void
part when dealing with methods (functions). The main function has an argument, String args[]
, which allows the program to be called with command-line arguments.
The next line is a single line comment. Everything in the line after the //
is a comment and it is ignored by the compiler (it doesn't affect the code). This single line comment can reach until the end of the line. Beside the multiline comment (between /*
and */
) and the single line comment (//
) there is also another kind of comment, used for generating JavaDoc. The JavaDoc comments goes between /**
and */
and its used for generating HTML files used for documenting the code. This is an example of a JavaDoc HTML file.
The other line is a statement inside the main function which is used to print something in the system standard output (stdout), that is the terminal. System.out.println()
is the function used for this; it prints a string of text in the terminal (println stands for print line). The next lines of code are brackets, used to end the blocks of code.
The public class
starts a block of code with the {
and so does the main function. Control statements, functions, classes, interfaces, all of these create blocks of code that should end in }
, otherwise you will get compile errors. If you use an IDE, you don't have to worry about the blocks of code; they are automatically created when you press the bracket that opens the block, and in most cases, you will get red notifications even before compiling the code.
Now that you learned this basic program, you are able to write in the terminal window whatever you want using the System.out.println()
function. You can also use it to print the result of a mathematical expression. Try this:
public class SimpleMath {
public static void main(String args[]) {
System.out.println((10/5) * 2);
}
}
It will calculate the division in brackets (10/5) and then multiply the result by 2 (*
is the multiplication operator for Java). So this program will display 4 in the terminal.