Home

      Hosting      Domains       Support       Contacts

 

JavaTM 5 Tutorial

By Omar Aldawud PhD for hostitwise.com 

Introduction

Computers follow a list of instructions one instruction at a time.  Start with first instruction, do each instruction in turn and stop when last instruction reached and executes.  Kinds of instructions a computer can follow (in one step) are similar to add the numbers 33 and 43.
Kinds of instructions a computer can’t follow (in one step) are something like calculate order totals.  The difference between the first kind of instructional in the 2nd type is the in the first computer instructions are simple (add 2 numbers, subtract one number from another), while in the latest computer must combine thousands of computer instructions in a program to do more complex tasks such as calculating totals.


Kinds of computer instructions

  • Arithmetic: add, subtract, multiply, divide
  • Comparison: compare two numbers to see which is greater, or whether the two numbers are equal or not
  • Branching: jump to some other instruction in the program (list of instructions) and continue processing from there
    A computer can execute millions of these simple instructions each second

Computers understand machine language directly, For example: add 16 and 23 in Intel 8086 machine language looks like this:

1011 0000 0001 0000
0000 0100 0001 0111
1010 0010 0100 1000 0000 0000 (Instruction says: Copy the number 16 into the AL register)

To write your first program, you need:

  • The Java 5 Development Kit (JDK™ 5.0).
  • A text editor such as NotePad, the simple editor included with the Windows platforms. To find NotePad, from the Start menu select Programs > Accessories > NotePad.
  • You could also use a Java Integrated Development Environment (IDE) such as: Borland® JBuilder®,  or Eclipse.

The Unicode Character Set

Each character stored as 16-bits the maximum number of characters that can be represented: 65,536 (216).  in ASCII character set (used by many programming languages) stores each character as 7 bits (maximum number of characters is 128). For compatibility, first 128 characters of Unicode set represent the ASCII characters.  Java uses Unicode character set.  Here are some samples of the unicode character set:

Unicode Character     Decimal Value
*                                 42
1                                 49
2                                 50
A                                 65
B                                 66
a                                 97
b                                 98
}                                 125
 

Java programming language uses a combination of compiler and interpreter. Compiler converts source code into byte codes (an instruction set for a virtual, machine-independent processor).  At run time, the Java Virtual Machine (JVM) interprets the byte codes and converts them into the machine language on which the program is running.  Java achieves portability by using both a compiler and an interpreter
Java compiler translates a Java program into an intermediate Bytecode--not machine language.  An interpreter program called the Java Virtual Machine (JVM) translates each successive instruction in the Bytecode program to machine language and immediately runs it.  Java platform is based on the idea that the same software should run on many different kinds of computers, consumer gadgets, and other devices. Thus Java programs are portable which means Java platform allows you to run the same Java application on different kinds of computers.
 

§Java virtual machine or JVMTM
–a translator that turns general Java platform instructions into tailored commands that make the programs.
The java Compiler javac
Creates bytecodes, Stored on disk as .class. Each file contains bytecode for one and only one class.
The java Class Loader
Loads bytecodes in memory.  Loads .class files from disk into memory.
The java Bytecode Verifier
Confirms that all bytecodes are: Valid and Don't violate Java’s Security restrictions. Works on bytecodes from memory.
The java Interpreter
Reads bytecodes and translate them into a language that is relevant to the targeted computer architecture: PC, Machintosh, Cell Phone. For applets execution its built into the browser or the appletviewer.
 

Java Programs

  • Applets, Small programs designed to add interactivity to Web sites.  Downloaded with the Web page and launched by the Internet browser.
  • Servlets, Run by Web server on the server.  Typically generate Web content.
  • Applications, Programs that run standalone on a client machine.

Programming Basics

Programming is translating a problem into ordered steps consisting of operations a computer can perform:

  • Input
  • Calculations
  • Comparisons of values
  • Moving data
  • Output

The order of execution of instructions is called flow of control.

