Wednesday, December 22, 2010

JavaRefreshing

1 Class A template that describes the kinds of state and behavior that objects
of its type support.

2 Object At runtime, when the Java Virtual Machine (JVM) encounters the
new keyword, it will use the appropriate class to make an object which is an
instance of that class. That object will have its own state, and access to all of
the behaviors defined by its class.

3 State (instance variables) Each object (instance of a class) will have its
own unique set of instance variables as defined in the class. Collectively, the
values assigned to an object's instance variables make up the object's state.

4 Behavior (methods) When a programmer creates a class, she creates methods
for that class. Methods are where the class' logic is stored. Methods are
where the real work gets done. They are where

Monday, April 12, 2010


CLASSESAND OBJECT

Now that we have introduced operators, expressions, statements, and methods, we can finally talk about classes. A class is
a named collection of fields that hold data values and methods that operate on those values. Some classes also contain
nested inner classes. Classes are the most fundamental structural element of all Java programs. You cannot write Java code
without defining a class. All Java statements appear within methods, and all methods are defined within classes.
Classes are more than just another structural level of Java syntax. Just as a cell is the smallest unit of life that can survive
and reproduce on its own, a class is the smallest unit of Java code that can stand alone. The Java compiler and interpreter do
not recognize fragments of Java code that are smaller than a class. A class is the basic unit of execution for Java, which
makes classes very important. Java actually defines another construct, called an interface, that is quite similar to a class.
The distinction between classes and interfaces will become clear in Chapter 3, "Object-Oriented Programming in Java", but
for now I'll use the term "class" to mean either a class or an interface.
Classes are important for another reason: every class defines a new data type. For example, you can define a class named
Point to represent a data point in the two-dimensional Cartesian coordinate system. This class can define fields (each of
type double) to hold the X and Y coordinates of a point and methods to manipulate and operate on the point. The Point
class is a new data type.
When discussing data types, it is important to distinguish between the data type itself and the values the data type
represents. char is a data type: it represents Unicode characters. But a char value represents a single specific character. A
class is a data type; the value of a class type is called an object. We use the name class because each class defines a type (or
kind, or species, or class) of objects. The Point class is a data type that represents X,Y points, while a Point object
represents a single specific X,Y point. As you might imagine, classes and their objects are closely linked. In the sections
that follow, we will be discussing both.

synchronizedblog

