Before I start programming in Java, I explain to my students an introduction to programming. I tell you what programming consists of, what an algorithm is, a flow chart and a pseudo-code. But above all, I insist that programming is about solving problems.
Several concepts should be internalized before starting to program. At least we should be clear about the following:
Set of disciplines that use computers to study and solve problems.
Computer science aims to solve problems using computers to develop the solution.
Computer science is NOT the study of computers.
What is programming?
Programming is the process of creating a set of instructions to tell the computer how to perform a specific task.
What is an algorithm?
An algorithm is an unambiguous sequence of instructions that enables the computer to solve a problem.
As its name suggests, it is a diagram that explains the flow of instructions to solve the problem. For this, a series of symbols are used, with a specific meaning, and connected by arrows that indicate the flow.
It allows many people to participate in creating the algorithm that solves the problem. You don’t need to know how to program.
There are programs for the development of flowcharts, although it is not unusual to make the first sketch with paper and pencil.
Example: Fix the problem that the car won’t start.
The process to solve this problem would be:
Check that the key has been inserted in the ignition.
Make sure that contact has been made: it has been started.
Check that the car has Gasoline.
After the previous checks, if the car has started everything is fine. If, on the other hand, it has not started, we call the crane and we will take it to the workshop.
This process, drawn in a flow chart, could look like the following:
In its implementation I have used the following symbols:
|Start/end: Represents the starting point and the endpoint of the process being described|
|Process: We indicate the operation to be carried out.|
|Decision: It is evaluated if a condition is true or false, taking the process one way or the other depending on the result of said evaluation.|
|Flowline: It marks the order in which we are executing the different operations and checks our diagram. The arrow points to the next action.|
There is a whole set of symbols to define the processes in detail. The best-known notations are flow chart and BPMN. For this notebook, it is enough for us to understand and handle the four symbols shown above.
We can also express an algorithm using pseudocode, which is nothing more than expressing its instructions in a natural language, easy for a human being to understand. Sequencing the instructions as they would be carried out in the execution of the process.
Example: Fix the problem that the car won’t start.
Start Check that we have inserted the key in the ignition. If we have not entered it, enter it. Check that we have started (make contact) If we have not made contact, start. Check for gasoline. If there is no gasoline fill the tank and start the process again. If it does not start, call the tow truck and take the car to the garage. End
Every computer system understands a very low-level language that is specific to the type of hardware in it. This language is called machine code.
On the contrary, programmers, when they write software, use a high-level language much closer to human language and therefore much easier for a person to understand.
We can distinguish three levels of programming languages:
The very low-level language that is understood by the computer.
- Instructions that can be directly executed by the CPU.
- Numerical language strictly composed of zeros and ones.
- Fastest possible language of execution.
- Tedious to detect and correct errors. Very unfriendly language.
Language close to machine code, but adapted to make it easier for coding by a person.
- Very close to machine code.
- The best known is assembly language.
- It needs to be “translated” by a compiler (the assembler) into machine code for it to be understood by the computer.
Language with a set of instructions close to human language, making it much more intuitive to program in it.
- They provide a high level of abstraction from machine code.
- They are easy to learn.
- They need a translator to communicate with the computer, which only understands machine code.
- There are hundreds of such languages.
Compilation Vs Execution
Normally we will program in a high-level language, very rarely in a low-level language and never in machine code.
As the computer only understands machine code, we will need a translator for the code programmed in a high-level language. We have two possibilities to translate it into machine code:
Compiler: We directly translate the entire program into machine code and execute it.
Interpret: We are translating the instructions into machine code one by one, depending on how we have to execute them.
A compiled program is going to be faster in execution. An interpreted program will allow changes to be made during execution.
Introduction to Java
Java is a high-level language with the following characteristics:
Platform independent (JVM)
Neutral architecture (object file)
Java is compiled and interpreted at the same time. The code programmed in Java is compiled to “bytecode” by the Java compiler, running on any platform. The generated bytecode is not exactly machine code, it is rather a kind of assembler that is platform-independent. That is, it is valid for all Operating Systems.
On our computer, we will have the JVM (Java Virtual Machine) installed, which is specific for each platform since it will be in charge of translating the “bytecode” into machine code. For the latter, it uses an interpreter that works quite fast as it does not have to do a translation from a high-level language, but from bytecode.
However, modern JVMs use JIT (just in time) compilation to compile the bytecode into machine code.
In the following figure you can see what has been commented: