The Java Programming Language
We will now focus on Java, the programming language, as opposed to the broader Java Platform. The Java programming language remains the exact same across all platforms, operating systems, and hardware architectures. The language is governed by the Java Language Specification document available from Sun Microsystems. In this topic, we will focus on what you need to understand to write Java code: syntax, object-oriented programming basics, and the Java Software Development Kit.
Java Language Syntax
Java is very similar to the C++ programming language. In fact, Java has sometimes been referred to as "C++ minus". From a structural viewpoint, all Java code is structured the same way using the following rules.
- All variables and methods are declared within a class. The C++ programming language allows variables and functions to be declared outside of classes. In addition, the definition of functions and classes also occur in a separate file (known as a header file) in C++. Java is much simpler to manage and read because all of the code can only be found in one place.
- There can only be one public class within a single source code file (see the Information Hiding discussion below for more information on the term public).
- This source code file must be named using the public class’s name with the .java extension. For example, a file containing the class Airplane would be named Airplane.java. Once compiled, a file named Airplane.class will be generated.
Data Types
The following data types are inherent to the Java language. Since Java is object-oriented, any new class becomes a new data type for use by others but these data types are built into the language itself.
| byte | float |
| short | double |
| int | boolean |
| long | char |
Flow Control Statements
The operators and expressions supported by Java are virtually identical to those of the C and C++ programming language so we will not discuss them here. Java does support a number of control statements for handling branching within programs. These include the if-else, for loop, while loop, do-while loop, break, and continue statements.
Object-Oriented Programming Fundamentals
Grouping variables and methods into objects allows us to componentize related code in our minds and also greatly eases the reuse and up-front design of code. Related classes can be grouped into a package using the package keyword at the beginning of a Java source file:
package Automotive;
To reuse other classes or packages, use the import keyword:
import java.awt.Panel;
As we have already stated repeatedly, Java is an object-oriented programming (OOP) language. In fact, Tim Berners-Lee (inventor of the World Wide Web) once stated that with the advent of Java: Now we all have a reason to do object-oriented programming. The combination of OOP with Java’s distributed and cross-platform capabilities are the reasons behind its success. Without any one of these capabilities, it is possible that Java would have never been commercially viable.
An object-oriented programming language provides the developer with the capability to model things in the real world as, you guessed it, objects. It is possible to define and build a class named Automobile with variables such as driver_name, vendor, year, and license_number with methods such as drive(), stop(), start(), and accelerate(). To make use of this class, the developer would just instantiate the class in Java using the following syntax:
Automobile a = new Automobile();
From then on (within scope), he could use the Automobile object like so:
a.driver = "John Doe";
a.vendor = "Ford";
a.year = 1985;
a.license_number = "RFK 123";
a.start();
a.accelerate();
a.drive();
a.stop();
In addition, another custom object (such as a Family object) could make use of or encapsulate the Automobile object.
class Family
{
Automobile a;
House h;
Dog d;
Dad dd;
Mom mm;
Kids k;
}
Generally speaking, for a programming language to be considered object-oriented, it must support three tenets:
- Polymorphism
- Inheritance
- Encapsulation
Polymorphism
Inheritance
Encapsulation
Exception Handling
Java supports the structured handling of error conditions through the keywords try and catch. A method attempts to try some operation and catches any errors that may occur to keep the entire application from crashing. If you’re writing your own methods within a class, you can force callers to handle errors by using the throws keyword. As an example:
boolean RunRedLight(Automobile a, Streets s) throws Exception;
Not to belabor the OOP point, but since Exception is also a Java class, you can extend it as well. This means that you can define your own exceptions with special error functionality.
Other Resources
Books
See our Store for recommended Java books that can be ordered directly from WirelessDevNet.com.
Links
Java Home
JavaWorld
Java Developer’s Journal
Java Buyer’s Guide
Gamelan
Wireless Developer Network Message Boards
Next: Java GUI Programming
|