Java: Hello Galaxy!

Today we’ll be looking at a classic program written universally in almost every language. You might be familiar with the classic “Hello World!” program, but in this section, we’ll print out “Hello Galaxy!” instead to keep things entertaining. This is a common program to write when learning a new language, I use this program to ensure that the programming language installed and the tools that I will be using (such as the text editor) are working correctly. Here are the following tools you will need:

  • Text Editor (I’m using Visual Studio Code)
  • Java
  • Java Compiler
  • Windows Terminal

Lets start by writing a Java Class. Within a directory of your choosing, create a file called HelloGalaxy.java and add the contents listed below:

public class HelloGalaxy {
     public static void main (string[] args){
         System.out.println("Hello Galaxy!"); 
     }
 }

within the Terminal of your directory where the program is located, lets compile the HelloGalaxy class by using the javac command:

> javac HelloGalaxy.java

If the compilation was successful you should see a HelloGalaxy.class file within the same directory. We can now run our java program by executing the following command below within the terminal:

> java HelloGalaxy

Our output should look something like this:

> Hello Galaxy!

There you have it! our first Java Program!, before we pop bottles of champagne and celebrate, let’s take some time to quickly debrief here on what’s going on under the hood of Java. Java is a compiled programming language, meaning that you must compile the java file before running the program, upon compilation, this translated the human-readable program to JVM byte code or (Java Virtual Machine byte-code). We defined class like the one listed below:

public class HelloGalaxy{

}

We’ve declared a class in Java named HelloGalaxy where you might have noticed the H and the G are both capital letters, this is following a convention called Pascal Case where the first letter and the begging letters of each word are capitalized. We’ve also declared an access modifier of public this entails that any code within the java program can talk to the HelloGalaxy class. (We’ll elaborate on this later on).

Next, we’ve declared something commonly known as the main method in Java:

public static void main (string[] args){

}

Ultimately, you will see this method in every Java program, as it is the “ignition” or main entry point to starting a Java program. If you’d like a detailed explanation of what’s going on under the hood of this program, you could take a look at the bullet points below, but we’ll elaborate more on the granular details throughout this course.

  • The public keyword – as we discussed earlier allows this method to be accessed anywhere within the code
  • static – a modifier within Java, and we’ll elaborate more on this later as well.
  • void – specifies that the method should not return a value (we’ll talk about the return variables in the future as well).
  • (string[] args) - the ( ) represent the parameters we can pass within this method, in this case the string[] args are the arguments we are passing through.
  • System.out.print(“Hello Galaxy!”); – System.out.print() is a feature within the general Java library that we can use to print what we want which defaults to the console. in this case, we’ve passed in a string argument with the value of Hello Galaxy!

Up next, we’ll talk about different data types that java has to offer.