Notions About the Java Language

De la WikiLabs
Jump to navigationJump to search

Java is a programming language developed by the former company Sun Microsystems (actual Oracle), launched in 1995. Based on this language and on the idea of a virtual machine, countless technologies emerged, for web and distributed applications ([1]).

Compilation vs. Interpretation

Before presenting the virtual machine mechanism, we must make a brief description of the methods of program implementation for different programming languages.

A computer processor (or computing machines in general) can execute a fixed number of instructions known to the processor, called instruction set. A program can be written directly using these instructions (their mnemonic), i.e. in assembly language, or in a high level language. There are advantages and disadvantages to both.

A program written in assembly language is not portable, so it can be executed only on the machine for which it was written, because it depends on the instruction set that the processor is able to execute, on the operating system running on the machine, on the peripherals , etc. Another disadvantage is that for a relatively simple functionality of the program, a much larger quantity of code must be written as opposed to a high-level language (such as C or Java), which makes code maintenance, as well as development opportunities to be limited. On the other hand, an experienced programmer can make the best optimizations in assembly language, since he has control over all resources.

However, for complex applications, you need a compromise between performance and size / complexity of the code. Thus arose formal, high level languages, which are more intuitive, easier to learn and understand, and abstracts away many of the low-level layers of a computing machine (instruction set , memory map, the stack structure execution, memory allocation, and so on). The advantages are enormous, beginning with the ease of syntax and semantics of a program and ending with the fact that we now have a certain degree of portability. This portability exists because the language itself is unique and an algorithm is described in the same way for any machine, but this issue arises: since a processor only knows how to execute its set of instructions, how to make the transition from a program written in a generic high level language to the assembly language? There are two solutions: interpretation and compilation.

Interpretation

Diagram of the interpretation system

Interpretation of a program is done by another program, called an interpreter. It parses the source code of the program you wish to run and it transforms it in machine code while executing it. The advantage of this approach is that there is the possibility of dynamic code generation, at runtime. An example of an interpreted language is Javascript. This accepts constructs like:

eval("x=10;y=20;document.write(x*y)");
document.write("<br />" + eval("2+2"));
document.write("<br />" + eval("x+17"));

It can be seen that the function eval takes a string of characters as an argument, which is dynamically built, then evaluated as a language expression. This is not possible with compiled programs. The major disadvantage of interpreted programs is the low execution speed (the translation from high level language to the machine language is done at the runime).

Compilation

Diagram of the compile system

The compiler is a program that has the role of translating a programming language in another programming language. Basically, this is a translation program, but not between two tongues, but two formal languages. The difference between a language and a formal language is that the latter has a set of strict rules, making rigorous translation possible for an algorithm. Unlike the interpreter, the compiler does the translation statically, at the compile-time, between the high-level language and the assembly language of a specific processor. Once the program is translated to assembly language, then into machine code with the help of an assembler and a linker, it is ran directly on the processor, without any other additional programs.

Mașina Virtuală Java (JVM)

Schema bloc a mecanismului de execuție a mașinii virtuale

Programele Java sunt compilate și apoi interpretate. Motivul pentru acest sistem este introducerea în flow-ul de execuție a unui strat suplimentar, numit Mașina Virtuală Java. După cum îi spune și numele, JVM este un procesor, dar nu unul real, fizic, realizat pe siliciu, ci un procesor virtual, simulat de procesorul gazdă. În esență, mașina virtuală este un alt program. Există două mari avantaje pentru utilizarea acestui concept:

  • portabilitate - mașina virtuală Java are o specificație clară, și fiecare implementare de mașină virtuală este identică, indiferent pe ce procesor rulează; asta implică faptul că odată programul compilat pentru JVM, acesta va rula pe orice implementare, adică pe orice procesor gazdă și pe orice sistem de operare;
  • securitate - layer-ul mașinii virtuale se comportă ca un sandbox, astfel încât execuția programului se reflectă în cea mai mare măsură în interiorul mașinii virtuale, și nu în mașina reală, oferind un grad ridicat de protecție.

În prima fază, programul inițial, care este stocat într-un fișier cu extensia .java, este compilat, folosind compilatorul Java (javac) și este generat fișierul executabil pentru JVM, care are extensia .class. Acest fișier este încărcat de către mașina virtuală (java) și este interpretat.

Particularitățile limbajului Java

Cu toate că sintaxa limbajului Java provine în cea mai mare măsură din C, există unele particularități, în special legate de modul de acces la memorie, care este fundamental diferit:

  • în Java nu există pointeri, doar referințe; implicit, nu există aritmetica pointerilor;
  • runtime checking - un sistem care verifică accesul la locațiile de memorie într-un vector și aruncă excepții atunci când se încearcă accesul în zone nepermise;
  • garbage collection - programatorul nu trebuie să țină seama de memoria alocată, o zonă de memorie este eliberată în mod automat atunci când nu mai există referințe active către ea;
  • distributed computing - Java pune la dispoziție în bibliotecile implicite clase care facilitează conectivitatea (java.net) și execuția distribuită (java.rmi);
  • multithreading - Java pune la dispoziție în bibliotecile implicite clase care facilitează execuția concurentă a mai multor metode și sincronizarea acestora.

În plus față de aplicațiile de sine stătătoare (standalone), în Java există suport pentru un tip de aplicații numite applet-uri, care se execută în interiorul unui browser de web. Acest lucru facilitează dezvoltarea aplicațiilor interactive pe web.

Totuși, poate cel mai important avantaj al limbajului Java, este setul de clase puse la dispoziție de Oracle, numit generic Application Programming Interface, care conține o vastă colecție de funcții deja implementate, gata de a fi folosite pentru o gamă largă de aplicații.