Getting started with Java

Java Wallpaper

In this new post “Getting started with Java”, I share with you my consideration about this programming language in comparison to C#. As a .NET developer, I love C# but I want also to be open-minded towards Java or other languages at least to understand if there are more skills I can add to myself.

Stackoverflow technologies 2022

As you know, Stackoverflow publishes every every an extensive survey about people and technologies. The survey for 2022 is available here. In this survey, we see the distribution of the technologies among all the respondents (professional developers and students).

So, the most popular language is JavaScript, HTML and CSS, in 6th position Java and then at the 8th position C#. Then, PHP and other languages. Out of curiosity, Swift is at the 19 place with 4.91% and R at the 20th place with 4.66%. If iOS and macOS are very popular, how do developers create their apps for iOS? There is no answer in this survey.

Stackoverflow - Programming, scripting, and markup languages: all respondents - Getting started with Java
Stackoverflow – Programming, scripting, and markup languages: all respondents

Now, if we have a look only at the professional developers, the scenario is slightly different. Java is at the 6th place and C# follows. The gap is about 4%, not very big.

Stackoverflow - Programming, scripting, and markup languages: all respondents - Getting started with Java
Stackoverflow – Programming, scripting, and markup languages: Professional Developers

What makes Java interesting?

Java was developed by a group of developers at Sun Microsystems in 1995 and is now owned by Oracle. The goal of the developers who designed Java was to create a language that could run on appliances. They were already thinking ahead to the time we live in now, where houses are full of smart devices and smart appliances. This was one of Java’s selling points — that you could write your code once in the language and run that code anywhere. Not just on every operating system but every type of hardware.

However, it would be quite a few decades before smart devices became a way of life. Despite this, Java still became popular and for a completely different reason. Java was released about the same time the Internet was born. Java had a feature called applets that could run inside a web browser. A lot of web applications were built in Java when most websites still consisted of static pages.

Java gave web developers the ability to build dynamic websites that reacted to user input, and its popularity took off from there.

Java was also based on the C and C++ programming languages, which were very popular. C++ was usually the programming language taught in computer science courses in those days, so many programmers were familiar with it, even today. They could apply that knowledge of C++ to programming in Java.

What is Java used for?

Java can be used in many different applications, but here are the most popular ways the language is used:

Android mobile apps

Java is the official language for Android mobile app development. In fact, the Android operating system itself is written in Java. Even though Kotlin has recently become an alternative to using Java for Android development, Kotlin still uses the Java Virtual Machine and can interact with Java code. Today, Android has 85% of the global market share for mobile devices. Therefore, developing Android apps is probably the most popular use of Java just because of the prevalence of Android phones.

The Java programming language can be considered as the official language for mobile application development. Most of the android applications build using Java. The most popular android app development IDE Android Studio also uses Java for developing android applications. So, if you are already familiar with Java, it will become much easier to develop android applications. The most popular android applications Spotify and Twitter are developed using Java.

Desktop applications

Java has been used to create desktop applications since its inception. AWT, Swing, and JavaFX are Java libraries that give desktop application developers pre-built components like buttons, menus, and form fields that they can use to build full-featured desktop applications.

Web applications

Java first became popular as a web development language because it provides applets that can run in a web browser. Applets are a thing of the past, but Java is still very popular for creating back-end web applications, which run on a web server. Now web developers use Java technologies like Struts, Servlets, or JSP instead of Applets to create all types of full-featured web applications.

Also, it provides vast support for web development through Servlet, JSP, and Struts. It is the reason that Java is also known as a server-side programming language. Using these technologies, we can develop a variety of applications. The most popular frameworks Spring, Hibernate, Spring Boot, used for developing web-based applications. LinkedIn, AliExpress, web.archive.org, IRCTC, etc. are the popular websites that are written using Java programming language.

Game development

