MOBI BOOT CAMP CORP. logoLearning Buddy
  • SIGN IN
  • Introduction
  • Unit 0: The First Program
    • Getting Started
    • The main method
    • How Programs Work
    • More on methods
    • Recap and Key Terms
    • Newbie pitfalls
    • Eclipse
    • Unit 0 Slides
  • Unit 1: Using Objects and Methods
  • Unit 2: Selection and Iteration
  • Unit 3: Class Creation
  • Unit 4: Data Collections

Getting Started

In the video lesson, you created a Java class called MyLittleProgram and a method with the name main.

In this lesson, you will understand these terms in detail.

What is a class in Java?

A class is the fundamental building block in the Java programming language. Your program, which consists of Java code, starts by declaring a class and then adding other program constructs inside it.

A simple, valid class named Tutorial would be:

The Class Keyword
  • A class is a template for objects. Every Java program must have at least one class definition. It starts with the class keyword followed by its name.

Task: Defining a simple Java class.

class Tutorial {

}

A class, in its simplest form, is defined with the class keyword and the name of the class. In this case, we called our class Tutorial, but you can call it whatever you want, as long as you follow identifier naming rules. Every class starts with a left curly brace { and ends with a right curly brace }.

In Java, the position of the opening curly brace doesn't strictly matter, as long as it follows a space after the class name. While many developers put it on the same line, you will usually see it on a new line in AP Computer Science A exam materials, like this:

Task: Defining a class with braces on separate lines.

class Tutorial 
{

}

Throughout this ebook, you will see both styles used interchangeably.

Even though the Tutorial class is empty and there are no other program constructs inside it, it is still a valid class, as you can compile this class without errors. You can create this class using any text editor. When you save the file on the filesystem, the file name should exactly match the name of the class. This is especially true for classes with the public modifier. You will learn more about modifiers in the next module. Although we did not add the public keyword modifier for this class, it is still better to save the file as Tutorial.java to maintain consistent coding practices.

To run a Java program, you have to follow two steps:

  • Compile your Java program using the javac executable.
  • Run your compiled program using the java executable.

Let us understand both steps in detail.

Compiling a Java Program

To compile this Java program file, you would execute the below command from the Terminal:

javac Tutorial.java

javac is an executable which is part of the Java Development Kit (JDK).

When you execute the command above, a compiled file with the .class extension is generated.

Note: Your filename should end with the .java extension. If you omit the .java extension, the javac executable will not be able to compile your file, and you will see an error message:

error: Class names, 'Tutorial', are only accepted if annotation processing is explicitly requested.

If you see this message, save the file with the .java extension and rerun the command.

Running a Java Program

You run the compiled version of your program using the java executable, which is part of the Java Runtime Environment (JRE). The JRE is bundled with the JDK, so you do not need to download it separately if you have the JDK.

To run the compiled class, execute:

java Tutorial

When the command above is invoked, a Java Virtual Machine (JVM) is spawned to run your program.

Note: Notice that when you run the java executable, you do not use the .class extension. However, the java executable automatically picks up the file with the .class extension to execute the program.

But the above program will throw an exception when you try to run it, as there is no main method in the class. You will learn more about the main method below.

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 the javac executable and debuggers necessary for developing applications.

Development vs Runtime
  • JDK (Java Development Kit): Used by developers to write and compile programs.
  • JRE (Java Runtime Environment): Used by users to run compiled Java programs.
  • JVM (Java Virtual Machine): The "engine" within the JRE that actually executes the code.

Using the JDK, you can convert a .java file into a .class file. Since the JDK is already installed on Google Cloud Shell, we did not have to install it in the video tutorial. The JDK is also bundled with Android Studio. However, if you are using neither of these environments, you will have to download the JDK and install it before you can compile and run your Java programs.

Using Java on Cloud Shell
  • Open Google Cloud Shell using the link: http://console.cloud.google.com/
  • You need a Gmail account to get access to your own Google Cloud Shell.
  • Find the Java version installed on your computer by using java -version.

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 the javac compiler for creating a .class file. However, the java executable is part of the JRE, so using the JRE, you can run a compiled Java program. The 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, the JRE is also bundled with it, so you do not have to download it separately.

What is JVM?

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

Diagram showing the relationship between JDK, JRE, and JVM, where JDK contains JRE, and JRE contains JVM.

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, and macOS.
  • Once a Java program is compiled on one OS, it can run on any other OS that has the JRE installed. This is why Java had a debut with the slogan "Write once, run anywhere!"

Most IDEs automatically compile your Java program as soon as you save it, making manual compilation via javac unnecessary

What is an IDE?

IDE stands for Integrated Development Environment. An IDE is a complex software program consisting of numerous tools necessary to make software development easy for the developer. With an IDE, you never really compile and run using the javac or java executables. javac is automatically invoked as soon as your file is saved in the IDE, and java is invoked when you select a button, typically called run, in the IDE interface. Using an IDE improves productivity as it highlights errors in code even before it is saved, provides easy searching for methods and classes, and offers many other features. Popular IDEs for writing Java include Eclipse, IntelliJ IDEA, and NetBeans.

However, to understand the basic building blocks of the Java programming language, we will use Cloud Shell, which provides a simple platform for learning Java.

What is an Identifier?

The name of the class is an identifier in Java. Identifiers are also used to name methods, variables, and other constructs. All identifiers are constructed according to the following rules:

Rules for Identifiers
  • Cannot start with a number
  • Can have a mix of letters, numbers, and the special characters $ and _
  • Cannot be a reserved keyword
  • Cannot contain spaces - e.g., my class is illegal
  • Identifiers are case-sensitive. They must be written the same way every time you reference them in your program (e.g., MyClass is not the same as myClass).

Reserved keywords

abstract continue for new switch
assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while

While it is difficult to use a reserved keyword by mistake when using an IDE (as most IDEs highlight them and warn you immediately), it is still important to be aware of them. An IDE makes writing Java programs faster and less error-prone compared to using simple text editors.

Recommendation for class names
  • The computer does not care—any name for the class that satisfies the identifier rules is fine. However, you write programs not only for the computer but also for other humans to read and understand! Therefore, it is recommended to give meaningful names to classes and follow standard Java naming conventions: https://www.oracle.com/technetwork/java/codeconventions-135099.html
  • A class name should start with an uppercase letter and should be a noun.
  • If you use multiple words for your class name, use "UpperCamelCase" (also known as PascalCase), where the first letter of every word is capitalized. E.g., MyLittleProgram.
  • Even though you can use $ and _ in a class name, it is better to avoid them. Some automated tools generate class names using the $ symbol, but human programmers typically avoid them.

If you remember from the last lesson, you also constructed a main method and added a System.out.println statement inside it.

Basic Linux Commands That Every Developer Should Know

You used the terminal (a.k.a. console or command prompt) to compile and run your Java program. Working from the terminal program is indeed a fundamental skill required in the software industry today. Did you know that you can copy, move, or delete a file from the terminal instead of using the visual interface provided by Google Cloud Shell? Although the visual interface is more intuitive, it is still essential to learn some of the basic Linux commands using the terminal program if you intend to pursue a career in the software industry. Here is a video on some very fundamental Linux commands that everyone in the software industry should know.

Privacy Policy | Terms & Conditions