Wednesday, June 11, 2008

To Install Java

These instructions are to help you download and install Java on your personal computer. You must install Java before installing Eclipse, and you will need both.

Downloading and Installing Java On Windows:

Prevent Errors like --> 'javac' is not recognized as an internal or external command

1. Go to http://java.sun.com and download the latest Version of Jave SDK or any Jace SDK as per your requirement and install on your system.

2. Accept all of the defaults and suggestions, but make sure that the location where Java will be installed is at the top level of your C: drive. Click on "Finish." You should have a directory (folder) named C:\j2sdk1.5.0_04, with subfolders C:\j2sdk1.5.0_04\bin and C:\j2sdk1.5.0_04\lib

4. Modify your system variable called "PATH" (so that programs can find where Java is located).
To do this for Windows 2000 or XP, either right-click on the My Computer icon or select "System" on the control panel. When the window titled "System Properties" appears, choose the tab at the top named "Advanced." Then, click on "Environment Variables." In the bottom window that shows system variables, select "Path" and then click on "Edit..." Add C:\j2sdk1.5.0_04\bin as the first item in the list. Note that all items are separated by a semicolon, with no spaces around the semicolon. You should end up with a path variable that looks something like
C:\j2sdk1.5.0_04\bin;C:\WINNT\system32;C:\WINNT;C:\WINNT\system32\Wbem

For Windows 98 or ME, open the file AUTOEXEC.BAT in Notepad. You should find a line in this file that begins
SET PATH=...
Modify this line to add C:\j2sdk1.5.0_04\bin; immediately after the equals sign.

5. Modify or create a system variable called "CLASSPATH," as follows. In the lower "System Variables" pane choose "New..." and type in Variable Name "CLASSPATH" and value (note that it begins with dot semicolon)
.;C:\j2sdk1.5.0_04\lib

6. To test Java to see if everything is installed properly, open a command window (a DOS window) and type the command "javac" The result should be information about the Usage of javac and its options. If you get a result that "'javac' is not recognized as an internal or external command, operable program or batch file" then there is a problem and Java will not work correctly.

Java Hello World Program:

Our first application will be extremely simple - the obligatory "Hello World". The following is the Hello World Application as written in Java. Type it into a text file or copy it out of your web browser, and save it as a file named HelloWorld.java. This program demonstrates the text output function of the Java programming language by displaying the message "Hello world!". Java compilers expect the filename to match the class name.

A java program is defined by a public class that takes the form:
 public class program-name {

optional variable declarations and methods

public static void main(String[] args) {
statements
}

optional variable declarations and methods

}
Source Code

In your favorite editor, create a file called HelloWorld.java with the following contents:

/** Comment
* Displays "Hello World!" to the standard output.
*/

class HelloWorld {

public static void main (String args[]) {

System.out.println("Hello World!"); //Displays the enclosed String on the Screen Console

}

}


To compile Java code, we need to use the 'javac' tool. From a command line, the command to compile this program is:

javac HelloWorld.java

For this to work, the javac must be in your shell's path or you must explicitly specify the path to the program (such as c:\j2se\bin\javac HelloWork.java). If the compilation is successful, javac will quietly end and return you to a command prompt. If you look in the directory, there will now be a HelloWorld.class file. This file is the compiled version of your program. Once your program is in this form, its ready to run. Check to see that a class file has been created. If not, or you receive an error message, check for typographical errors in your source code.

You're ready to run your first Java application. To run the program, you just run it with the java command:

java HelloWorld

Sample Run

Hello world!

The source file above should be saved as myfirstjavaprog.java, using any standard text editor capable of saving as ASCII (eg - Notepad, Vi). As an alternative, you can download the source for this tutorial.

HelloWorld.java


Note: It is important to note that you use the full name with extension when compiling (javac HelloWorld.java) but only the class name when running (java HelloWorld).


Java Comments:

The Java programming language supports three kinds of comments:

/* text */
The compiler ignores everything from /* to */.

/** documentation */
This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /* and */. The JDK javadoc tool uses doc comments when preparing automatically generated documentation.

// text

The compiler ignores everything from // to the end of the line.

Example

Java denotes comments in three ways:

1. Double slashes in front of a single line comment:

int i=5; // Set the integer to 5

2. Matching slash-asterisk (/*) and asterisk-slash (*/) to bracket multi-line comments:

/*
Set the integer to 5
*/
int i=5;

3. Matching slash-double asterisk (/**) & asterisk-slash(*/) for Javadoc automatic hypertext documentation, as in

