Overview of This Lesson

This lesson introduces you to the Java language. it might be tempting to skip this lesson if you've already learned another language or have learned a bit of Java before, but I go over some details that many other books and courses skip over. These details will make it easier to understand how we write programs and this will in turn help you solve problems more easily.

In this lesson we'll cover the following:

What is Java?

Java was originally designed as a language that would be embedded in appliances that would talk to one another. For example, you maybe have heard stories about a "smart kitchen": You take a bag of frozen peas out of the fridge and place it on the counter. The fridge notes the bag of peas missing and it updates an electronic shopping list so you'll know to buy another bag of peas. The counter feels the weight of something so it reads the bar code and recognizes a bag of frozen peas. It immediately warms up so that the peas can be defrosted. The microwave remembers that you like to nuke your peas in a bit of water so it sets the timer and waits for you to put the pot of peas inside so it can turn itself on. All of these devices mentioned - the fridge, the counter, the microwave, the electronic shopping list - need to be able to communicate with each other. In 1991 a team of developers (called the "Green Team" and led by James Gosling) at Sun developed Java as part of a special project that resulted in a device controller called the "*7" (Star Seven). The *7 needed a robust, platform-independent language but the developers were not satisfied with the performance of existing languages. Team member James Gosling developed Java to solve the problem.

In order to be able to make appliances work well, the original language was developed to be small and compact, secure and reliable, and portable. Why?

Today these features of Java make it a great tool for writing applications that must be run on different platforms or operating systems. A perfect example of this is programs that run over a network such as the Internet. The original Java web-based programs were called applets, but we don't use applets anymore because of changes to browser security.

Many internet applications are not completely run on the server: sometimes all or portions of an application are downloaded to the user's computer. If these applications were very large, it would take longer to download, and most users don't wish to wait a long time for an application to start working. Furthermore, larger applications take up more memory, which means less memory for other applications and tasks, and that slows down applications. The fact that Java is small and compact means that it won't have excessive download times and won't take up as much memory.

You don't need me to tell you how important security and reliability is right now with internet applications: we hear stories all the time on the news and in technical publications about big hacks and security breaches. A developer wants to be able to write code that makes their application trustworthy to the users. Java has several components that make it possible to create secure and reliable code.

A developer of an application needs to be assured that their application can run on any kind of machine. Many different kinds of machines and devices access the internet and use/download internet applications, so developers need to know that their application can run on any one of these different devices without any problems. Java makes this easier because it's portable and platform independent: it can run on any device that has a Java Virtual Machine (which, as you'll see later, is pretty standard these days).

Java is Object Oriented

Java is an object oriented programming language. Object Oriented Programming (OOP) is a method of programming that focuses on defining classes that model or represent objects in a system. A class defineds what data or properties an object has and what behaviours an object can perform. For example, in an application that kept track of drivers in a ride-sharing app, a Driver class might define that driver objects have a name, car make and model, license plate, and current status (available, completing a trip, not available, etc). A Driver class might also define that a driver object has behaviours for picking up a customer, completing a trip, dropping off a customer, changing their status, or rating a customer.

The Java language already comes with many classes that can be used as building blocks to create applications. these classes can also be used to create even more complex building blocks, which can also be used to create applications.

In this course we will focus on console applications, which means that our programs won't have a GUI (Graphical User Interface) component. Don't be too disappointed about not learning to create user interfaces right away: it's better to become comfortable and proficient with the foundations of programming before moving on to UI programming. You'll learn how to write Java GUI applications in the UI programming course.

Versions of Java: Which Should You Use?

There are a few different flavours of Java. These are called APIs, or Application Program Interfaces. Each API is meant for a specific purpose.

The Java APIs contain pre-defined classes and interfaces. These are the building blocks used to write programs in Java. The API is always growing and being improved, so you should keep up with any changes and update when it's necessary for your projects. API Documentation is the set of documentation (usually it's web-based) that describes the different classes and what they do. The Java 17 API is at https://docs.oracle.com/en/java/javase/17/docs/api/index.html and it's a good idea to learn how to use it so you can learn more about the classes you'll be introduced to.

The Java APIs:

We'll be using Java SE in this course.

The Java SE contains 2 parts:


a box containing 2 smaller boxes; 
                     big box represents Jave SE, smaller boxes represent JDK and JRE
Java SE consists of the JDK and the JRE

The Java Virtual Machine (JVM) mentioned above is what makes Java portable or platform independent: if a computer has a Java Virtual Machine, it can run Java programs. Most operating systems these days come with a JVM installed. Even most modern browsers come with a JVM embedded within them. Even if you come across a computer that doesn't have a JVM, you can easily download one for free from the Java web site.

How we Write Java Programs

So how does the process of writing programs work for Java? Jav is platform independent. This means it can run on any platform, regardless of where it was compiled! How does this work?

Java source code is written and compiled just like a regular language, except that it isn't compiled into machine code: it's compiled into bytecode. Bytecode is "not quite machine code" - it can be run on any machine, as long as that machine has an interpreter. An interpreter takes a line of bytecode, interprets it into machine code, and then executes that machine code. Then it reads and interprets the next line of bytecode and executes it, then the next, etc until it either runs out of bytecode (your program has finished) or a run-time error occurs (you'll learn more about runtime errors later in the course). A run-time error causes the program to stop working, or "crash".

How does this work on different platforms? An interpreter is part of a computer's Java Virtual Machine or JVM. A JVM is needed on any machine that wants to run Java programs. So if you write and compile a Java program on a Linux machine, you can then run that program on a PC as long as that PC has a JVM. The JVM's interpreter can read the bytecode and interpret it into the PC's native machine language.


a flow chart showing Java Source 
                     Code .JAVA compiled by the Java compiler into Java Bytecode .CLASS
                     which then goes through a Java Interpreter, which shows output
                     Hello, World! on a computer screen
Java source code is compiled into Java bytecode, which is then executed using an interpreter program.

This is why Java is often called and interpreted language: because the bytecode is interpreted as it's executed rather than a fully compiled language, where all the code is compiled into one big block of machine language and then executed. Think of an interpreted language like an interpreter that is listening to someone speak and interpreting that language into another language as it's being spoken (similar to what you often see at international news conferences or when someone is interpreting sign language for someone else). A compiled language would be similar to someone listening to the whole speech and recording it, and then sitting down and interpreting the entire speech and then reciting it in a different language, and giving you that audio file so you can listen to the interpreted version.

When you write Java source code, this code is saved in a file with the .java file extension. That is then compiled by the compiler program javac.exe. This javac.exe file is included in the Java installation.

We write or code Java source code in an editor or an IDE (Integrated Development Environment). In these tutorials, we will use Notepad++ or IntelliJ IDEA, but if you prefer something else, that's fine.

A successful compilation creates a bytecode file with the same name as your source code file, except it has the .class extension instead of .java. You then execute the bytecode file using the java.exe interpreter program. The java.exe program is also included when you install Java on your machine.

Once you are able to successfully compile your code, a Bytecode file is created. This is the file that can now be run or executed by the interpreter. An interpreter reads in a line of bytecode and then executes those instructions. Then it reads the next line and executes those instructions. It continues until it runs out of bytecode (in which case your program is finished) or if it encounters a run-time error or exception (which basically means your program has crashed due to some unexpected circumstance).

With all of this in mind, we can summarize the creation of a java application into these steps:

  1. Type the source code in an editor (such as Notepad++ or IntelliJ).
  2. Compile the source code into a class file using the Java compiler.
  3. If you are notified of any syntax errors, fix them and repeat step 2.
  4. Run the Java class file using the Java interpreter.