Diferență între revizuiri ale paginii „Notions About the Java Language”
Linia 31: | Linia 31: | ||
The [http://en.wikipedia.org/wiki/Compiler 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. | The [http://en.wikipedia.org/wiki/Compiler 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. | ||
− | == | + | == The Java Virtual Machine (JVM) == |
− | [[Fișier: | + | [[Fișier:jvm_en.png|Diagram of the JVM execution environment]] |
− | + | Java programs are compiled then interpreted. The reason for this system is the insertion, in the execution flow, of an extra layer, called the [http://en.wikipedia.org/wiki/Java_virtual_machine Java Virtual Machine]. As the name says, JVM is a processor, but not a real, physical, silicon one, but a virtual processor, simulated by the host CPU. So basically, the virtual machine is another program. There are two main advantages of this concept: | |
− | * '' | + | * ''portability'' - the JVM has a clear specification and each of its implementations is identical, regardless on what hardware it runs on; this entails that once the program is compiled for JVM, it will run on any implementation, on any host CPU and any operating system; |
− | * '' | + | * ''security'' - the virtual machine layer behaves like a [http://en.wikipedia.org/wiki/Sandbox_(computer_security) sandbox], so that the program's execution is only relevant inside the virtual machine, not in the real machine, offering a high degree of protection. |
− | + | In the first phase, the initial program, stored in a file with .java extension is compiled, using the Java compiler (''javac''), and a new, executable file is generated for JVM, having the .class extension. This file is loaded by the virtual machine (''java'') and is interpreted. | |
== Particularitățile limbajului Java == | == Particularitățile limbajului Java == |
Versiunea de la data 21 octombrie 2013 19:34
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
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
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.
The Java Virtual Machine (JVM)
Java programs are compiled then interpreted. The reason for this system is the insertion, in the execution flow, of an extra layer, called the Java Virtual Machine. As the name says, JVM is a processor, but not a real, physical, silicon one, but a virtual processor, simulated by the host CPU. So basically, the virtual machine is another program. There are two main advantages of this concept:
- portability - the JVM has a clear specification and each of its implementations is identical, regardless on what hardware it runs on; this entails that once the program is compiled for JVM, it will run on any implementation, on any host CPU and any operating system;
- security - the virtual machine layer behaves like a sandbox, so that the program's execution is only relevant inside the virtual machine, not in the real machine, offering a high degree of protection.
In the first phase, the initial program, stored in a file with .java extension is compiled, using the Java compiler (javac), and a new, executable file is generated for JVM, having the .class extension. This file is loaded by the virtual machine (java) and is interpreted.
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.