Object-Oriented Programming: A gentle Introduction to the Java Programming Language

Java is one of the several programming languages one can use. In this article, you will find a very short introduction to Java, which is all you need to start writing computer programs.

So, Java is a programming language, but: What is a programming language?

Informally, a programming language is a way that we (programmers, software developers, coders, etc.) tell computers what to do.

Very simple, isn’t it?

Well, there is a lot more to say about programming languages, but for now, we are going to use this informal definition.

Wait, but there are many programming languages, why? The same way we human’s beings have several languages to communicate (English, Spanish, Chinese, etc.), we can communicate to a computer what to do using different languages.

Notice that I said it is a way to tell the computer what to do. There are other ways. Every input device connected to a computer (keyboard, trackpad, mouse, etc.) gives us the ability to tell a computer what to do.

However, we are going to focus on the programming language tool, because this course is about programming.

Introduction to Java

According to the Tiobe Index for June 2021, Java is the third most popular programming language among 50 of them. Several Software Development Companies use it to develop enterprise software.

Apart from the use in the industry, it is a great language to learn programming because if we want to use it, we must learn most of the concepts about the Object-Oriented Paradigm (the goal of this short course).

Programmers must learn several programming languages as they move forward in their careers. This happens because different companies use, sometimes, different programming languages. Therefore, if you want to work in a certain company, you must learn the technology they use.

It is for this reason, that my teachings focus on the paradigm, instead of the programming language. Once you understand the paradigm, learning a programming language is easy.

Why we should focus on the paradigm?

For you to have an idea, there are software applications that can translate code written in Java to C++. However, there is no program that you can give a problem description as input and can give you the code in a certain programming language that solves that specific problem.

To translate from a programming language into another one, we just have to follow the rules of the two languages, that’s it.

It is like that because programming languages are formal languages, everything is well-defined in formal languages. As opposite as the languages we humans use to communicate, that are called natural languages.

Saying all of that, what matter most is to know how to solve problems by using a programming language. Therefore, we focus on how to solve problems, not on the programming language itself.

What if I don’t know the programming language? That’ easy, you can find everything you need to know about a programming language in a book or on the internet.

However, you won’t find the solution to new problems (and sometimes to old ones) in a book or on the internet, you have to develop it yourself with the set of tools you have on your hands.

We are going to use a programming language in that way, as a tool that will help us to solve a problem by using a computer.

Structure of a Java program

Java program general structure
Java program general structure

As I mentioned before Java is a formal language. So, it has a set of rules that we must follow, otherwise, a computer won’t understand what we are “telling” it to do.
In the previous example, we should concentrate on the line “// write your code here “. It is in that area where we are going to tell the computer what to do. When you move forward to other lessons, you will understand why the rest is there.
In programming, we usually receive data as an input, and we should use that data to give an output to the user.
A trivial example is this one: write a computer program that prints on the screen the sum of two integer numbers.
In this problem, our input is two integer numbers, and the output will be the sum of the two numbers.
This problem is very simple but involves several concepts that are key to programming.

Variables

Variable is a key concept in programming. We use variables everywhere.
As a variable, we understand a “place” that we can store data or values. We will use these values in different ways to calculate the output that is expected.
In our example, we have to input two values, two integer numbers. Once those values are entered where they are stored? How can I access them?
The most common way to store those two values is by using variables. A variable gives you the means to store the values in the computer memory and to access them any time you need.
“The Java programming language is a statically typed language, which means that every variable and every expression has a type that is known at compile time. The Java programming language is also a strongly typed language because types limit the values that a variable can hold or that an expression can produce, limit the operations supported on those values, and determine the meaning of the operations. Strong static typing helps detect errors at compile time.” Source document here.

One of the things we must do when using a strongly typed programming language is that any time you want to use a variable, you must define it first.
In Java, we define variables by writing their type, and then the variable name.
A type defines the values that a variable can hold. For instance, in our example, we have to store two integer values. Therefore, we must use the type that can store integer values.

Common types used in Java

Find below a summary of the most common types we use in Java.

  • int: by using this type you can store integer values between -231 and 231-1
  • long: to store integer values between -263 and 263-1.
  • float: real numbers (recommended over double)
  • double: to store real numbers with a bigger number of decimal numbers than the float data type.
  • boolean: two possible values, true or false
  • char: a Unicode character. i.e. ‘a’, ‘b’, etc.

According to the types above, we have to use the type int, because we want to store integer numbers.
There is a word very important to all programming languages: syntax. The syntax defines the rule(s) to use/do something in a programming language.
To define a variable, we do it as follows:

int number1;

