|
| |
Introduction
 | Java was developed by
Sun Microsystems.
In theory, there are 3 reasons how Java differs from other languages such as
C++:
- Java applications can run on any hardware platform that has a Java
Virtual Machine (JVM) installed.
- It has a number of built-in facilities for dealing with networks (i.e.,
the Internet).
- It can be used to build both standalone and embedded applications.
|
 | Java uses a form of memory management called garbage collection
that enables very stable, long-lasting applications to be developed and run
continuously. |
 | Java syntax is very similar to C/C++ enabling a faster transition to Java
development for developers in C/C++ environments. |
 | For the record: Java and JavaScript, while they share a common syntax, are
entirely different languages. |
Language Fundamentals
Program Structure and Organization
 | Java program building blocks:
 | Java programs are organized by "classes" which are groups of
features designed to accomplish specific tasks. |
 | A class is actually a template that is used to create objects for the
program. |
 | A class is defined as follows:
 | class - a keyword |
 | className - the name of the class being defined, e.g., class
myClass( |
|
 | Classes generally support inheritance:
 | The sending class is called the superclass. |
 | The receiving class is called the subclass. |
 | The "new" keyword defines the creation of an object from a class
called instances: e.g., myClass sourceclassName = new myClass() |
 | The receiving class can extend the behavior or capabilities of the
sending class. |
|
 | Methods are features of classes that are used to perform various
actions:
 | returnType: the type of data returned by the method (void is used when
no data is returned). |
 | methodName: the name the method. |
 | parametersList: the input values for the method |
 | e.g., returnType methodName (plist1, plist2) ( |
 | Methods may be public, private, protected, and default |
 | Methods are called by the object name and specific method using dot
notation: objectName.methodName(); |
|
|
 | Commenting code: uses the same commenting scheme as C. |
 | Language/character sets: Java natively supports the ASCII character
set and is capable of unicode support and other character sets. |
 | Source code management:
 | Java classes are organized into files called packages. |
 | Packages can be imported into applications depending on their needs. |
|
 | Program delivery:
 | Java programs are compiled into bytecode which is a highly optimized
code that is executed by the Java Virtual Machine or JVM. |
 | The JVM is platform, hardware and operating system, specific. |
 | Java programs can be run as:
 | Embedded applications within other programs such as web browsers. |
 | Support programs for other applications, e.g., to support a web server
application. |
 | Standalone programs complete with user interfaces (command line,
windows/GUI). |
|
|

Data Types
Java, unlike C, offers a fixed set of data types that are independent of
hardware platforms.
 | Primitive types:
 | byte - 8 bits, -128 to 127, initializes to 0 |
 | short - 16 bits, -32768 to 32767, initializes to 0 |
 | int -- Integer - 32 bits, -2,147,483,648 to 2,147,483,647, initializes to 0
|
 | long - 64 bits, -9,223,372,036,854,775,808 to
-9,223,372,036,854,775,807, initializes to 0 |
|
 | Floating point:
 | float - 32 bits, 1.4E-45 to 3.4E+38, initializes to 0.0d |
 | double - 64 bits, 4.9E-324 to 1.7E+308, initializes to 0.0d |
|
 | Other types:
 | char - 2 bytes per character (first 128 bits represent ASCII) |
 | boolean - True or False, does not convert to numbers, initializes to
False |
|
 | Variables:
 | Defined by a type (see above) |
 | Are assigned a name. |
 | May be initialized with a value. |
 | Local and global variables are supported in classes. |
 | Constant variables may be created using the static keyword. |
 | Variables my be "casted" (casting) allow one variable type to be
converted to another (e.g., string to integer). |
|
 | Arrays: Java supports arrays, including multidimensional arrays.
Arrays are 0 referent. Brackets are used to define and manipulate array
elements. |

Expressions and Operators
Java shares a set of expressions and operators that are very similar to
C/C++:
 | Math operators (and precedence):
 | * Multiplication |
 | / Division |
 | + Addition |
 | - Subtraction and negation |
 | % Modulus |
 | ~ 1s complement |
 | ++ increment |
 | -- decrement |
|
 | Assignment operators:
 | = |
|
 | Relational operators:
 | == equals |
 | >, >= -- greater than, greater than/equal |
 | <, <= --less than, less than equal |
|
 | Logical operators:
 | & logical AND |
 | |, || logical OR (first is a short circuit OR, the second checks both
sides of the equation) |
 | ! logical negation |
 | ^ XOR |
|

Control Statements
Java uses a set of control statements that are very similar to those found in
C/C++:
 | If-then
|
if (expression) {
statement;
statement;
.... }
if (expression) {
statement;
statement; }
else
statement;
if (expression) {
statement;
statement; }
else {
statement;
statement; }
if (expression) {
statement;
statement; }
else if (expression)
statement;

Input/Output
 | Keyboard and direct screen output (command line interface) is possible via
Java. |
 | Browser and desktop applications may be supported by libraries offering
screen objects via the java.awt library including:
 | Text boxes, areas, and labels |
 | List boxes |
 | Radio and check boxes |
 | Menus and dialog boxes. |
 | Graphics support |
 | Text and fonts |
 | Images |
 | Audio clips |
 | Colors |
|
 | File I/O:
 | Java file I/O is not allowed in web browser applets. |
 | Java supports a complete range of file and network I/o via the java.io
and java.net libraries. |
|
 | Database: database support is available via operating system based tools
and interfaces to commercial database applications. |

Libraries and Support
Java, like C, is a relatively small language, with a powerful library to
support application development.
Libraries are stored in directories that are accessible to the Java Virtual
Machine.
Java libraries include:
 | Applet (java.applet) - supports web browser development. |
 | java.lang - supports objects, classes, strings, wrappers, threads, math,
error handling, and runtime classes. |
 | java.util - supports bit operator support, dates, dictionaries, hashtables,
randomizers, strings, and vectors. |
Resources
References
Tutorials
|