How Programs Work?

Desktop/Laptop systems

Now that you have written your first simple program, are you wondering how programs really work on your Desktop or a Laptop computer? It is indeed important to understand the different parts of your computer and the role that each part plays when your program is executed.

Although a computer contains many parts, the three important building blocks of a computer are:

image alt text

  • Central Processing Unit (CPU)-- This is the brain of every computer. A computer should have one or more CPU's to make it a Computer! As the name suggests, this is the unit which helps in computational tasks. Programs containing statements like 2+5, 2*4 will take the help of CPU to calculate the result.
  • Random Access Memory (RAM)-- This is the temporary memory of your computer. Anything that is saved on this memory will be erased when the computer is shutdown. RAM is the play arena for your program! Your class, method, variables all are loaded into RAM when a program is started. When the program completes its run and exits, all the variables and any other associated objects in RAM used during the program execution will be marked for deletion. When the computer shuts down, the RAM is wiped clean and when you restart your computer you start with a clean slate on the RAM.
  • Hard Disk (HD)-- This is the permanent memory of the computer. Anything that is saved on the Hard Disk will remain even after the computer is restarted. Your .java file and .class files are stored on the Hard Disk. JDK and JRE programs are saved on HD. When the JRE runs your program however, it spawns a Java Virtual Machine (JVM) and executes your program inside the JVM. Every stand alone program gets its own JVM.

All of these resources of the computer are used not only by your program but also by the Operating System (OS) itself and all the other program that you may be running on your machine like a Browser, Word Program etc.. The OS will decide based on its scheduling algorithm, which program gets to use the CPU at any point of time. The memory is shared by many programs at a time. Your program will get memory allocated as required by the OS. When your program has finished its execution, memory used by your program is released to be used by other programs.

image alt text

CPU, RAM and HD belong to the hardware layer of your computer. You can physically see the hardware components. Over this hardware layer, a special program called Operating System (OS) is saved. There are many OS's in the market today; the most notable ones are different flavors of Windows, MacOS and Linux. Linux is an open-source OS.

Open-source software is a software program with its source code made available with a license in which the copyright holder provides the rights to use, change, and distribute the software to anyone and for any purpose and it is free. Windows and Mac however, are not open-source software. You have to pay to download and use them and also you do not get to see the source code of the OS, let alone make changes to it.

JDK and JRE are installed on top of the OS. Every OS has its own version of JRE and JDK. JRE spawns a special program called Java Virtual Machine (JVM) on which your program runs. The four layers are shown in the figure above. When your program tries to access any of the hardware components it has to cut through all the layers and finally the OS will directly access the hardware and do the necessary tasks on the hardware as required by your program.

Mobile systems

Mobile devices encompasses all types and sizes of phones, pads, wearables etc.. There are many types of mobile OS's today. Since we are interested in Android systems, you will learn some basic facts of an Android OS.

Android devices run on a mini version of Linux OS on which a special type of JRE called ART (Android Runtime) is installed. Any mobile device does not have enough capacity; be it CPU, RAM or HD, to be able to download JDK/JRE and run your regular Java Programs. However you can run a compressed Java program using ART. In the earlier versions of Android devices, Dalvik Virtual Machine (DVM) used to run Java programs, however currently ART has replaced DVM. But there is no equivalent for the JDK and hence there is no way you can write a Java program and compile it on a mobile device.

To run a Java program on an Android device, you have to compile it to .class first on a Desktop or Laptop computer and then convert all the .class files to a compressed single .dex file which is bundled up with all the other resource files into a single .apk file. This single .apk file - which is the 'app' is copied over to an Android device for it to run on ART. All these tasks are seamlessly done by Android Studio IDE with the click of a button.

In the event that your app code references more than 64K methods, then a single .dex file will not work, in which case you have to add multidex support. Refer: https://developer.android.com/studio/build/multidex

results matching ""

    No results matching ""