Notice above, we wrote the type (int), the name of the variable (number1) and ‘;’. That is the way we can define a variable.
For variable names, there are certain rules to be followed:
• It cannot start with a number or special character.
• It cannot be one of the reserved words of the language. Do not worry about learning the reserved words, we are going to use some of them as required to solve our problem.
• Variable names are case-sensitive. This means that “number” is not the same variable as “Number”.
There are certain conventions about variable names. You will find them as part of a code style guide, but it is out of the scope of this article.
After we define a variable, we can assign values to it. Find two examples below.

• number1 = 10;
• number1 = 20;

We can also define a variable and assign a value to it at the same line of code.

int number1 = 10;

Using tools that are already available to us in Java

One of the things that can help us understand Object-Oriented Programming (OOP) in a better way, is to know that we are modelling the real world. You will find many similarities in software code, especially if we write the code using the OOP paradigm.
Let’s see this example.
Let’s say you are at home, and you are going to cook meat. If you need to cut the meat, you usually don’t do it using your bare hands, do you? You use a knife.
If you need to go to a far place, you usually don’t walk, you go in a car.
The knife and car you have, are tools that are available to you, you already have them. You don’t have to create them every time you need to use them.
The same applies when we use a programming language. We don’t do everything ourselves. We use one of the principles of OOP, code reuse.

This means that there is code already there (embedded in the programming language or made by someone else) that we can use as part of our solution. We don’t have to write all the code ourselves. Look at it as a toolbox that is available to you to solve your problems.
In Java, the tools in that toolbox are usually grouped in packages. Think about packages like the drawers in the kitchen of the house you live in. You put tools that are somehow related in the same drawer.

Printing on the screen using Java

So, Java has one tool that will help us to solve the problem from our example:
• System.out.print: This will help us to show information on the screen. Such as the sum of the two integers.
In the example below, you can see the way we use this tool. Don’t worry to learn now why it is like that or how does it work. At this moment, you should just know that it works like that.

You write the name of our “tool” (System.out.print), and just after it and between parenthesis you write what you want to print on the screen. Notice that in this case, we are using a variable name. When you use a variable name, the computer will print the value that the variable is holding.

int number1 = 10;
int number2 = 15;
int result = number1 + number2;
System.out.print(result);

This is the solution to our problem. As you can see, we have two integer numbers, and we print on the screen the result of the sum of the two numbers.

How to test this code?

First, you have to install the Java platform on your computer.
You can download from the official website, and follow the instructions for your current operating system.
Notice that if you are using Linux, you can use the package manager to install Java without opening any website.
After having Java installed, it is recommended that you install an Integrated Development Environment (a.k.a IDE). I recommend you start with IntelliJ IDEA, from Google. It has a free version with more than what you need to start your learning process. You can find the installation instructions on the same download page.

Testing the solution to our first problem

When you open IntelliJ IDEA for the first time, you will see a screen like the one below.

Creating a Java project from template III in IntelliJ
Creating a Java project from template I in IntelliJ

On the screen above, choose (click) the option Create New Project. After that, you will see the window below.

Creating a Java project from template III in IntelliJ
Creating a Java project from template II in IntelliJ

In this window select Java (clicking on it, upper left corner) and click next (bottom right corner).

Creating a Java project from template III in IntelliJ
Creating a Java project from template III in IntelliJ

In the window above, click on the square next to “Create project from template”. You will see that the square is selected when the tick appears. Also, click the option Command Line App, which is the type of app we are going to use for now. After that, click next (bottom right corner) and you will see the next window.

Java Command Line App example
Java Command Line App example

This window is where you are going to do the actual coding. As you can see, there is a place that shows “write your code here” (I show you in the first section of this article the structure of a Java program). There you will write the code from our solution. It should look like the window below.

Java code example in IntelliJ
Java code example in IntelliJ

Executing the code

Now you are ready to execute (also known as running) the code. Code execution is another way to say we are “telling” the computer, execute these instructions.
To do that, you just have to click on the green triangle on the upper right side of the window.
Once the execution is done, you will see the window below.

Java code execution in IntelliJ
Java code execution

Notice that a new window appears at the bottom, showing a blue line, the number 25, and another blue line stating, “Process finished with exit code 0”. The number 25 is the result we “told” the computer to print (line 10, highlighted in yellow). The last line in blue means that the program was executed without errors.

That’s it. You created your first program and you executed it.
Congratulations!!

Now, you must keep learning so you can create solutions (using a programming language and a computer) to more complex problems.

If there is anything that is not clear to you or you cannot execute the code of the example by following the instructions in the last section, please leave a comment below. I’ll make sure I contact you to help you go through your first example.

H@ppy coding!!