Recap

A class, main method and a statement. Let us summarize these three elements:

A Class: Class declaration in our example has three components:

  1. Optional modifiers public, the meaning of which you will learn later.

  2. Give any name of the noun form with first letter capitalized and follow java literal naming conventions.

  3. The class body is surrounded by left and right curly braces, {}.

At the minimum a class must have a name and a set of curly braces for the JRE to compile. However to run it from the command line it has to have a main method.

A main method: Main method should have

  1. public and static keywords in any order

  2. Method return type should be void

  3. Method name should be 'main'

  4. Should receive argument of type a String array represented by String[]. You can name the parameter what ever you want although typically 'args' is used.

  5. Mandatory method body, containing optional statements enclosed between left and right curly braces which forms the main method code block.

A Statement: A Statement is a single line of executable. They end with a semicolon ‘;’ System.out.println statement will enable us to write something to the console. That is why we see ‘Hello World’ printed on the console.

Key Terms

What is a Java Program (a.k.a. application)

A typical Desktop Java program is a collection of one or more classes with typically one of them containing the main method, which is the program execution starting point. Java source file can contain more than one class definition however the file name should match the public class name. Each class definition is compiled into separate .class file containing the Java byte code. The name of the .class file matches the class name defined in the .java file. All .java files must be first compiled to .class files and then you run the .class file.

What is Java Runtime Environment (JRE)

The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine, and other components to run applications written in the Java programming language. The JRE does not contain tools and utilities such as _javac_ compiler for creating a .class file. However _java_ executable is part of JRE, so using JRE you can run a compiled Java program. JRE is already installed on the Google Cloud Shell so we did not have to install it. If you use an IDE like Android Studio, then JRE is also bundled with it, so you did not have to download that separately.

What is Java Development Kit (JDK)

The JDK is a superset of the JRE, and contains everything that is in the JRE, plus more - tools such as _javac_ executable, and debuggers necessary for developing applications. Using JDK you can convert a .java file into a .class file. Since JDK is already installed on Google Cloud Shell, we did not have to install it. JDK is also bundled with Android Studio. However if you using neither of these environments, you will have to download JDK and install it before you can compile and run your Java programs.

What is JVM?

Java Virtual Machine (JVM) is a program which can run Java programs. When you run your Java program using `java` executable, the JRE spawns a JVM program which inturn runs your specific Java program. Every Java program which is run from the command line will be executed in a separate JVM program.

image alt text

Points to note

  • Every operating system has its own version of JDK and JRE. Popular operating systems (OS) on which you can install JDK and JRE are Windows, Linux, MacOS
  • Once a Java program is compiled on one OS, it can run on any other OS which has JRE installed. This is why Java had a debut with the slogan "Write once run anywhere!"

results matching ""

    No results matching ""