7.1 The Java Language


Java is a programming language and environment that was designed to solve a number of problems in modern programming practice. It started as a part of a larger project to develop advanced software for consumer electronics. These are small, reliable, portable, distributed real-time embedded systems. When James Gosling and his group started the project, they intended to use C++, but quickly stumbled on a number of crippling problems. Initially these were just compiler technology problems, but as time passed they realized that a new programming language was required.

One of the key properties that makes Java suitable for iTV programming is the ability to build mobile code that can run in small machines (see Figure 7.1). The source code of an iTV program is typically produced using Integrated Development Environments (IDE), which often enable generating code using wizards and subsequent editing of the code manually. Typically, the IDE allows developers to compile the code into executable mobile class files. Those files are transported to receivers, which use the JVM interpreter to execute the code residing within those class files [JVM]. The size of the basic JVM interpreter and class support is about 30 kB, adding the basic standard libraries and thread support ( essentially a self-contained microkernel ) brings it up to about 120 kB. The size of a Java program, which needs to be transported over the air, is usually on the order of 100 kB, as opposed to the megabyte code sizes common in modern PC environments.

Figure 7.1. The iTV Java programming model.

Java is a complete implementation environment equipped with a library of reusable tested code rendering the development process more rapid and reliable. It omits many rarely used, poorly understood , confusing features of C++ that often bring more grief than benefit. This primarily consists of operator overloading (although it does have method overloading), multiple inheritance, and extensive automatic coercions.

7.1.1 Encoding

Java programs are written in plaintext using the Unicode character encoding, version 1.1.5. Except for comments and identifiers and the contents of character and string literals, all input elements in a Java program are formed from only ASCII characters (ANSI X3.4), the American Standard Code for Information Interchange.

7.1.2 Naming Mechanisms

7.1.2.1 Identifiers

An identifier serves to identify a component of a program or the data it processes. In Java, it is an unlimited-length sequence of Unicode letters and digits, the first of which is a letter. Letters and digits may be drawn from the entire Unicode character set. This allows Java programmers to use identifiers in their programs that are written in their native languages.

Two identifiers are the same only if they have the same Unicode character for each letter or digit; identifiers that have the same external appearance may still be different, because it is possible that several Unicode strings are rendered exactly the same way.

7.1.2.2 Names

Names are used to refer to entities declared in a Java program. A declared entity is a package, type, member (field or method) of a type, parameter, or local variable. A simple name is a single identifier. Qualified names provide access to members of packages and reference types. A qualified name consists of a name, a "." token, and an identifier, in a sequence. For example, com.xyz.myClassName is an identifier of a class called myClassName ; the prefix com.xyz is a package name and often implies a directory structure in which this class file could be found.

Not all identifiers in Java programs are part of a name. Identifiers are also used in declarations, where the identifier determines the name by which an entity is known, in field access expressions and method invocation expressions, in statement labels, and in statements that refer to those labels.

7.1.2.3 Packages

Java programs are organized sets of packages. A package consists of a number of compilation units and associated with a hierarchical namespace. Packages are typically independently developed, and each package has its own set of names, reducing (and often eliminating) name conflicts.

Each Java host determines how packages, compilation units, and subpackages are created and stored; which top-level package names are in scope in a particular compilation; and which packages are accessible. Packages may be stored in a local file system, in a distributed file system, or in some form of database.

Due to the use of Unicode character sets, a package name component or class name might contain a character that cannot legally appear in a host file system's ordinary directory or file name (most file systems allow only ASCII characters in file names). Unnamed packages are supported primarily for providing convenience when developing small or temporary packages.

An import declaration allows a type declared in another package to be known by a short name rather than by the fully qualified name of the type. An import declaration affects only the type declarations of a single compilation unit. A compilation unit automatically imports each of the public type names declared in the predefined package java.lang .

7.1.2.4 Members

Package and reference types have members. The members of a package are sub packages and all the class and interface types declared in all the compilation units of the package. The members of a reference type are fields and methods .

7.1.3 Types

Java is a strongly typed language, which means that every variable and every expression has a type that is known at compile time, as opposed to run time (e.g., ECMA-script interpreters bind types at run time). Types restrict the values that a variable can hold or that an expression can produce, restrict the operations supported on those values, and determine the meaning of those operations. Strong typing helps detect some errors and can serve to improve efficiency.

The types of the Java language are divided into two categories: primitive types and reference types, with three respective categories of data values, primitive values, and reference values, that can be stored in variables , passed as arguments, returned by methods, and operated on. There is also a special null type that has no name and can always be converted to any reference type. In practice, the Java programmer can ignore the null type and just pretend that null is a special literal that can be of any reference type.