Java is a free, open-source language. Many game developers use it because they can get started without paying any licensing fees and because of the powerful Java 3D game engine, JMonkeyEngine. Some video games written in Java include Tetris, The Sims 3, Space Invaders, Street Fighter II, and Contra. One of the best-known Java games, Minecraft, was created by a single developer.

Big data processing

Java and big data go hand in hand. Many of the top applications used for big data are written in Java. Hadoop is a Java framework that helps data scientists process large datasets. Spark is a tool that data scientists use for stream processing, machine learning analytics, and other big data processes. Storm handles real-time data streams. All of these frameworks are written in the Java.

IoT applications

Java was originally designed to run on all types of hardware, making it one of the main programming languages used for the Internet of Things, or IoT. IoT refers to a network of physical devices that connect and exchange data over the Internet. The devices include smartwatches, wearables, smart TVs, smart lighting, and more.

Distributed applications

Many distributed applications run in a cloud environment and are designed to scale when the load changes. But distributed applications are not necessarily easy to deploy and manage. Java provides the Java Intelligent Networking Infrastructure or JINI to make distributing applications simpler. JINI is an infrastructure to provide, register, and manage distributed Java applications.

Cloud-based applications

Java is also heavily used in cloud-based applications. Because of its low cost and wide use, many companies use it to develop SaaS, IaaS, and PaaS services in the cloud.

Enterprise Development

Java is used heavily in enterprise development to build intranets and internal software for all types of businesses, big and small. Java Enterprise Edition (Java EE) is specifically designed for enterprise development. It comes with network applications, web services, and a scripting environment that make setting up an intranet with Java simpler.

Blu-ray

Finally, blu-ray is a technology heavily based on Java.

BD-J, or Blu-ray Disc Java, is a specification supporting Java ME (specifically the Personal Basis Profile of the Connected Device Configuration or CDC) Xlets for advanced content on Blu-ray Disc and the Packaged Media profile of Globally Executable MHP (GEM).

BD-J allows bonus content on Blu-ray Disc titles to be far more sophisticated than bonus content provided by standard DVD, including network access, picture-in-picture and access to expanded local storage. Collectively, these features (other than internet access) are referred to as “Bonus View”, and the addition of internet access is called “BD Live”. BD-J was developed by the Blu-ray Disc Association. All Blu-ray Disc players supporting video content are required by the specification to support BD-J. Starting on October 31, 2007, all new players are required to have hardware support for the “Bonus View” features, but the players may require future firmware updates to enable the features. “BD Live” support is always optional for a BD player.

Sony’s PlayStation 3 has been the de facto leader in compliance and support of BD-J. The PlayStation 3 added Blu-ray Profile 1.1 support with a firmware upgrade and was used to showcase BD-Live at CES 2008 in January.

Top Companies that Use Java

There is a majority of companies such as, Uber, Pinterest, Google, Instagram, Spotify, Netflix, Airbnb, etc. that use Java in their tech stack. We have listed some companies or organizations and their projects. It will help you to decide which programming language you have to choose for the next project.

NASA Word Wind

NASA Word Wind is the project of an independent agency of the U.S. federal government NASA. It is a fully 3D virtual globe that provides geographic information. It uses imagery and aerial photography received from the NASA satellite and builds 3D models of the planets.

Also, it is an open-source proprietary software written in Java and supports all operating systems. In this project, the OpenGL API is used to provide 2D and 3D graphics that interact with the graphics processing unit. It also shows the data in real-time by using the GPS plugin such as displaying clouds, hurricanes, earthquakes, etc. Using this application, we can search for locations by geographical names, set visible layers and viewing angles, and much more.

Netflix

Netflix is one of the most popular and largest US entertainment company that provides movies and TV shows on streaming multimedia. Most of the applications of Netflix is developed using Java. With a slight mixture of C++, android and android TV applications are almost build in Java.

Spotify

Spotify is an online audio streaming service that uses Java to implement the functionality of its web application. For example, log and stabilize, and data transfer. The android application of Spotify uses Java.

