/*
* This is a multiline
* comment
*/
class HelloWorldApp {
public static void main (String[] args) {
System.out.println ("Hello World!"); // This is a comment
}
}Java is another popular language that we can consider compiled,
although it doesn’t generate native code. Instead, it generates
so-called bytecodes, something like machine code for an
invented machine, the self-proclaimed Java Virtual Machine,
better known by its English acronym JVM.
DID YOU KNOW…
The concept of bytecode is not exclusive to Java. Perl, Python, and even TCL can be compiled into bytecode, although Java was the first language/environment to do this explicitly, especially because it clearly separated the process of generating the bytecode and its execution by the virtual machine. Smalltalk already supported this concept, and even the BASIC on my old MSX tokenized the programs to store them in memory in a more efficient way… something that can be seen as a primitive and rudimentary version of what we know today as bytecode.
The files with the Java programs (source code) use the
.java extension. The bytecode generated by the compiler
uses the .class extension. More complex projects that
involve many classes, group several .class files into a
.jar file which is nothing more than a ZIP file with a
certain structure.
Java can be seen as a simplified version of C++ with a more formal
and also much simpler object-oriented approach. The syntax is
practically the same. We will use ; as an instruction
terminator which we can group using the curly braces {}.
The comments are identical to those used by C++.
As we said, Java provides a much simpler (in the sense that it allows you to do less) but more formal object-oriented approach. That is why, even for our simple Hello World program, we must define a class and methods in order to print our message.
The message is printed with System.out.println which is
an invocation of a method of a static object in the System
class. Take that… But well, it’s more or less the same as C++’s
cout, but using a method instead of an operator. In this
book we will not touch the topic of object-oriented programming beyond
the strictly necessary, so for the time being, get by with the fact that
to write a program in Java you have to put all that appears in the
example code.
DID YOU KNOW…
So far, the languages we’ve seen either add or don’t add the carriage return. We will see in the next chapter how to control this, usually by calling a different function. Regarding this particular topic, there are usually two ways to name these functions. Either a lower-level function is used that allows you to control whether you want to use the carriage return, or a family of functions is used that adds the prefix
lnto the printing function.lncomes fromlineand indicates that we want to use the carriage return. We will see this in several languages. For Java,printlnprints the string with a carriage return.
One more thing. Java requires that the name of the file containing the sources and the high-level class declared in it match…. Just so you know.
To compile and execute our Java program, we will execute:
$ javac HelloWorld.java
$ java ./HelloWorld
When executing the program with java, it is not
necessary to indicate the .class extension. If we want to
generate a .jar file and then execute it:
$ javac HelloWorldApp.java
$ jar cvf HelloJava.jar HelloWorldApp.class
$ java -cp HelloJava.jar HelloWorldApp
Where HelloWorldApp is the name of our main class. We
can avoid this, by adding a manifest to our .jar file.
$ javac HelloWorldApp.java
$ mkdir META-INF
$ echo "Main-Class: HelloWorldApp" >> manifest.txt
$ jar -cvmf manifest.txt hello.jar HelloWorldApp.class
$ java -jar hello.jar
binfmt_miscAs with the
pycfiles of Python,.jarfiles (once we have given them execute permission) can be executed directly from the command line ifbinfmt_miscis configured on our system.
Java was originally developed by James Gosling at Sun Microsystems (now acquired by Oracle) and released in 1995. Gossling along with Mike Shreridan and Patrick Naugthon started the design of the language in 1991 although, as we have said, the project was not released until 1995 (an alpha version actually). The project was initially called Oak, for the oak tree that could be seen from Gossling’s office. It was then renamed Green, to finally be called Java for the coffee of Java (Indonesia).
While Java wasn’t, by any means, the first environment to use bytecodes and offer a solution with a high degree of portability, that was the main novelty that was highlighted when it was released. Initially designed for the development of interactive applications for digital television, it gained popularity with similar use, but on web pages with the so-called Java Applets.
Its use on the web declined with the advent of Flash (which has also disappeared, fortunately, thanks to HTML5 and related technologies), Java remains the original programming language of Android devices, both as a language and as a virtual machine used by other languages. Java is also still used in the enterprise environment.
DID YOU KNOW
The Java virtual machine originally used by Android devices is known as the Dalvik Virtual Machine, and it doesn’t use
.classfiles but uses a proprietary format with the.dex(Dalvik Executable) extension. The Dalvik Virtual Machine was replaced years later by ART (Android Runtime) which adds new functionalities and is compatible with the original.dexfiles. ART was introduced in Android 4.4
In Debian-based distributions, you can install the Java SDK using the following command:
apt install openjdk-17-jdk
Perhaps when you are reading this you will have to change the
17 for a higher number.
Summary
- Compiled language to bytecode
- Extension:
java - Line terminator:
; - Groups instructions with:
{...} - Multi-line comments: Using
/* .. */ - Single-line comments:
// - String delimiter:
"string" - You can print text with
System.out.println(Adds carriage return) - Does not natively support multi-line strings
- Does not support
HERE-DOCS - Entry Point:
mainmethod