Sunday, 10 May 2015

Language Features and Syntax of Java

 It is now time to compare basic features of the Java language versus features of the Visual Basic language. The first item to be examined is data types.

Data Types

Java contains a small set of basic language types. (Ignore for now the fact that each new object declared in Java is a new "temporary" data type.) Here are the types:
boolean-Can be trueor false

byte-8-bit signed quantity
short-16-bit signed quantity 
int-32-bit signed quantity 
float-32-bit floating-point value
double-64-bit floating-point value
long-64-bit signed quantity 
char-16-bit Unicode character
Listed next are the Visual Basic basic data types. Next to each VB data type is the equivalent (if any) Java data type:
Integer-16-bit signed quantity; equivalent to short.
Long-32-bit signed quantity; equivalent to int.
Single-32-bit floating-point value; equivalent to float.
Double-64-bit floating-point value; equivalent to double.
Currency-64-bit floating-point value limited to four decimal places; there is no Java equivalent, although a Currency class could be defined (and undoubtedly will be with a full set of properties and methods).
String-8-bit character; although the Java char type appears to be equivalent, caution should be used here. Note that the Java char type is actually a Unicode character, whereas the Visual Basic Stringmakes use of the ASCII characters. There are Java functions that can be used to convert Unicode characters to their ASCII values, and vice versa.
Byte-8-bit unsigned quantity; no Java equivalent.
Boolean-16-bit value used to store True or False; equivalent to boolean.
Date-64-bit value; Visual Basic automatically converts this number to represent a date between January 1, 100 and December 31, 9999. There is no Java basic type equivalent, although as with Currency, it would be relatively easy to build a date class.
Object-32-bit Objectreference value; no Java equivalent, unless the classkeyword is considered.
Variant-16 bytes (8 bits/byte) plus 1 byte for each character; no Java equivalent.

Declaring Variables

Variables are declared in Visual Basic using the dimkeyword. To declare an Integervariable within a function, the following syntax is used:
dim x As Integer
Java has no keyword that designates a variable being defined. Instead, the following syntax is used:
<data type> <variable_name>;
To repeat this example, an integer variable would be declared in Java as follows (keep in mind that Java is case sensitive!):
int x;
What is completely different about Java, however, is the fact that there are no global variables. Every variable and method in Java must be declared inside some class. This forces everything to be stored and referenced as an object, thus enforcing the object-oriented paradigm.
Any object in Java can be stored as an array of that object. This can be done in three steps:
  1. Declaring the array of objects using the syntax <data type> <variable_name>[];
  2. Example: int group[];
  3. Creating a new array object. Although the preceding statement declared a new array *object, it must actually be initialized in memory using the new operator:
  4. Int[] group = new int[50];
  5. Filling the array with values. This can be done using standard array notation:
    Group[15] = 304;

Programming Constructs

All modern programming languages contain programming constructs such as for and whileloops and if…then branches. Visual Basic and Java are no exception. This section provides a comparison of the primary differences between the control structures of the two languages.
Visual Basic supports the following decision structures:
If...Then

If...Then...Else
Select Case

The If…Then Structure

The syntax for producing a Visual Basic If…Thenstructure is the following:
For a single-line statement:
If condition Then statement
For multiple statements:
Ifcondition Then

  statements
End If
To add an additional Elseblock to the preceding structure, the syntax looks like this:
If condition1 Then

  [statementblock1]
[ElseIf condition2 Then
  [statementblock2]]...
[Else
  [statementblock3]]
End If
Java greatly simplifies this construct with the following syntax:
if condition1 {

  [statementblock1]
}
else if condition2 {
  [statementblock2]
}
else {
  [statementblock3]
}

The Select Case (orswitch) Structure