Minecraft

Minecraft is a famous computer game that is written in Java. The Minecraft Java edition comes with Java 1.8 and Minecraft used it by default.

Get ready for Java

Install a Java Development Kit (JDK)

A Java Development Kit (JDK) is a bunch of software that makes all Java programs work. To install a Java Development Kit, you have few choises:

  • Download the official JDK from Oracle
  • Visit the website adoptium.net and follow that website’s instruction

If Oracle is the official one and the “creator” of Java (the real creator was Sun Microsystem acquired by Oracle), why do I have to use adoptium? The problem with Oracle’s official version is that it comes with a long, somewhat confusing list of legal requirements. Plus, the cost of Oracle Java licence is raising.

Install an integrated development environment

Now, getting started with Java requires an integrated development environment (IDE). IDE is a program to help you compose and test new software. It is like Visual Studio. Here’s a list of the IDE’s that are most popular among professional developers:

Test your environment

So, my choice is Eclipse only because it is the historical IDE for Java. When you launch Eclipse, it asks already a lot of things about the environment, where to place files and workspace and so on. I select the default for every choice.

Now, finally Eclipse starts. What I notice immediately is that the help is not helping you: the instruction doesn’t have the correct instruction. This makes me crazy! Welcome to Java!

Eclipse at the first start
Eclipse at the first start

Create the first project

First, we have to create a new Java Project. Click on the menu File, then New and then Java Project.

Create a new Java project
Create a new Java project

Next, we see this easy window to configure the new project. I only add the Project name at the top and I accept all the other default values. Then, click Next that creates the project and show some info about it and then Finish.

New Java project window
New Java project window

So, it is time to create a Java Class where we add the code to print as an output a simple string. Again, from the menu File > New select Class. You see a window like the following.

Create a new class
Create a new class

Remember to check the option for public static void main(String[] args). As the name add HelloWorld and click Finish. In the new class, add the line to print a value in the console.

public class HelloWorld {
	public static void main(String[] args) {
		System.out.print(12345);
	}
}

Now, press the Run button. So, Eclipse shows me a Save and Launch window asking me what resource to save. I save everything.

Save and Launch
Save and Launch

There is already an error. No comment. I understand that there is a conflict between the HelloWorld.java and the module-info.java. So, we have to delete the module-info.java and then the program starts.

What is Java Bytecode?

Java bytecode is the instruction set for the Java Virtual Machine. It acts similar to an assembler which is an alias representation of a C++ code. As soon as a java program is compiled, java bytecode is generated. In more apt terms, java bytecode is the machine code in the form of a .class file. With the help of java bytecode we achieve platform independence in java.

How does it works?

When we write a program in Java, firstly, the compiler compiles that program and a bytecode is generated for that piece of code. When we wish to run this .class file on any other platform, we can do so. After the first compilation, the bytecode generated is now run by the Java Virtual Machine and not the processor in consideration. This essentially means that we only need to have basic java installation on any platforms that we want to run our code on. Resources required to run the bytecode are made available by theJava Virtual Machine, which calls the processor to allocate the required resources. JVM’s are stack-based so they stack implementation to read the codes.

Java Bytecode Generic approach
Java Bytecode Generic approach

Advantage of Java Bytecode

Platform independence is one of the soul reasons for which James Gosling started the formation of java and it is this implementation of bytecode which helps us to achieve this. Hence bytecode is a very important component of any java program. The set of instructions for the JVM may differ from system to system but all can interpret the bytecode. A point to keep in mind is that bytecodes are non-runnable codes and rely on the availability of an interpreter to execute and thus the JVM comes into play.