The synchronized Statement
Since Java is a multithreaded system, you must often take care to prevent multiple threads from modifying an object
simultaneously in a way that might corrupt the object's state. Sections of code that must not be executed simultaneously are
known as critical sections. Java provides the synchronized statement to protect these critical sections. The syntax is:
synchronized ( expression ) {
statements
}
expression is an expression that must evaluate to an object or an array. The statements constitute the code of the critical
section and must be enclosed in curly braces. Before executing the critical section, the Java interpreter first obtains an
exclusive lock on the object or array specified by expression. It holds the lock until it is finished running the critical section,
then releases it. While a thread holds the lock on an object, no other thread can obtain that lock. Therefore, no other thread
can execute this or any other critical sections that require a lock on the same object. If a thread cannot immediately obtain the
lock required to execute a critical section, it simply waits until the lock becomes available.
Note that you do not have to use the synchronized statement unless your program creates multiple threads that share
data. If only one thread ever accesses a data structure, there is no need to protect it with synchronized. When you do
have to use synchronized, it might be in code like the following:
public static void SortIntArray(int[] a) {
// Sort the array a. This is synchronized so that some other thread
// cannot change elements of the array while we're sorting it (at
// least not other threads that protect their changes to the array
// with synchronized).
synchronized (a) {
// Do the array sort here...
}
}
The synchronized keyword is also available as a modifier in Java and is more commonly used in this form than as a
statement. When applied to a method, the synchronized keyword indicates that the entire method is a critical section. For
a synchronized class method (a static method), Java obtains an exclusive lock on the class before executing the method.
For a synchronized instance method, Java obtains an exclusive lock on the class instance. (Class and instance methods

JAVA BASICS

What Is Java?
In discussing Java, it is important to distinguish between the Java programming language, the Java
Virtual Machine, and the Java platform. The Java programming language is the language in which Java
applications (including applets, servlets, and JavaBeans components) are written. When a Java program
is compiled, it is converted to byte codes that are the portable machine language of a CPU architecture
known as the Java Virtual Machine (also called the Java VM or JVM). The JVM can be implemented
directly in hardware, but it is usually implemented in the form of a software program that interprets and
executes byte codes.
The Java platform is distinct from both the Java language and Java VM. The Java platform is the
predefined set of Java classes that exist on every Java installation; these classes are available for use by
all Java programs. The Java platform is also sometimes referred to as the Java runtime environment or
the core Java APIs (application programming interfaces). The Java platform can be extended with
optional standard extensions. These extension APIs exist in some Java installations, but are not
guaranteed to exist in all installations.
1.1.1. The Java Programming Language
The Java programming language is a state-of-the-art, object-oriented language that has a syntax similar
to that of C. The language designers strove to make the Java language powerful, but, at the same time,

they tried to avoid the overly complex features that have bogged down other object-oriented languages,
such as C++. By keeping the language simple, the designers also made it easier for programmers to write
robust, bug-free code. As a result of its elegant design and next-generation features, the Java language
has proved wildly popular with programmers, who typically find it a pleasure to work with Java after
struggling with more difficult, less powerful languages.
1.1.2. The Java Virtual Machine
The Java Virtual Machine, or Java interpreter, is the crucial piece of every Java installation. By design,
Java programs are portable, but they are only portable to platforms to which a Java interpreter has been
ported. Sun ships VM implementations for its own Solaris operating system and for Microsoft Windows
(95/98/NT) platforms. Many other vendors, including Apple and various Unix vendors, provide Java
interpreters for their platforms. There is a freely available port of Sun's VM for Linux platforms, and
there are also other third-party VM implementations available. The Java VM is not only for desktop
systems, however. It has been ported to set-top boxes, and versions are even available for hand-held
devices that run Windows CE and PalmOS.
Although interpreters are not typically considered high-performance systems, Java VM performance is
remarkably good and has been improving steadily. Of particular note is a VM technology called just-intime
( JIT) compilation, whereby Java byte codes are converted on-the-fly into native-platform machine
language, boosting execution speed for code that is run repeatedly. Sun's new Hotspot technology is a
particularly good implementation of JIT compilation.
1.1.3. The Java Platform
The Java platform is just as important as the Java programming language and the Java Virtual Machine.
All programs written in the Java language rely on the set of predefined classes[1] that comprise the Java
platform. Java classes are organized into related groups known as packages. The Java platform defines
packages for functionality such as input/output, networking, graphics, user-interface creation, security,
and much more.
[1]A class is a module of Java code that defines a data structure and a set of methods (also
called procedures, functions, or subroutines) that operate on that data.
The Java 1.2 release was a major milestone for the Java platform. This release almost tripled the number
of classes in the platform and introduced significant new functionality. In recognition of this, Sun named
the new version the Java 2 Platform. This is a trademarked name created for marketing purposes; it
serves to emphasize how much Java has grown since its first release. However, most programmers refer
to the Java platform by its official version number, which, at the time of this writing, is 1.2.[2]
)
It is important to understand what is meant by the term platform. To a computer programmer, a platform
is defined by the APIs he or she can rely on when writing programs. These APIs are usually defined by
the operating system of the target computer. Thus, a programmer writing a program to run under
Microsoft Windows must use a different set of APIs than a programmer writing the same program for the
Macintosh or for a Unix-based system. In this respect, Windows, Macintosh, and Unix are three distinct
platforms.
Java is not an operating system.[3] Nevertheless, the Java platform--particularly the Java 2 Platform--
provides APIs with a comparable breadth and depth to those defined by an operating system. With the
Java 2 Platform, you can write applications in Java without sacrificing the advanced features available to
programmers writing native applications targeted at a particular underlying operating system. An
application written on the Java platform runs on any operating system that supports the Java platform.
This means you do not have to create distinct Windows, Macintosh, and Unix versions of your programs,
for example. A single Java program runs on all these operating systems, which explains why "Write
once, run anywhere" is Sun's motto for Java.
[3]There is a Java-based operating system, however; it is known as JavaOS.
It also explains why companies like Microsoft might feel threatened by Java. The Java platform is not an
operating system, but for programmers, it is an alternative development target and a very popular one at
that. The Java platform reduces programmers' reliance on the underlying operating system, and, by
allowing programs to run on top of any operating system, it increases end users' freedom to choose an
operating system.
1.1.4. Versions of Java
As of this writing, there have been four major versions of Java. They are:
Java 1.0
This was the first public version of Java. It contained 212 classes organized in 8 packages. There
is a large installed base of web browsers that run this version of Java, so this version is still in use
for writing simple applets--Java programs that are included in web pages. (See Java Foundation
Classes in a Nutshell (O'Reilly) for a discussion of applets.)
Java 1.1
This release of Java doubled the size of the Java platform to 504 classes in 23 packages. It
introduced inner classes, an important change to the Java language itself, and included significant
performance improvements in the Java VM. This version of Java is out of date, but is still in use
on systems that do not yet have a stable port of Java 1.2.

Java 1.2
This is the latest and greatest significant release of Java; it tripled the size of the Java platform to
1520 classes in 59 packages. Because of the many new features included in this release, the
platform was renamed and is now called the Java 2 Platform.
Java 1.3 (beta)
This release includes minor corrections and updates to the Java platform, but does not include
major changes or significant new functionality.
In addition, Sun has instituted a process for proposing and developing standard extensions to the Java
platform. In the future, most new functionality is expected to take the form of a standard extension, rather
than be a required part of every Java installation.
In order to work with Java 1.0 or Java 1.1, you have to obtain the Java Development Kit ( JDK) for that
release. As of Java 1.2, the JDK has been renamed and is now called a Software Development Kit
(SDK), so we have the Java 2 SDK or, more precisely, the Java 2 SDK, Standard Edition, Version 1.2 (or
Version 1.3 beta). Despite the new name, many programmers still refer to the development kit as the
JDK.