Introduction to Java for Real Cross-Platform Programming

in #technology2 days ago

image.png

Recently I went through a phase of learning or relearning a bunch of programming languages with a view to get back into developing desktop applications.

I love Python for writing scripts and automating, and C is a staple of course, but for developing GUI apps, especially apps you want to distribute to end users, the field is a lot smaller than you would expect in 2025.

So amongst the languages I stuck with, Java was surprisingly in the top 5.

Write Once Run Anywhere with Java

In my head, Java is still relatively new. Except it isn't, it dates back to 1995. Back then I learned it as the big new thing, but I moved on to C# largely other than the few clients who used JSP (Java Server Pages).

The goal was a fully object-oriented, memory-safe, truly cross-platform language. It's compiled, but not to native binaries, but to an intermediate stage which is then executed by a native translation layer. This makes it very fast but also very portable even in compiled form.

At the time the idea of running against a virtual machine (JVM) was innovative, and others followed, such as .NET.

Since then it has become the number #1 programming language for Android-based hardware, which means everything from phones to set-top boxes.

Billions of devices run Java.

Performance

You might think having Java compile to a byte code executed by the JVM rather than "to the metal", performance might take a huge hit?

Check these results from Dave's Garage:

image.png

The Java community has put a LOT of effort into performance over the years, which means it is no laggard. You get quality of life user experience, extremely robust security, along with top 5 raw performance ...

What are the JVM, JRE, and JDK and which do I need?

One source of confusion for people getting into Java is the three-letter acronym components of Java development: the JRE, JVM, and JDK. Partly the confusion comes from developer communities specifying that you need a specific or updated version of one or more of these elements, so it is worth knowing what each is for and how they interrelate.

  1. Java Runtime Environment (JRE): The JRE is necessary for executing Java programs. It includes the Java Standard Library, which provides essential classes and files like java.lang, java.util, java.io, and java.sql, among others. The JRE also contains the Java Virtual Machine (JVM), which executes Java programs by loading class files into memory and ensuring smooth operation across different platforms with automatic memory management and security checks.

  2. Java Virtual Machine (JVM): The JVM is part of the JRE and is responsible for executing Java programs. It handles platform compatibility, memory management, garbage collection, multithreading, and security checks, ensuring the program runs efficiently without requiring modifications for different operating systems.

  3. Java Development Kit (JDK): The JDK is, as the name suggests, essential for developing Java programs. That means creating, modifying, compiling, debugging, testing, and running Java. It includes the JRE (and thus the JVM), the Java compiler (javac), and the source code for Java standard libraries. It also provides tools for debugging, monitoring, and managing Java applications, such as creating jar files. The JDK is comprehensive, containing many tools and resources that may not always be used but are available when needed.

Basically, the JRE is all you need for running Java programs, the JVM is bundled in the JRE and executes them.

The JDK provides the necessary tools for developers to use Java in their applications. This is what you will need to grab if you want to get your hands dirty with Java.

Installing the Java Development Kit on your computer

Not sure if you have Java? You can see what you have installed from a terminal using:

java --version

Open (GPL) builds are available at JDK.Java.net and not open but free-to-use installers are available from Oracle.

On Linux, you can usually install the Open version of the SDK using your package manager, eg.

sudo apt install openjdk-21-jdk

snap install openjdk

Visual Studio Code has an Extension Pack for MacOS and Windows that is very helpful for getting up and running real quick with a nice (and free!) development environment, but you do not need an IDE necessarily.

Hello World in Java

Open your text editor and create a new file, calling it something like Hello.java:

public class HelloWorld {

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

}

Now compile either with your IDE or using the terminal:

javac Hello.java

You should see a new file called something.class

Then, if all goes well, you should be able to run it:

java Hello (where Hello is the name of the class that was generated above)

image.png

Java Hello World Breakdown

Here is what all that means:

  1. Public: The main method is public so that the Java Runtime Environment (JRE) can access and call it from outside the class. If it were private or protected, the JRE wouldn't be able to run it.

  2. Static: The main method is static because it needs to be called on the class itself. This allows the JRE to execute the method without creating an object of the class.

  3. Void: The main method has a return type of void because it does not need to return any value. Once the method completes its execution, the program ends without returning anything. You might note that in C or BASH we might want to return a number indicating everything was ok or that there was an error number generated.

  4. Main: The method must be named main because the JRE specifically looks for this name to start the program. This is a convention that must be followed.

  5. String[] args: This is an array of strings so that you can pass arguments to the program at runtime.

Wait, it gets easier

Java has made it so you do not need all those lines or even to compile before running:

image.png

void main() {
    System.out.println("Hello World!");
}

I am in two minds about this though for anyone actually wanting to learn Java versus see Hello World running because the OOP aspects are really important to understand ...

Where next?

Java is a super-mature programming ecosystem today. You can make everything from desktop and mobile apps to big iron server backends.

Even today in 2025, C# is really aimed at Windows if you want the full experience, and other languages are often amazing but do not have the same level maturity of tools and community support.

Java really is "write once run anywhere" as the creators intended.

Sort:  

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).


 
You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.