Getting started with Java
This subchapter is intended to provide you with a basic understanding of Java.
The goal is not that you understand each and every aspect, but that you have a solid foundation that makes learning the rest much easier.
Code Editors
In this subchapter you will find multiple code editors like this.
They allow you to run some simple java code directly from your browser. Note that there are multiple tabs at the top. Each tab represents its own file that can be opened. Click the run button to execute the code, and after a few seconds it should show you some output in the Terminal section. The output should look somewhat like this:
▶ Running Main.main()...
Hello World!
Process finished with exit code 0
"Hello World!" being the output that the code told the program to print out. The code editors allow you to modify the code however you want. For now, you could try replacing the part within the green highlighted codes with something else, and see how the result differs.
What is Java?
Java is a Programming Language. Specifically it is an Object-Oriented-Programminglanguage (OOP). What this means exactly can wait for later.
At it's most basic, a programming language tells the computer to do a certain task. A whole programm then is a sequence of tasks for the computer to perform.
Computers themself do not read or understand human readable code. Computers work with machine code, a sequence of 0's and 1's that would be hard to decipher by human eye. For that purpose, higher-level programming languages exist. They provide human readable commands, which are then turned in to machine code by a compiler.
Java is a bit special however, it does not directly compile to machine code. One potential issue with a direct compile to machine code is that the required machine code is widely different on each platform and processors. This means that one programm might need to be compiled multiple times for different platforms, and then shipped and adjusted seperately.
Java attempts to solve this with its Java Virtual machine (JVM). Instead of compiling directly to machine code, you compile to Java bytecode. This bytecode is a language that the JVM understands and translates to the platforms required machine code. This means that any computer that has the JVM installed, can understand java code, as the JVM release for each platform builds the machine code that the platform requires.
flowchart TD
A["<b>Java source code</b><br/>.java files<br/>Your human-readable code"]
B["<b>Java bytecode</b><br/>.class files<br/>Instructions for the JVM"]
C["<b>JAR file</b><br/>.jar<br/>a zip of .class files"]
D["<b>JVM</b><br/>the Java runtime"]
E["<b>Machine code</b><br/>instructions your CPU<br/>actually understands"]
A -->|compiles to| B
B -->|bundled in to a| C
C -->|JVM opens the zip and runs the bytecode| D
D -->|translates to machine code on the fly| E
classDef pkg fill:#fde68a,stroke:#d97706
class C pkg
You do not need to fully understand this process, but its worth remembering that:
- You write your code in Java code (.java files)
- It is compiled in to java bytecode (.class files)
- The compiled bytecode is packaged in to something like a zip (.jar)
What Starsector eventually receives from you is the .jar file, not the .class files or the .java files. Modfiying .java files has no effect unless you compile said java code. On the next page we will take a deeper look in to the basics of writing simple programs with the java.