Tuesday 28 July 2015

An introduction to programming, for the absolute beginner

What is Java? 

In the most simple of terms, Java is a general purpose programming language. Initial version of Java was released by Sun Microsystems in 1995. The characteristics behind Java's huge popularity lay behind its simplicity, wide variety of usages and robustness. Java can be used to develop programs that can be used in different environments. You can write programs that can be used in a client-server environment. One of the most popular usages of Java in its early days was in the development of applets. An applet is a Java program that is embedded within a webpage.

Programming can be defined as a sequence of instructions that perform a specific task on a computer.  Well defined notations are required for this process and these set of notations are a called programming language. A programming language is a tool used by an engineer to solve problems. This can be from acquiring statistical data through complex algorithms or designing the logic for a circuit.
 

So how do we tell a computer what to do? As humans we mostly communicate through a spoken language be it English, French or Japanese. However this is not the only means of communicating we also use gestures without saying anything to convey a message.


                                                          https://netbeans.org/images_www/collateral/80/banner_182X150_80FCS.png   

Netbeans is a popular IDE used to write Java programs and also supports a host of other languages.
You can even try it online at http://www.tutorialspoint.com/compile_java_online.php

To have successful communication we have to both be using the same language. If I were to speak Chinese and you French we would need a translator to understand each other. Computers understand instructions in the binary format, this consists of a sequence of 1's and 0's. The following is 'hello' in binary:

      (H)              (E)               (L)                (L)                 (O)
01101000     01100101     01101100     01101100      01101111

This is what is known as machine language. Programs that are written in machine language are machine-dependent. Each computer has its own different instructions where 0011 can be instruction to add two numbers and on another computer something else. Programs written in machine code are difficult, perhaps one could say even impossible to read or write.

Machine language is used to program hardware ranging from consumer electronics like disc drivers to video game consoles like the PlayStation 1.

Below is some code that handled input on a controller for the PlayStation 1.


int move_user_char( unsigned char* pad )
{
 int n;
 int bcount;
 long range;
 long vx=0, vz=0;
 static long ofx=0, ofy=0;


 /* Turning left/right */
 n = 0;
 if(pad[2]<=0x60 ) n = -(0x60-pad[2]);
 if(pad[2]>=0xa0 ) n = -(0xa0-pad[2]);
 PVect.vy = (PVect.vy+n/2)&4095;


 /* Moving forth and back */
 n = 0;
 if(pad[3]<=0x60 ) n = (0x60-pad[3]);
 if(pad[3]>=0xa0 ) n = (0xa0-pad[3]);
 if(n!=0) {
  vz += (rcos( PVect.vy )*n/128/4);
  vx += (rsin( PVect.vy )*n/128/4);
 }


High and low-level languages? 

Machine languages and assembly languages are also known as low-level languages as these are close to the heart of machine, where we can control and adjust each instruction. So low-level details of the computer CPU, memory peripherals and which registers to use, are essential to understanding low-level languages like assembly language. With machine languages we do not have an abstraction of the lower level irrelevant details of a computer.


In comparison higher-level languages provide a means of abstraction where the programmer does not have to worry about the internal specifications of the machine in order to create programs efficiently. The need for this abstraction gave rise to programming languages like C, C++ Java, FORTRAN (formula translator, developed by IBM) and COBOL (common business-oriented language). These higher level languages make use of English words, mathematical notations and concepts the are intuitive to our understanding. The program that is written in a high level language is called the source code.

Simple instructions can be read and understood easily for example to divide two numbers we could have some as code as follows: int x = 10 / 5;

We can see that it is easy and self-explanatory but this also means that the computer does not understand these instructions without a translator. So there is a requirement for the computer to translate these high-level instructions to machine code.

Below is some simple java code that that prints a hello world message.

1
2
3
4
5
6
7
public class Main {

    public static void main(String[] args) {
        System.out.println("Hello, World");
    } 
} 
  
 
Minecraft is a popular game written in Java


Code Compilation

Compilation of code or compiling a program is overly simplified phrase that is often used by programmers and developers alike. This phrase means translating a written program in a high-level language to a lower-level programming language. The code that is complied by a compiler is called compiled code. Another method that is used is by using an interpreter. An interpreter does not translate the whole code rather it reads one instruction written in a high-level language at a time, we can sometimes describe an interpreter as a simulation. 

In a programming language such as Java compiled code is translated into byte-code. An interpreter call the Java Virtual Machine (JVM) is used for this process in the Java platform, it interprets the byte-code in a .class file, this is a binary format. The procedure of bringing byte-code for a class definition into the memory of a running JVM is called classloading. The JVM has classloaders which take the .class files and them loads them into memory. The classloader servers as an abstraction, classes are only loaded on-demand when the JVM needs the class definition.
 
Most Java Virtual Machines use just-in-time compilers (JIT), which compiles an entire Java program into machine language. Ahead-of-time (AOT)  is also sometimes used this is a compiler which is used to compile a program in a intermediate language to machine language.

Other compiled languages such as C++ or Objective-C have to be compiled to run with a defined architecture and operating system, however this is not the case with Java. With Java one is able to run on any JVM regardless of the platform.

Programming Paradigm

An important aspect of modern software development is the idea of Object Oriented Programming. OOP is a paradigm, a theory, an idea, an approach to providing a solution to real world problems. We use models supported by that language, which then enables us to create a solution that is called the program. In fact one may often here that "Java is a object-oriented programming language", but what this really means is that provides features and constructs that support that object-oriented paradigm.

"A programming paradigm is a way of conceptualizing what it means to perform computation, and how tasks are carried out on a computer should be structured and organized" - Robert W. Floyd 1978

The above quote by Floyd, captures the definition of paradigm in the programming context. There are many different paradigms including Procedural, Functional, Logic and Object-oriented.  



If you enjoyed reading this blog, please leave a like, comment or share. 

Until next time goodbye for now.

 

No comments:

Post a Comment