To provide an alternative to many multiple If…Thenstructures, most languages provide a mechanism of selectively executing blocks of code depending on the value of some variable.
In Visual Basic, you can do this by creating a Select Case structure. Here's the syntax:
Select Case testexpression 

  [Case expressionlist1
    [statementblock1]
  [Case expressionlist2
    [statementblock2]
  .
  .
  .
  [Case Else
    [statementblockn]
End Select
In Java, this construct is known as a switchstatement (similar to C/C++). The testexpressionmust be a byte, char, short, or int. Its syntax is just like C's:
switch (testexpression) {

  [case expressionlist1:]
    [statementblock1]
   break;
  [case expressionlist2:]
    [statementblock2]
   break;
  .
  .
  .
  default:
    [statementblockn]
  break;
}

The for Loop

For loops are used to execute a group of statements a set number of times. Both Visual Basic and Java provide a forloop, but there are differences between the two. In Visual Basic, the for loop has the following properties:
Forcounter = start To end [Step increment]

  statements
Next [counter]
Visual Basic allows the programmer to set the start and ending value of the variable counteras well as set the Stepincrement. Here is the Java forloop:
for(counter = start; booleanexpression; expression) {

  statements
}
There are several subtle differences between the Java forloop and that of Visual Basic. The Java forloop allows the programmer to provide an expression as opposed to the Visual Basic increment. This expression could be a call to a method or, as in VB, the programmer could simply increment or decrement a value. In addition to that, Java allows a variable to be declared in the forloop. (See Listing 6.5 for an example.)

Listing 6.5. Example of variable initialization in a Java forloop.
for (int counter = 1; counter < 99; counter ++) {

     /* statements */
}

The variable declared in the forloop cannot be used outside of that loop, however. (This is yet another example of a potential error-causing language feature that the Java designers have removed!)

The while and do…whileLoops

Visual Basic and Java provide nearly identical whileand do…while loops. The do loop is used to execute a group of statements an unknown amount of times (until the condition returns true). The do…whileloop executes the group of statements at least once before testing the condition. The syntactical differences will be shown here.
In Visual Basic:
Do While condition 

  statements
Loop
Do
  statements
Loop While condition
In Java:
while (condition) {
  statements
}
do {
  statements
} while (condition);

Language Features Missing from Visual Basic

There is some similarity between Visual Basic and Java (particularly in the area of GUI programming), but there are some language features in Java that just are not available using Visual Basic. All of these topics will be discussed in greater detail later in this book; however, they will be touched on now so that the Visual Basic programmer is aware of the differences.

Threads

32-bit Windows developers are perhaps familiar with the concept of multithreaded programming. A thread is a single self-contained set of states of execution of a program. A multithreaded environment (or language) allows multiple threads to run simultaneously (in parallel). In other words, threads allow applications to execute simultaneous operations at the same time. This is most often seen in applications where the display is being updated with some type of graphics while the application is performing some type of computation in the background. (See the JDK's Animator example.) Java applets and applications that take advantage of threading can be identified easily because they implement the Runnableinterface. Implementing the Runnableinterface can be thought of as opening a window into Java's threading capabilities.

It should be noted that on single-processor machines, both threads are not actually executing concurrently. The execution of the threads will switch between the two at regular intervals. Using a machine with multiple processors, it is possible to split the threads off and run them at the same time. This type of operation is operating-system dependent.

Networking

The java.net package  provides the Java programmer with a full complement of networking capabilities using both HTTP (the World Wide Web protocol) and sockets. Sockets were originally implemented for UNIX. They allow the programmer to open a pipe to another machine (or to their own machine, if so desired) for passing data back and forth. Entire books have been written on socket programming, so we won't go into much detail here. Suffice it to say, however, that the Java networking classes are platform-independent and will continue to support open, standards-based protocols. These classes also allow Java applets to do things like call Web pages up in a browser and stream data to and from the client computer over the Internet.

Exception Handling

The ability to handle application exceptions is a powerful feature that is common to many languages including C++, Ada, and Object Pascal. An exception is any type of condition that prevents a program from continuing in its current state. Without exception handling, the program would either crash or exit at that point. Java allows this exception to be "caught" and handled appropriately.

No comments:

Post a Comment