Online Documentation Server
 ÏÎÈÑÊ
ods.com.ua Web
 ÊÀÒÅÃÎÐÈÈ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ÏÎÄÏÈÑÊÀ

 Î ÊÎÏÈÐÀÉÒÀÕ
Âñÿ ïðåäîñòàâëåííàÿ íà ýòîì ñåðâåðå èíôîðìàöèÿ ñîáðàíà íàìè èç ðàçíûõ èñòî÷íèêîâ. Åñëè Âàì êàæåòñÿ, ÷òî ïóáëèêàöèÿ êàêèõ-òî äîêóìåíòîâ íàðóøàåò ÷üè-ëèáî àâòîðñêèå ïðàâà, ñîîáùèòå íàì îá ýòîì.




Previous Table of Contents Next

JavaScript and Java

JavaScript Resembles Java

JavaScript supports most of Java’s expression syntax and basic control flow constructs. Take a look at the following JavaScript code segment:

for (var i = 0; i < 10; ++i) {
   /* statements come here */
}

Now take a look at the Java equivalent:

for (int i = 0; i < 10; ++i) {
   /* statements come here */
}

Notice the similarity. The only difference is the variable declaration (JavaScript is loosely typed).

JavaScript and Java are both based on objects, but their implementations differ. In both languages, many built-in functions are implemented as properties and methods of various objects.

JavaScript Differs from Java

JavaScript resembles Perl in that it is interpreted, not compiled. Java is referred to as a compiled language. Unlike most other programming languages, though, Java is not compiled to a native machine code, but rather to a Java byte code. Java byte code is an architecture-neutral byte-code compiled language. That is, an applet is compiled to Java byte code and then run by a machine-dependent runtime interpreter. Therefore, Java is much slower than general programming languages such as C++. Since Java is compiled, the common user cannot see the actual code behind the program. Nevertheless, when a user comes across a JavaScript script, he or she can generally see and even copy (legally or illegally) the code by simply using the browser to view the HTML source which contains the script (unless it is an external script). A compiled language has many other advantages. For example, a compiled program is much more efficient than one that is always interpreted directly from a text file, such as Perl and JavaScript. On the other hand, there are more than enough reasons to prefer an interpreted language over a compiled one. It is much easier and more convenient to debug and modify a program by simply modifying its text file rather than having to recompile it. Furthermore, most scripts and applets implemented in Java or JavaScript for Web usage do not require efficiency and are not resource demanding. Therefore, an interpreted language is somewhat more convenient.

Both Java and JavaScript are based on objects. However, their implementations of objects are different. JavaScript is an object-based language. It supports built-in, extensible objects, but no classes or inheritance. Its object hierarchy is not an inheritance one as in Java. JavaScript features two types of objects:

  • Static objects—objects that combine general functions (methods) and data constructs (properties). The values of such objects’ properties are usually read-only. A static object is a single object, and thus does not enable you to create instances of it. For example, the Math object in JavaScript is a static one, because you cannot create instances according to its template. Its methods are common mathematical functions, whereas its properties are mostly mathematical constants.
  • Dynamic objects—objects by which instances are created. A dynamic object resembles a template. You do not touch or use the object directly. In order to take advantage of such an object, you must create an instance of it. For example, the Date object is a dynamic object in JavaScript. An instance of that object is associated with a given date. You can create as many instances of a dynamic object as needed.

Java, on the contrary, is fully extensible. A programmer can create numerous classes that group objects together. A class is a term used in object-oriented programming vernacular to refer to a set of related objects that share common characteristics. Java programmers create their own extensions to the base set of tools or classes. JavaScript’s object model is somewhat simpler than Java’s equivalent, so JavaScript’s object implementation is much easier to accommodate than Java’s model.

Another difference between Java and JavaScript is their time of binding. JavaScript features dynamic binding, so all object references are checked at run time. Java, on the other hand, is based on static binding, meaning that all object references must exist and be valid at compile time. However, an object-oriented language may require dynamic method bindings because polymorphism allows multiple definitions of methods sharing a common name, and calling such polymorphic methods often cannot be resolved until run time. The most obvious reason for this difference is that JavaScript is not compiled, so checking object references at compile time has no meaning. If you are not familiar with Java, you should pay no attention to these confusing terms—simply bear in mind that the object implementation in JavaScript widely varies from that in Java.

When you come to writing a script, you surely need to use variables. In order to use a variable you must declare it. Another difference between Java and JavaScript is that Java is strongly typed, as opposed to JavaScript which is loosely typed. That is, when you declare a variable in Java, you must specify its data type. For example, when you create a variable to contain integer values you must use the int keyword. In JavaScript, all variables are declared in the same way. Furthermore, a variable of one data type can contain a value of a different data type elsewhere in the script. This behavior resembles other popular languages such as Perl. This topic is discussed later in the book.

We have intentionally left the most critical difference to the end. You have noticed that all differences discussed thus far are related to the language itself. A very important difference between Java and JavaScript is that JavaScript is integrated with, and embedded in, HTML. Although Netscape Navigator 3.0 introduced an alternative in the form of external files, that is still the general situation. Java, on the contrary, is neither integrated with nor embedded in HTML. Applets (small Java programs) are always distinct from the HTML code and are only invoked by it. A space is reserved on the Web page for later execution if needed. If you have some experience creating HTML documents, JavaScript’s implementations will be a piece of cake for you. When you create the HTML document for a Web page, you simply embed the JavaScript script according to its syntax and semantics.

Another significant difference between Java and JavaScript is the area in which they are executed. A Java applet resembles an image in that you give it a specified area on the page, and the applet executes independently in that area. Within the defined area, the applet can do virtually anything, including animation, games, and so forth. An applet does not have any effect outside its area. JavaScript, on the contrary, gives you access to the entire Web page. You can affect various elements in the page, provided that JavaScript is given permission to access them. For example, you can reference any form, link, image, and browser window via JavaScript. Netscape Navigator 3.0 introduced a new feature, LiveConnect, which enables Java to interact with JavaScript and vice versa. This tool makes it possible for a Java applet to interact with HTML elements through JavaScript, which is the connector in this case.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us