2.Java Programming Fundamentals

Thursday, August 6, 2009 Posted by Sudarsan
Lexical Issues

Java programs are a collection of white space, identifiers, comments, literals, operators, separators and keywords. The operators are described in the next chapter. The others are described next.

White space

Java is a free-form language. This means that you do not need to follow any special Indention rules as long as there is at least one white space character with each token that was not already delineated by an operator or separator. In Java, white space is a space, tab or new line.

Identifiers

Identifiers are used for class names, method names and variable names. An identifier may be any descriptive sequence of uppercase and lowercase letters, numbers or the underscore and dollar sign characters. They must not begin with a number again. Java is case sensitive, so VALUE is different identifier than Value.

Some examples of valid identifiers are: AvgTemp, count, a4, $test This_is_ok

Invalid variable names include: 2count, high - temp, Not/ok

Literals

A constant value in Java is created by using aliteral representation of it. For example, here are some literals:

100 98.6 'x' "This is a test"

Left to right the first literal specifies an integer, the next is a floating-point value, the third is a character constant and the last is a string.

Comments

There are three types of comments defined by Java.

Single-line comment
Multiline comment
Documentation comment
Single-line comments: line starts with "//"
Multiline comments: begins with "/*" and ends with a "*/"
Documentation comment: This type of comment is used to produce an HTML file that documents your program. The documentation comment begins with a /** and ends with a /*

Separators

In Java, there are a few characters that are used as separators. The most commonly used separator in Java is the semicolon. It is used to terminate statements. The separators are shown in the figure



The Java Keywords

There are 48 reserved keywords currently defined in the Java language. These keywords, combined with the syntax of the operators and separators, from the definition of the Java language. These keywords cannot be used as names for variables, class or method. The keywords const and go to are reserved but not used. In addition to the keywords, Java reserves the following: true, false and null. These are values defined by Java. You may not use these words for the names of variables classes and so on. These are defined in Table 2.2

abstract, const, finally, int, public, this
boolean, continue, float, interface, return, throw
break, default, for, long, short, throws
byte, do, goto, native, static, transient
case, double, if, new, strictfp, try
catch, else, implements, package, super, void
char, extends, import, private, switch, volatile
class, final, instanceof, protected, synchronized, while


Two Paradigms

All Computer programs consist of two elements: code and data. Furthermore, a program can be conceptually organized around its code or its data. That is, some programs are written around "What is happening" and others are written around "Who is being affected". These are the two paradigms that govern how a program is constructed. This first way is called the process-oriented model. The process-oriented model can be thought of as code acting on data.

To manage increasing complexity, the second approach, called object-oriented programming was conceived. Object-Oriented programming organizes a program around its data (that is, objects) and a set of well-defined interfaces to that data. An object-oriented program can be characterized as data controlling access to code.

Abstraction

An essential element of object-oriented programming is abstraction. Humans manage complexity through abstraction. For example, people do not think of a car as a set of tens of thousands of individual parts. They think of it as a well-defined object with its own unique behavior. This abstraction allows people to use a car to drive to the grocery store without begin overwhelmed by the complexity of the parts that form the car. They can ignore the details of how the engine, transmission and braking systems work. Instead they are free to utilize the object as a whole.

A powerful way to manage abstraction is through the use of hierarchical classifications. This allows you to layer the semantics of the complex systems, breaking them into more manageable pieces. From the outside, the car is a single object. Once inside, you see that the car consists of several subsystems: steering, breaks, sound system, seat belts, heating, cellular phone and so on. In turn, each of these subsystems is made up of more specialized units. For instance, the sound system consists of a radio, a CD player and/or a tape player. The point is that you manage the complexity of the car (or any other complex system) through the use of hierarchical abstractions.

Hierarchical abstractions of the complex systems can also be applied to the computer programs.

The Three OOP Principles

All Object Oriented Programming languages provide mechanisms that help you implement the object-oriented model. They are encapsulation, inheritance and polymorphism

Encapsulation

Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe outside interference and misuse. One way to think about encapsulation is as a protective wrapper that prevents the code and data from the begin arbitrarily accessed by other code defined outside the wrapper. Access to the code and data inside the wrapper is tightly controlled through a well-defined interface.

To relate this to the real world, consider the automatic transmission on an automobile. It encapsulates hundreds of bits of information about your engine, such as how much you are accelerating, the pitch of the surface you are on, and the position of the shift lever. You, as the user have only one method of affecting this complex encapsulation: by moving the gearshift lever. The gearshift lever is a well defined (indeed, unique) interface to the transmission. Further, what occurs inside the transmission does not affect objects outside the transmission. For example, shifting gears does not turn on the headlights. Because an automatic transmission is encapsulated, dozens of car manufacturers can implement one in any they please. However, from the driver's point of view they all work the same. This same idea can be applied to programming.

The power of encapsulated code is that everyone knows how to access it and thus can use it regardless of the implementation details and without fear of unexpected side effects. In Java, the basis of encapsulation is the class. A class defines the structure and behavior (data and code) that will be shared by a set of objects. Each object of a given class contains the structure and behavior defined by the class, as if it were stamped out by a mold in the shape of the class. For this reason, objects are sometimes referred to as instances of a class. Thus is a logical construct, an object has physical reality.

When you create a class, you will specify the code and data that constitute that class. Collectively, the elements are called members of the class.

Inheritance

Inheritance is a form of software reusability in which new classes are created from existing classes by absorbing their attributes and behaviors and embellishing these with capabilities the new class require. Software reusability saves time in program development. It encourages the reuse of proven and debugged high-quality software, thus reducing problems after a system becomes operational.

Let us develop a simple inheritance hierarchy. A typical university community has thousands of people who are community members. These people consist of employees, students and alumni. Employees are either administrators (such as deans and department heads) or teaching faculty. Note that the inheritance hierarchy could contain many other classes. For example, students can be graduates or under graduates and so on.



Polymorphism

Polymorphism (from the Greek means "many forms") is a feature that allows one interface to be used for a general class of actions. More generally, the concept of polymorphism if often expressed by the phrase "one interface, multiple methods". To understand how, consider the following. In languages that do not support polymorphism, there are usually three or more versions of a function, each with a slightly different name. For instance, in C, the function abs () returns the absolute value of an integer, labs () returns the absolute value of a long integer, and fabs () returns the absolute value of a floating-point-value. Although the underlying concept of each function is the same, you still have three names to remember. This situation does not occur in Java, because each absolute value method can use the same name, thus one interface, multiple methods. This helps reduce complexity by allowing the same interface to be used to specify a general class of action (i.e. method) as it applies to each situation. You, the programmer, do not need to make this selection manually. You only need to remember and utilize the general interface.

Important Points

  • Java programs usually go through 5 phases to be executed-edit, compile, load, verify and execute.
  • Java program file names end with the. java extension.
  • The Java compiler (javac) translates a Java program into bytecodes -the language understood by the Java interpreter. If a program compiles correctly, a file with the .class extension is produced. This is the file containing the bytecodes that are interpreted during the execution phase.
  • A Java program must first be placed in memory before it can be executed. This is done by the class loader, which takes the. Class file (or files) containing the byte codes and transfers it to the memory. The. Class file can be loaded from a disk on to your system all over the network
  • An application is a program. That is normally stored and executed from the user's local computer.
  • An applet is a small program that is normally stored on a remote computer that users connect to via a World Wide Web browser. Applets are loaded from a remote computer into a browser, executed in a browser and discarded when execution completed.
  • Applications are loaded into memory and executed using the Java interpreter.
  • Browsers are used to view documents on the World Wide Web called HTML (Hyper Text Markup Language) documents
  • When the browser sees an applet in an HTML document, the browser launches the java class loader to load the applet. The browsers that support java each have a built-in Java interpreter. Once applet is loaded, the Java interpreter in the browser begins executing the applet.
  • Applets can also be executed from the command line using the applet viewer command provided with the java 2 Software Development Kit (J2SDK). The applet viewer is commonly referred to as the minimum browser-it only knows how to interpret applets.



Post a Comment