THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

Classes are always part of a package. A package is named by providing a package declaration at the top of the
source file:


package com.sun.games;


class Card {
// ...
}


If a package is not specified via a package declaration, the class is made part of an unnamed package. An
unnamed package may be adequate for an application (or applet) that is not loaded with any other code.
Classes destined for a library should always be written in named packages.


1.17. The Java Platform


The Java programming language is designed to maximize portability. Many details are specifically defined for
all implementations. For example, a double is a 64-bit IEEE 754 floating-point number. Many languages
leave precise definitions to particular implementations, making only general guarantees such as minimum
range, or they provide a way to ask the system what the range is on the current platform.


These portable definitions for the Java programming language are specific all the way down to the machine
language into which code is translated. Source code is compiled into Java bytecodes, which are designed to be
run on a Java virtual machine. Bytecodes are a machine language for an abstract machine, executed by the
virtual machine on each system that supports the Java programming language.[2] Other languages can also be
compiled into Java bytecodes.


[2] A system can, of course, implement the Java virtual machine in silicon that is, using a
special-purpose chip. This does not affect the portability of the bytecodes; it is just another
virtual machine implementation.

The virtual machine provides a runtime system, which provides access to the virtual machine itself (for
example, a way to start the garbage collector) and to the outside world (such as the output stream
System.out). The runtime system checks security-sensitive operations with a security manager or access
controller. The security manager could, for example, forbid the application to read or write the local disk, or
could allow network connections only to particular machines. Exactly what an application is allowed to do, is
determined by the security policy in force when the application runs.


When classes are loaded into a virtual machine, they will first be checked by a verifier that ensures the
bytecodes are properly formed and meet security and safety guarantees (for example, that the bytecodes never
attempt to use an integer as a reference to gain access to parts of memory).


These features combined give platform independence to provide a security model suitable for executing code
downloaded across the network at varying levels of trust. Source code compiled into Java bytecodes can be
run on any machine that has a Java virtual machine. The code can be executed with an appropriate level of
protection to prevent careless or malicious class writers from harming the system. The level of trust can be
adjusted depending on the source of the bytecodesbytecodes on the local disk or protected network can be
trusted more than bytecodes fetched from arbitrary machines elsewhere in the world.

Free download pdf