umma.dev

Learning Java: Part One

Java is known as a strongly typed programming language. In this post I go through the basics of setting up your machine to read, write and run Java programs.

What is Java?

It’s platform with a number of different solutions. It’s most common use case being a back-end programming language to program APIs.

Set Up

Mac

brew install java

java --version

It should look like this:

openjdk 18.0.2 2022-07-19
OpenJDK Runtime Environment Homebrew (build 18.0.2+0)
OpenJDK 64-Bit Server VM Homebrew (build 18.0.2+0, mixed mode, sharing)

If you get the following:

The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.

Do the following:

sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk \
     /Library/Java/JavaVirtualMachines/openjdk.jdk

Java Versions

The current Java version at the time of writing this post is Java 9.

Documentation

https://docs.oracle.com/en/java/

Create Your First Program

mdkir [directory-name]
cd [directory-name]
touch [file-name].java

Open up the Java file in the code editor and insert the following code.

public class [file-name] {
  public static void main(Stringp[] ars) {
    System.out.println("This is my first Java program!")
  }
}

What does this code do?

It should print out, β€œthis is my first Java program!” in the terminal.

Run Your First Program

Save the file and close it. Head over back to the terminal.

javac [file-name].java
java [file-name]

Other Simple Code Examples

public class [file-name] {
  public static void main(Stringp[] ars) {
    System.out.println(1+2)
}
public class [file-name] {
  public static void main(Stringp[] ars) {
    System.out.println("hello" + "world")
  }
}

Part two will look at different variable and data types.