Welcome to the first chapter of this introductory programming series. In this chapter, you will learn the basics of how programming languages make computers work and the advantages of using Java. You will learn a simple definition of object-oriented design and explore its advantages. Hopefully, you’ll pick up some useful study tips, too. If you already have some experience, you may want to skip ahead to later chapters.
1.1: About Java
Java is a very popular high-level, object-oriented, cross-platform programming language running on billions of devices worldwide. We will break down what all this means soon. Sun Microsystems originally developed the language specifications in the early 1990s under the project name Oak. In 1995, the name was changed from Oak to Java and version 1 was released to the public. Oracle Corp acquired Java from Sun in 2010 and has maintained it ever since. As of this writing, Java is up to version 21. In this series, I demonstrate using Java 21 (LTS), though most of the code we’ll talk about would still run fine in older or newer versions, as the basics remain largely the same.
Although Java is free to use, Oracle makes money by providing services and support to developers and companies. There are two versions of Java – an open-source version and Oracle’s licensed closed-sourced version, which offers a few more advanced enterprise features and sometimes, better performance. We’ll be using the open version (Open JDK) in this series.
Prerequisites
These guides are as beginner-friendly as possible and roughly cover all the topics taught in an introductory computer science course (assuming that the course uses Java – not all intro courses do). To successfully complete this series, you must…
- Be comfortable using computers
- Understand basic math and algebra
- Have a computer you’re able to install and run programs on
- Any basic computer with Windows, MacOS, or Linux built within the last five years should be fine. Even older computers may work.
Getting The Most From This Material
To gain a proper understanding of Java programming, you should carefully follow along with all the exercises in this series. The only way to learn to program is to do it yourself! Reading this material and copy/pasting likely won’t be allow you to gain a complete understanding. You should reference the code blocks in these guides and then write and run your own code. Change your code and see what happens. Make it your own. Run the code on your computer and make sure it works!
I’d encourage you to avoid using generative AI tools like ChatGPT, Bard, Copilot, etc. during the first year or two of your studies. These tools are incredibly useful, but using them with little knowledge could lead to poorly written code, security vulnerabilities, performance issues, and unexpected bugs. Therefore, a fundamental knowledge of programming is extremely useful before you rely more on these tools. If you still want to use them, I would avoid using them for code generation initially. Instead, use them to help you debug code and fix errors you’re really struggling with (after you’ve tried to figure it out yourself).
Without further ado, let’s get started!
1.2: Computers Components
Before we discuss how Java works, it’s important to have a basic understanding of how computers work. Make sure you’re familiar with the following terms, as we will talk about them in the upcoming chapters.
The Central Processing Unit (CPU) is an essential component of a computer. It basically is the brain of the computer. It does all the processing. CPUs perform computations based on mathematical instructions given as machine code.
Main memory is a component responsible for storing data temporarily. It allows data to be accessed by the CPU very quickly. All the programs running on your computer are loaded into the memory before being executed. It’s commonly referred to as RAM, which stands for Random Access Memory. Modern computers usually have anywhere from 4GB to 32GB of RAM.
Input devices are pieces of hardware which allow users to interact with their computers. Examples include: computer mouse, touch screen, keyboard, microphone, game controller, etc.
The Operating System (OS) is the most important piece of software running on a computer. It manages hardware and provides essential services to software running on the computer. Examples include Linux, Unix, Windows, Mac, iOS, and Android.
Also known as Auxiliary Memory, secondary memory stores data not being actively processed for later use. This might be a hard drive, solid-state drive, flash drive, SD card, etc. Data stored in secondary memory is called persistent data because it persists after the computer is turned off (RAM usually needs to be powered). Files not actively in use are stored in secondary memory.
Output devices are pieces of hardware that allow the user to see the results of their input. Things such as monitors, printers, or speakers are all output devices.
The graphics processing unit, or GPU, is the component responsible for accelerated rendering computations. GPUs are different from CPUs in that GPUs usually have a large multitude of processing cores responsible for processing data in parallel (all at once), whereas a CPU may have anywhere from one to eight more powerful cores. This allows GPUs to perform the complex task of calculating the math needed to render 3D environments in video games and such. Well-known examples include NVidia GTX/RTX series GPUs and AMD Radeon series GPUs.
The motherboard (also known as the main board) is an essential component responsible for facilitating connections between all the various pieces that make up a PC. Motherboard circuits are made by printing thin layers of conductive metal onto a non-conductive material. They allow the CPU to communicate with the RAM, GPU, IO (input/output) devices, etc.
A System on a Chip, or SOC, is a single chip containing all the elements of a fully functional computer. They’re one entire system integrated into a single chip. They can include the CPU, GPU, main memory, secondary memory, and input/output ports, all in one unit. Compared to a traditional desktop PC with its large motherboard, SOCs are much smaller. This allows them to fit in things like smartphones and tablets.
1.3: Understanding Java
Java is High Level
A programming language is a formal language used to give instructions to a computer. Basically, it’s the code that makes up an app that runs on your computer or smartphone. There are many types of programming languages with distinct advantages. In this series, we’re focusing on Java.
By high-level, we mean the language is written using natural language elements humans can understand. This is as opposed to writing in low-level programming languages – a naturally unreadable language like machine code, assembly languages, or binary.
You may have heard of binary – the base-2 numbering system made up of only 1s and 0s used to give instructions to a computer’s central processing units (CPU). Computer processors can only understand binary machine code. Thankfully, we have software that converts human-readable language like Java into machine code for us. Therefore, we do not have to worry about writing our programs in 1s and 0s.
Take a look at the sample code below. In this hypothetical scenario, a user is pressing the right trigger button on their game controller in a video game. The program detects this, checks if they have enough ammo, and fires the weapon.
Instead of having to write code that looks something like this (Binary/Machine Code)
01101101
01101011
11011011
10110111
10111101
10111101
10111111
We can write code that looks more like this:
//Shoot Gun
if user presses rightTrigger
and userAmmo is greater than 0
fire userWeapon
subtract 1 from userAmmo
Code language: JavaScript (javascript)
The code required to shoot projectiles in a video game would be more advanced than that. But you understand the difference–instead of writing in 1s and 0s, we’re writing in human-readable language. This is the difference between high and low-level programming languages.
Note that the above code for shooting the gun is pseudocode – sample code written in plain English to convey a point or explain what the actual code is doing. Programmers may use pseudocode as a sort of rough draft to get their ideas written down before continuing on to transform the pseudocode into proper code. This is not Java. We will look at our first Java sample in the next chapter.
Java is Object-Oriented
Java is object-oriented, which means it’s a language written in terms of abstract objects that perform certain functions. In other words, Java programs are made up of objects that do things. There are many preexisting objects in the Java Application Programming Interface (API) that we can import into our projects and use. The Java API is an extensive library of objects provided by Java that we can use to accomplish tasks in our programs.
There are various objects in the API. Examples include objects that help us create graphical user interfaces (GUIs), work with computer hardware, load and save files, manipulate databases, connect our programs to the internet, and much more. Being able to import these objects into our projects makes it easier for us to write programs with less code.
Imagine you want to create a program that prints out a text file to the user’s printer.
If you wanted to manually program every single aspect of this from scratch, you would have to write an enormous amount of code. You would have to code a file reader to open the text file from the user’s hard drive, use an algorithm to decode the file so you can figure out what the text is, change the data into a form the user’s printer can understand, and send it to the print spool. This could easily amount to thousands of lines of code just to achieve a relatively simple task.
Thankfully, we don’t have to do this. The Java API already has objects designed to handle reading files and sending documents to printers. We just need to import them from the API and tell them what to do. All the low-level things are taken care of for us. Using tools from the API, we could instruct our program to open a text file and send it to the printer in only a few dozen lines of code.
Java also gives us the ability to create our own custom objects if they don’t already exist in the API. For example, if we were creating software to keep track of customers at a bank, we might create a “Customer” object and assign it certain properties like Name, Account Balance, Phone Number, etc. We could then create custom subroutines or methods for the Customer object that handle things like account inquiries, withdraws, deposits, etc. This allows us to think of things in more concrete terms.
If you don’t fully understand what object-oriented means yet, that’s okay. We will discuss it in much more detail later in this series. Just remember that Java programs are built with objects that do things.
Java is Cross-Platform
Finally, Java is cross-platform, meaning it works across many platforms or operating systems. A key slogan when Java was first introduced by Sun Microsystems was “Write Once, Run Anywhere.” So you could write a program in Java on a Windows PC and it should execute just fine on a Mac or Linux device. The bulk of Android applications are also written using Java.
We’ll get into more details about how this works soon.
1.4: Integrated Development Environments
Most developers will write their code with the help of an Integrated Development Environment (IDE). An IDE is a piece of software designed to simplify programming with features such as intelligent code editors, debugging tools, project management tools, documentation tools, and more. If you wanted, you could write all your Java code in a basic notepad application – but I personally wouldn’t recommend it, as we have tools to make life easier.
You may use any Java IDE you like, but for this series, I’m going to be using IntelliJ IDEA Community Edition. This is one of the most popular professional Java IDEs on the market and the community edition is available for free on Windows, Mac, and Linux.
IntelliJ is also the basis of Android’s Developer Tool (Android Studio). So learning it will be useful if you might need to develop Android apps down the road.
You may download IntelliJ’s IDE using the button below.
Though technically not a full IDE, Visual Studio Code is one of the best code editors out there. It is highly popular due to the enormous repository of third party extensions available. With extensions, VS Code can basically function as a full IDE.
Other good IDE options for Java include Eclipse and NetBeans. However, I think these are falling in popularity. I’d recommend Intellij IDEA or Visual Studio Code as these are the most common tools you’ll see used.
1.5: How Java Works
How do we go from Java code in an IDE to a fully working program that executes on your computer? There are a few steps and key components you should know. Let’s take a look at this process.
First, the programmer writes their code and saves it as a Java file. By itself, this Java file is the same as any other text file on your computer. It’s not a program yet. Computers can only understand things in terms of machine code (binary ones and zeroes). We must use the tools provided to us in the Java Development Kit (JDK) in order to turn our code into working software.
The Java Development Kit (JDK) is a suite of tools provided to software developers so they can turn their source code into an actual running program. The JDK contains important things like the Java compiler, which is a program that turns high-level languages into low-level code that computers can understand. The JDK also includes a copy of Java’s API so you can import the useful objects into your projects, and a debugger to help identify the source of errors with your programs.
In some languages, the compiler turns source code directly into machine code. This isn’t the case with Java. After we’re done writing our program and want to execute it, the Java compiler from the JDK converts our Java source code into what is known as bytecode. Bytecode is a lower level of code that’s not as easily human-readable designed to be executed by a Java Virtual Machine. It’s middle of the road code in between high level Java and low level machine code.
In order to run Java programs, users need to have a Java Runtime Environment (JRE) installed on their computers. The Java Runtime Environment is a package for end-users containing everything they need to execute Java programs on their systems. Essentially, the JRE provides the software that safely turns bytecode into machine code. Since machine code is platform-specific, there are different JREs for each operating system. This way, you can write code on a Windows computer, compile it into bytecode, and then the JREs on each respective different operating system can execute the program from the provided bytecode. A copy of the JRE is included with the JDK so you shouldn’t need to install it yourself. We will set up the JDK in the next chapter.
As long as a JRE exists for the platform, Java will work on it. If a new platform comes out, a new JRE can be developed to run on that platform. “Write Once, Run Anywhere”
The Java Virtual Machine is the specific piece of software within the JRE responsible for executing our programs by converting bytecode into machine code. Just like its name, you can picture the JVM as being a virtual machine running within a real machine. It’s just that instead of reading machine code, the JVM reads bytecode. It first verifies the bytecode using a bytecode verifier, which is a piece of software that double-checks the bytecode to make sure it can be executed without breaking anything important. Java programs are contained in a way such that if the program crashes, it shouldn’t crash the entire computer or damage other systems.
After the JVM has verified the bytecode, it turns it into the platform-specific machine code for the CPU to execute.
So in summation, we start with a Java file. Use the Java compiler from the JDK to turn it into bytecode. Then the JRE and its JVM work to turn bytecode into an actual program that executes on your respective operating system.
The Just in Time Compiler
Originally, the JVM used to just interpret code. It would look at a piece of bytecode and match it with the appropriate machine code. Unfortunately, this could cause performance issues — as only one bytecode was being executed at a time. Modern versions of Java contain a just-in-time (JIT) compiler which takes the most frequently used parts of programs and compiles them directly into reusable machine code, thus allowing the JVM to simply execute that machine code the next time it needs to, rather than re-compiling/reinterpreting the same bytecode repeatedly.
1.6 Advantages of Java
Besides being object-oriented and cross-platform, there are many other advantages of using and learning Java. Here are a few of them:
- Community Size – As one of the most popular programming languages on Earth, the community is vast. If you run into a problem or need help with something, chances are there’s someone out there who already ran into that exact problem, or a similar one, and has posted a solution. You can find tons of support and guides online in places like StackOverflow, Reddit, or random websites like Kevin’s Guides.
- Academic – Many universities teach their introductory programming courses in Java. Learning Java may give you a leg up on your first computer science courses.
- Android Development – A lot of Android development is done using Java. Android is the most widely used operating system on Earth, ahead of even Microsoft Windows. If you want to be an app developer with a wide reach – Java is a great tool to know.
- Job Potential – Even with other languages (namely JavaScript and Python) gaining popularity in recent years, the demand for Java developers is still increasing and certainly isn’t going away any time soon.
- Similar To Other Modern Languages – Although each programming language follows different rules and conventions, the main logic behind programming remains the same. Once you learn the basic concepts in Java, it’ll be easier for you to learn other languages down the line.
- Enterprise – A lot of specialized enterprise apps, internal tools, etc. are built on Java.
Conclusion
This concludes Chapter 1—What is Java? You should now know the basics of what Java is and how it works. In the next section, we will look at IntelliJ and start writing our first program.
Assignment
Your assignment for this chapter is to download and install the IntelliJ IDEA Community from their official website. If you are an enrolled student in school you can apply to use the Ultimate edition for free – more info can be found on their academic license page. For this series, it won’t really matter which version you use.