The Java primitive types are named by a reserved keyword. The primitive types are the boolean type and the numeric types. The boolean type has the truth values true and false. The numeric types are the integral types and the floating-point types. The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit, and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing Unicode characters. The floating-point types are float, whose values are 32-bit IEEE 754 floating-point numbers, and double, whose values are 64-bit IEEE 754 floating-point numbers. The IEEE 754 standard includes not only positive and negative sign-magnitude numbers , but also positive and negative zeroes, positive and negative infinities, and a special NaN value. The NaN value is used to represent the result of certain operations such as dividing zero by zero.

7.1.4 The Object and String Class

Two special classes in Java are the Object and String classes. The standard class Object is the superclass of all other classes. A variable of type Object can hold a reference to any object, whether it is an instance of a class or an array. All class and array types inherit the methods of class Object .

String objects, namely java.lang.String , are constant and their values cannot be changed after they are created. Strings are comparable and serializable sequences of characters. String buffers support mutable strings. Because String objects are immutable they can be shared. For example, String str="abc" is equivalent to String str = new String(data) where char data[]={'a','b','c'} .

7.1.5 References

There are three kinds of references: to class types, to interface types, and to array types. An object is a dynamically created class instance or an array. The reference values (i.e., references) are pointers to these objects. A special null reference is used to indicate that a reference does not point to any object.

Reference types form a hierarchy. Each class type is a subclass of another class type, except for the class Object , which is the superclass of all other class types. All objects, including arrays, support the methods of class Object . String literals are references to instances of class String .

There may be many references to the same object. Most objects have state, stored in the fields of objects that are instances of classes or in the variables that are the components of an array object. If two variables contain references to the same object, the state of the object can be modified using one variable's reference to the object, and then the altered state can be observed through the other variable's reference.

Memory management is one of the most noteworthy features of Java. An object is created in the Java heap, and is garbage collected after there are no more references to it. All object instances as well as all references are fully contained within the memory space of the JVM process executing the Java code. It is the responsibility of the JVM to perform garbage collection and reclaim memory used by unreferenced objects. Objects are never reclaimed or freed by explicit Java language directives. Each object has an associated lock that is used by synchronized methods and by the synchronized statement to provide control over concurrent access to state by multiple threads.

7.1.6 Variables

A variable represents a location in memory at which data is stored. It has an associated type that is either a primitive type or a reference type. Compatibility of the value of a variable with its type is guaranteed by the design of the Java language. A variable always contains a value that is assignment compatible with its type; its value prior to initialization is also compatible with its type. A variable of a primitive type always holds a value of that primitive type. A variable of reference type can hold either a null reference or a reference to any object whose class is assignment compatible with the type of the variable.

  • Class variable : This is a field of a class type declared using the keyword static within a class declaration, or with or without the keyword static in an interface declaration. Class variables are created when the class or interface is loaded and they are initialized on creation to default values. All instances of the same class share a single copy of each class variable. The class variable effectively ceases to exist (i.e., it is garbage collected) when its class or interface is unloaded after any necessary finalization of the class has been completed.

  • Instance variable : This is a field declared within a class declaration without using the keyword static. If a class T has a field a that is an instance variable, then a new instance variable a is created and initialized to a default value as part of each newly created object of class T or of any class that is a subclass of T . The instance variable effectively ceases to exist (i.e., garbage collected) when the object of which it is a field is no longer referenced, after any necessary finalization of the object has been completed.

  • Array components : These are unnamed variables that are created and initialized to default values whenever a new array object is created. The array components effectively cease to exist when the array is no longer referenced.

  • Method parameters : These name argument values are passed to a method. For every parameter declared in a method declaration, a new parameter variable is created each time that method is invoked. The new variable is initialized with the corresponding argument value from the method invocation. The method parameter effectively ceases to exist (i.e., it is garbage collected) when the execution of the body of the method is complete.

  • Constructor parameters : These name argument values are passed to a constructor. For every parameter declared in a constructor declaration, a new parameter variable is created each time a class instance creation expression or explicit constructor invocation is evaluated. The new variable is initialized with the corresponding argument value from the creation expression or constructor invocation. The constructor parameter effectively ceases to exist (i.e., it is garbage collected) when the execution of the body of the constructor is complete.

  • Exception-handler parameter : These variables are created each time an exception is caught by a catch clause of a try statement. The new variable is initialized with the actual object associated with the exception. The exception-handler parameter effectively ceases to exist (i.e., it is garbage collected) when execution of the block associated with the catch clause is complete.

  • Local variables : These are declared by local variable declaration statements. Whenever the flow of control enters a block or a for statement, a new variable is created for each local variable declared in a local variable declaration statement immediately contained within that block or for statement. The local variable is not initialized, however, until the local variable declaration statement that declares it is executed. The local variable effectively ceases to exist (i.e., it is garbage collected) when the execution of the block or for statement is complete.



ITV Handbook. Technologies and Standards
ITV Handbook: Technologies and Standards
ISBN: 0131003127
EAN: 2147483647
Year: 2003
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net