Bytecode is essentially the machine level language which runs on the Java Virtual Machine. Whenever a class is loaded, it gets a stream of bytecode per method of the class. Whenever that method is called during the execution of a program, the bytecode for that method gets invoked. Javac not only compiles the program but also generates the bytecode for the program. Thus, we have realized that the bytecode implementation makes Java a platform-independent language. This helps to add portability to Java which is lacking in languages like C or C++. Portability ensures that Java can be implemented on a wide array of platforms like desktops, mobile devices, severs and many more. Supporting this, Sun Microsystems captioned JAVA as “write once, read anywhere” or “WORA” in resonance to the bytecode interpretation.

Example

Consider the following Java code:

outer:
for (int i = 2; i < 1000; i++) {
    for (int j = 2; j < i; j++) {
        if (i % j == 0)
            continue outer;
    }
    System.out.println (i);
}

A Java compiler might translate the Java code above into bytecode as follows, assuming the above was put in a method:

0:   iconst_2
1:   istore_1
2:   iload_1
3:   sipush  1000
6:   if_icmpge       44
9:   iconst_2
10:  istore_2
11:  iload_2
12:  iload_1
13:  if_icmpge       31
16:  iload_1
17:  iload_2
18:  irem
19:  ifne    25
22:  goto    38
25:  iinc    2, 1
28:  goto    11
31:  getstatic       #84; // Field java/lang/System.out:Ljava/io/PrintStream;
34:  iload_1
35:  invokevirtual   #85; // Method java/io/PrintStream.println:(I)V
38:  iinc    1, 1
41:  goto    2
44:  return

In any case, you don’t have to understand the code in the bytecode or change it. So, we don’t care so much about it but we know it is exist.

What is the JVM?

The JVM in simple words is an engine that reads compiled code in a format that is specified from a Java Virtual Machine Specification and executes it on the current machine. The advantages of this approach is mainly cross-platform compatibility, as the compiled code, which is called bytecode, is supposed to be platform agnostic.

That means that the code compiled in a Linux machine and the code compiled in a Windows machine should work in the JVM either ways. We can copy the compiled .class files from linux to windows and run them there without issues and vice versa.

In other words, when you install Java on your Windows PC, the java tool will use a platform specific runtime and a JIT compiler to run your code on Windows. The javac on the other-hand will compile your .java files to the generic bytecode format.

The Bytecode itself is a format that follows a specification from the Java Virtual Machine Specification. It has various features enabled based on the current version. Those features are dictated based on JSR’s or Java Specification Requests and based on the current implementation.

Add some documentation

Now, look at this code

/*
 * Example of a Java code
 */

/**
 * The HelloWorld class displays text
 * on the computer screen
 * 
 * @author  Enrico Rossini
 * @version 1.0 01/02/2023
 * @see     java.lang.System
 */
public class HelloWorld {
	/**
	 * The main method is where
	 * execution of the code begins
	 * 
	 * @param args Generic arguments 
	 */
	public static void main(String[] args) {
		System.out.print("This is a test"); // Replace 12345 with This is a test
	}
}

A comment is a special section of text, inside the program, whose purpose is to help people understand the program. There are 3 types of comment:

  • traditional comments: at the top of the file, you see this type of comment. The comment begin with /* and ends with */. Everything between the opening /* and the closing */is for human eyes only.
  • end-of-line comments: the // Replace 12345 with This is a test is an end-of-line comment. An end-of-line comment starts with 2 slashes and goes to the end of a line of type.The compiler doesn’t translate the text inside the end-of-line comment
  • Javadoc comments: a javadoc comment begins with /**. This is a special kind of traditional comment and it is meant to be read by people.

Javadoc

Now, all IDE provides a way to generate the documentation starts with the comment in your code and they look the same. To generate the documentation, on Eclipse, click on the menu Project and then Generate Javadoc.

Generate Javadoc
Generate Javadoc

Now, a new window opens with the options you want for the documentation. Here, I just click on Finish.

In the hierarchy of the project, there is not a new doc folder that contains all the HTML and CSS file for the documentation. If you click in the index.html, you see a documentation like that.

The javadoc page generated from the code
The javadoc page generated from the code

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.