Java from Scratch

Introduction to Java

Welcome to the world of Java programming!

Programming languages enable humans to write instructions that a computer can perform. With precise instructions, computers coordinate applications and systems that run the modern world.

Sun Microsystems released the Java programming language in 1995. Java is known for being simple, portable, secure, and robust. Though it was released over twenty years ago, Java remains one of the most popular programming languages today.

One reason people love Java is the Java Virtual Machine, which ensures the same Java code can be run on different operating systems and platforms. Sun Microsystems’ slogan for Java was “write once, run everywhere”.

Programming languages are composed of syntax, the specific instructions which Java understands. We write syntax in files to create programs, which are executed by the computer to perform the desired task.

Let’s start with the universal greeting for a programming language.

Hello Java File!

Java runs on different platforms, but programmers write it the same way. Let’s explore some rules for writing Java.

In the last exercise, we saw the file Java files have a .java extension. Some programs are one file, others are hundreds of files!

Inside, we had a class:

public class HelloWorld {}

We’ll talk about classes more in the future, but for now think of them as a single concept.

The HelloWorld concept is: Hello World Printer. Other class concepts could be: Bicycle, or: Savings Account.

We marked the domain of this concept using curly braces: {}. Syntax inside the curly braces is part of the class.

Each file has one primary class named after the file. Our class name: HelloWorld and our file name: HelloWorld. Every word is capitalized.

Inside the class we had a main() method which lists our program tasks:

public static void main(String[] args) {}

Like classes, we used curly braces to mark the beginning and end of a method.

public, static, and void are syntax we’ll learn about in future lessons. String[] args is a placeholder for information we want to pass into our program. This syntax is necessary for the program to run but more advanced than we need to explore at the moment.

Our program printed “Hello World” with the line:

System.out.println("Hello World");

println is short for “print line”. We’ll use System.out.println() whenever we want a program to write a message to the screen.

public class HelloYou {

public static void main(String[] args) {

System.out.println(“Hello Hritika”);



Commenting Code

Writing code is an exciting process of instructing the computer to complete fantastic tasks.

Code is also read by people, and we want our intentions to be clear to humans just like we want our instructions to be clear to the computer.

Fortunately, we’re not limited to writing syntax that performs a task. We can also write comments, notes to human readers of our code. These comments are not executed, so there’s no need for valid syntax within a comment.

When comments are short we use the single-line syntax: //.

// calculate customer satisfaction rating

When comments are long we use the multi-line syntax: /* and */.

We chose to store information across multiple databases to
minimize the possibility of data loss. We'll need to be careful
to make sure it does not go out of sync!

Here’s how a comment would look in a complete program:

public class Timeline {

public static void main(String[] args) {

System.out.println(“Hello Java!”);

System.out.println(“You were born in 1995”);

// Sun Microsystems announced the release of Java in 1995

System.out.println(“You were created by James Gosling”);

/*James Gosling is a Canadian engineer who

created Java while working at Sun Microsystems.

His favorite number is the square root of 2!*/

System.out.println(“You are a fun language!”);



Comments are different from printing to the screen, when we use System.out.println(). These comments won’t show up in our terminal, they’re only for people who read our code in the text editor.

Semicolons and Whitespace

As we saw with comments, reading code is just as important as writing code.

We should write code that is easy for other people to read. Those people can be co-workers, friends, or even yourself!

Java does not interpret whitespace, the areas of the code without syntax, but humans use whitespace to read code without difficulty.

Functionally, these two code samples are identical:





They will print the same text to the screen, but which would you prefer to read? Imagine if it was hundreds of instructions! Whitespace would be essential.

Java does interpret semicolons. Semicolons are used to mark the end of a statement, one line of code that performs a single task.

The only statements we’ve seen so far are System.out.println("My message!");.

Let’s contrast statements with the curly brace, {}. Curly braces mark the scope of our classes and methods. There are no semicolons at the end of a curly brace.

Compilation: Catching Errors

Java is a compiled and interptreted programming language, The code we write in a .java file is transformed into byte code by a compiler before it is executed by the Java Virtual Machine on your computer.

A compiler is a program that translates human-friendly programming languages into other programming languages that computers can execute.

Previous exercises have automatically compiled and run the files for you. Off-platform development environments can also compile and run files for you, but it’s important to understand this aspect of Java development so we’ll do it ourselves.

The compiling process catches mistakes before the computer runs our code.

The Java compiler runs a series of checks while it transforms the code. Code that does not pass these checks will not be compiled.

For example, with a file called, we could compile it with the terminal command:


A successful compilation produces a .class file: Plankton.class, that we execute with the terminal command:

java Plankton

An unsuccessful compilation produces a list of errors. No .class file is made until the errors are corrected and the compile command is run again.

For Example, when we compiled this program-

public class Compiling {

public static void main(String[] args) {

System.out.println(“Java is a class-based language.”);

System.out.println(“Java classes have a ‘main’ method.”);

System.out.println(“Java statements end with a semicolon.”)

System.out.println(“Programming is… fun!”);



Compiling on the terminal will give us the errors

In the terminal, type ls and press return or enter.

ls is short for "list" and this command lists all the available files.

There is only one file:, we did not successfully compile the file because of the error.

After correcting the mistake , your java file would be compiled.

Compilation: Creating Executables

Compilation helped us catch an error. Now that we’ve corrected the file, let’s walk through a successful compilation.

As a reminder, we can compile a .java file from the terminal with the command:


If the file compiles successfully, this command produces an executable class: FileName.class. Executable means we can run this program from the terminal.

We run the executable with the command:

java Whales

Note that we leave off the .class part of the filename.

Here’s a full compilation cycle as an example:

// within the file:
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java course!");

We have one file: We compile with the command:


The terminal shows no errors, which indicates a successful compilation.

We now have two files:

  1., our original file with Java syntax.
  2. Welcome.class, our compiled file with Java bytecode, ready to be executed by the Java Virtual Machine.

We can execute the compiled class with the command:

java Welcome

The following is printed to the screen:

Welcome to Java course!

Review: Putting It All Together

In this lesson, we’ve started writing our first programs in Java.

We’ve also learned rules and guidelines for how to write Java programs:

  • Java programs have at least one class and one main() method.
  • Each class represents one real-world idea.
  • The main() method runs the tasks of the program.
  • Java comments add helpful context to human readers.
  • Java has whitespace, curly braces, and semicolons.
  • Whitespace is for humans to read code easily.
  • Curly braces mark the scope of a class and method.
  • Semicolons mark the end of a statement.
  • Java is a compiled language.
  • Compiling catches mistakes in our code.
  • Compilers transform code into an executable class.

Read Next - Learn Java Variables



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store