/**
This applet tests graphics.
*/
public class testApplet extends applet{...

or

/**
* Asterisks inside the comment are ignored by javadoc so they
* can be used to make nice line markers.
**/

The SDK tool javadoc uses the latter /** ..*/ comment style when it produces hypertext pages to describe a class.

Java Data and Variables:

There are 8 primitive data types. he 8 primitive data types are numeric types. The names of the eight primitive data types are:

byte short int long float double char boolean

There are both integer and floating point primitive types. Integer types have no fractional part; floating point types have a fractional part. On paper, integers have no decimal point, and floating point types do. But in main memory, there are no decimal points: even floating point values are represented with bit patterns. There is a fundamental difference between the method used to represent integers and the method used to represent floating point numbers.

Integer Primitive Data Types

Type Size Range
byte 8 bits -128 to +127
short 16 bits -32,768 to +32,767
int 32 bits (about)-2 billion to +2 billion
long 64 bits (about)-10E18 to +10E18

Floating Point Primitive Data Types

Type Size Range
float 32 bits -3.4E+38 to +3.4E+38
double 64 bits -1.7E+308 to 1.7E+308

Examples

int yr = 2006;
double rats = 8912 ;

For each primitive type, there is a corresponding wrapper class. A wrapper class can be used to convert a primitive data value into an object, and some type of objects into primitive data. The table shows primitive types and their wrapper classes:

primitive type Wrapper type
byte Byte
short Short
int Int
long Long
float Float
double Double
char Character
boolean Boolean

Variables only exist within the structure in which they are defined. For example, if a variable is created within a method, it cannot be accessed outside the method. In addition, a different method can create a variable of the same name which will not conflict with the other variable. A java variable can be thought of as a little box made up of one or more bytes that can hold a value of a particular data type:

Syntax: variabletype variablename = data;

Source Code ( demonstrating declaration of a variable )

class example
{
public static void main ( String[] args )
{
long x = 123; //a declaration of a variable named x with a datatype of long

System.out.println("The variable x has: " + x );
}
}

Source Code

public class MaxDemo {
public static void main(String args[]) {
//integers
byte largestByte = Byte.MAX_VALUE;
short largestShort = Short.MAX_VALUE;
int largestInteger = Integer.MAX_VALUE;
long largestLong = Long.MAX_VALUE;

//real numbers
float largestFloat = Float.MAX_VALUE;
double largestDouble = Double.MAX_VALUE;

//other primitive types
char aChar = 'S';
boolean aBoolean = true;

//Display them all.
System.out.println("largest byte value is " + largestByte + ".");
System.out.println("largest short value is " + largestShort + ".");
System.out.println("largest integer value is " + largestInteger + ".");
System.out.println("largest long value is " + largestLong + ".");
System.out.println("largest float value is " + largestFloat + ".");
System.out.println("largest double value is " + largestDouble + ".");
}
}


Sample Run

The largest byte value is 127.
The largest short value is 32767.
The largest integer value is 2147483647.
The largest long value is 9223372036854775807.
The largest float value is 3.4028235E38.
The largest double value is 1.7976931348623157E308.

Java Command Line Arguments:

This class demonstrates how command line arguments are passed in Java. Arguments are passed as a String array to the main method of a class. The first element (element 0) is the first argument passed not the name of the class.

Source Code

An example that prints in the command line arguments passed into the class when executed.

public class ReadArgs
{
public static final void main(String args[])
{
for (int i=0;i


Sample Run

With the following command line, the output shown is produced.

java ReadArgs zero one two three

Output:

The following command line arguments were passed:
arg[0]: zero
arg[1]: one
arg[2]: two
arg[3]: three
Java Arithmetic Operators:

The Java programming language has includes five simple arithmetic operators like are + (addition), - (subtraction), * (multiplication), / (division), and % (modulo). The following table summarizes the binary arithmetic operators in the Java programming language.

The relation operators in Java are: ==, !=, <, >, <=, and >=. The meanings of these operators are:
Use Returns true if
op1 + op2 op1 added to op2
op1 - op2 op2 subtracted from op1
op1 * op2 op1 multiplied with op2
op1 / op2 op1 divided by op2
op1 % op2 Computes the remainder of dividing op1 by op2

The following java program, ArithmeticProg , defines two integers and two double-precision floating-point numbers and uses the five arithmetic operators to perform different arithmetic operations. This program also uses + to concatenate strings. The arithmetic operations are shown in boldface.

public class ArithmeticProg {
public static void main(String[] args) {

//a few numbers
int i = 10;
int j = 20;
double x = 10.5;
double y = 20.5;

//adding numbers
System.out.println("Adding");
System.out.println(" i + j = " + (i + j));
System.out.println(" x + y = " + (x + y));

//subtracting numbers
System.out.println("Subtracting");
System.out.println(" i - j = " + (i - j));
System.out.println(" x - y = " + (x - y));

//multiplying numbers
System.out.println("Multiplying");
System.out.println(" i * j = " + (i * j));
System.out.println(" x * y = " + (x * y));

//dividing numbers
System.out.println("Dividing");
System.out.println(" i / j = " + (i / j));
System.out.println(" x / y = " + (x / y));

//computing the remainder resulting
//from dividing numbers
System.out.println("Modulus");
System.out.println(" i % j = " + (i % j));
System.out.println(" x % y = " + (x % y));

}
}

Java Assignment Operators:

It's very common to see statement like the following, where you're adding something to a variable. Java Variables are assigned, or given, values using one of the assignment operators. The variable are always on the left-hand side of the assignment operator and the value to be assigned is always on the right-hand side of the assignment operator. The assignment operator is evaluated from right to left, so a = b = c = 0; would assign 0 to c, then c to b then b to a.

i = i + 2;

Here we say that we are assigning i's value to the new value which is i+2.

A shortcut way to write assignments like this is to use the += operator. It's one operator symbol so don't put blanks between the + and =.
i += 2; // Same as "i = i + 2"

Here are some examples of assignments:

//assign 1 to
//variable a
int a = 1;

//assign the result
//of 2 + 2 to b
int b = 2 + 2;

//assign the literal
//"Hello" to str
String str = new String("Hello");

//assign b to a, then assign a
//to d; results in d, a, and b being equal
int d = a = b;