Developing a Java Application
  1. Write the source code Using an Integrated Development Environment (IDE) or text editor
    Save in a .java file
  2. Compile the source code:
    javac ClassName.java
    Creates .class file
  3. Load
    Class Loader
  4. Verify
    Verifier (Security and Validation)
  5. Execute the application:
    java ClassName or you can use the appletviewer
    Run by the Java Virtual Machine
     

Your 1st Java Program

// First program in Java should be save as MyFirstClassProgram.java
public class MyFirstClassProgram
{
    public static void main( String [] args )
    {
         System.out.println( "Hostitwise.com is excellent for "   + " hosting java application" );
         System.exit( 0 );
      }
}

Comments in java starts with // and go until end of line (called line comments).  You can also use C++ comments style /* comments here */ the latest style can span multiple lines (block comments).  Java is case-sensitive. The class name and the source filename must match exactly, including capitalization. All Java programs consist of at least one class (MyFirstClassProgram in the above example).  Space, tab, newline are white space characters.
At least one white space character is required between a keyword and identifier. Any amount of white space characters are permitted between identifiers, keywords, operators.

Java Identifiers - symbolic names
Identifiers are used to name classes, variables, and methods.  Identifier Rules:

  • Must start with a "Java letter"
  • A - Z, a - z, _, $, and Unicode letters
  • Can contain essentially any number of Java letters and digits, but no spaces
  • Case sensitive (a does not equal A, Number1 and number1 are different)
  • Cannot be Java keywords or Java reserved words (if, else, while, for, etc. See text book Appendix A for a complete list of Java keywords)

Java Data Types
For all data, assign a name (identifier) and a data type.  Data type tells compiler: How much memory to allocate,  format in which to store data, and types of operations you will perform on data?
Java is a "strongly typed" language, which means that all identifiers must be declared before they can be used.  Jave supports the following
primitive data types: byte, short, int, long, float, double, char, boolean.

Type      Size in Bytes             Minimum Value                                   Maximum Value
byte             1                                       -128                                                    127
short            2                                    -32,768                                              32,767
int                4                      -2, 147, 483, 648                                 2, 147, 483, 647
long              8      -9,223,372,036,854,775,808                9,223,372,036,854,775,807
float             4                                   1.4E-45                                   3.4028235E38
double          8                                 4.9E-324                  1.7976931348623157E308
char            2              character  encoded as 0              character encoded as FFFF
boolean      2 values true and false

Declaring Variables
Variables hold one value at a time, but that value can change.  Syntax for declaring a variable:
dataType identifier;
int myAge;

or
dataType identifier1, identifier2, …;
int age, years, numberOfChildren;

Naming convention for variable names: first letter is lowercase, embedded words begin with uppercase letter.

Declaring Constant

In java constant value cannot change during program execution.  Syntax:
final dataType constantIdentifier = Value;
Note: assigning a value when the constant is declared is optional. But a value must be assigned before the constant is used.  Use all capital letters for constants and separate words with an underscore: Example:  final double TAX_RATE = .05; Declare constants at the top of the program so their values can easily be seen. Declare as a constant any data that should not change during program execution.

Examples:

int numPlayers = 10; // numPlayers holds 10
numPlayers = 8; // numPlayers now holds 8

int legalAge = 18;
int voterAge = legalAge;

The next statement is illegal
int height = weight * 2; // weight is not defined
int weight = 20;
and generates the following compiler error:
illegal forward reference.

Java Arithmetic Operators

 

Operator Operator
+ addition
- subtraction
* multiplication
/ division
% modulus(remainder after division)

 

Where To Go From Here:

Java Tutorial Home 2 3 4 5 6 7 8 9

 

 



 

Disclaimer
This tutorial is a summary of class notes for an introduction to java class.  the text book used in the class is
Java 5 Illuminated by Julie Anderson, Hervé Franceschi; Jones and Barlett Publishers (some examples used in this tutorial are modified versions of those found in the text book).

 

| Home | Hosting | Domains | Support |  Contacts |
|
Terms & Condition | Privacy Policy |


Copyright © 2005 by HostItWise.com Read our Copyright. All rights reserved.

 

LasX.com