Monday, 17 September 2012

About JAVA

What is Java...
Java is a computer programming language. It enables programmers to write computer instructions using English based commands, instead of having to write in numeric codes. It’s known as a “high-level” language because it can be read and written easily by humans. Like English, Java has a set of rules that determine how the instructions are written. These rules are known as its “syntax”. Once a program has been written, the high-level instructions are translated into numeric codes that computers can understand and execute.

Who created Java
In the early nineties, Java was created by a team led by James Gosling for Sun Microsystems. It was originally designed for use on digital mobile devices, such as cell phones. However, when Java 1.0 was released to the public in 1996, its main focus had shifted to use on the Internet. It provided more interactivity with users by giving developers a way to produce animated webpages . Over the years it has evolved as a successful language for use both on and off the Internet. A decade later, it’s still an extremely popular language with over 6.5million developers worldwide.

Why choose Java
Java was designed with a few key principles in mind:
1.Easy to Use: The fundamentals of Java came from a programming language called c++. Although a powerful language, it was felt to be too complex in its syntax, and inadequate for all of Java's requirements. Java built on, and improved the ideas of c++, to provide a programming language that was powerful and simple to use. 
2.Reliability: Java needed to reduce the likelihood of fatal errors from programmer mistakes. With this in mind, object-oriented programming was introduced. Once data and its manipulation were packaged together in one place, it increased Java’s robustness.  
3.Secure: As Java was originally targeting mobile devices that would be exchanging data over networks, it was built to include a high level of security. Java is probably the most secure programming language to date.
4.Platform Independent: Programs needed to work regardless of the machine they were being executed on. Java was written to be a portable language that doesn't care about the operating system or the hardware of the computer. 

Where Do I Start?

To start programming in Java, all you need to do is download and install the Java development Kit.

Great Things about Java

Java is an operating system independent platform for software development. It consists of a programming language, utility programs and a run time environment. A Java program can be developed on one computer and run on any other computer with the correct run time environment. In general, older Java programs can run on newer run time environments. Java is rich enough that even very complicated applications can be written without operating system dependencies. This is called 100% java.
                    With the development of the Internet, Java has gained in popularity because when you program for the Web, you have no way of knowing which system the user may be on. With the Java programming language, you can take advantage of the "write once, run anywhere" paradigm. This means that when you compile your Java program, you don't generate instructions for one specific platform. Instead, you generate Java byte code, that is, instructions for the Java Virtual Machine (Java VM). For the users, it doesn’t matter what platform they use--Windows, Linux/Unix, MacOS, or an Internet browser—as long as it has the Java VM, it understands those byte codes.

Three Types of Java Programs

- An "applet" is a Java program designed to be embedded on a web page.
- A "servlet" is a Java program designed to be run on a server.
In these two cases the Java program cannot be run without the services of either a Web browser for an applet or a Web server for a servlet. 
- A "Java application" is a Java program that can be run by itself.
The following instructions are for you to program a Java application on a computer that runs Linux. 

A Checklist 
Very simple, you need only two items to write a Java program:
(1) The Java 2 Platform, Standard Edition (J2SE), formerly known as the Java Development Kit (JDK). 
Download the Linux version. Make sure you download the SDK, not the JRE (the JRE is included in the SDK/J2SE).
(2) A text editor
Almost any editor you find on Linux will do (e.g., Vi, Emacs, Pico). We’ll use Pico as an example.

Creating Your First Java Application

Your first application, FatCalories, will wait for you to enter two numbers and then compute and print out the result. To create this program, you will: (1) create a Java source file, (2) compile the source file into a byte code file, and (3) run the program contained in the byte code file. See specific instructions below: 
Step 1. Create a Java Source File.
A source file contains text written in the Java programming language. You can use any text editor to create and edit source files.
You have two options:
* You can save the file onto your computer. This way can save you some typing. Then, you can go straight to step 2.
* Or, you can follow the longer instructions:
(1) Bring up a shell (sometimes called terminal) window. 
When the prompt first comes up, your current directory will usually be your home directory. You can change your current directory to your home directory at any time by typing cd at the prompt (typically a “%”) and then pressing Return.
The Java files you create should be kept in a separate directory. You can create a directory by using the command mkdir. For example, to create the directory java in your home directory, you would first change your current directory to your home directory by entering the following command:
% cd 
Then, you would enter the following command:
% mkdir java 
To change your current directory to this new directory, you would then enter:% cd java 
Now you can start creating your source file.
(2) Start the Pico editor by typing pico at the prompt and pressing Return. If the system responds with the message pico: command not found, then Pico is most likely unavailable. Consult your system administrator for more information, or use another editor.
When you start Pico, it'll display a new, blank buffer. This is the area in which you will type your code.
(3) Type the code for listed here into the blank buffer. Type everything exactly as shown. The Java compiler and interpreter are case-sensitive.
(4) Save the code by typing Ctrl-O. When you see File Name to write: ,, preceded by the directory in which you want the file to go. If you wish to save in the directory /home/smith/java, then you would type 
/home/smith/java/ and press Return.
Use Ctrl-X to exit Pico.
Step 2. Compile the Source File.
The Java compiler, javac, takes your source file and translates its text into instructions that the Java Virtual Machine (Java VM) can understand. The compiler puts these instructions into a byte code file.
Now, bring up another shell window. To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is /home/smith/java, you would type the following command at the prompt and press Return:
% cd /home/smith/java 
If you enter pwd at the prompt, you should see the current directory, which in this example has been changed to /home/smith/java.
If you enter ls at the prompt, you should see your file:
Now you can compile. At the prompt, type the following command and press Return: javac 

If you see this error message: 
javac: Command not found
then Linux cannot find the Java compiler, javac.
Here's one way to tell Linux where to find javac. Suppose you installed the Java 2 Platform (J2SE) in /usr/java/jdk1.4. At the prompt, type the following command and press Return: 
The compiler now has generated a Java byte code file: FatCalories.class. 
At the prompt, type ls to verify the new file is there.

Step 3. Run the Program

The Java VM is implemented by a Java interpreter called java. This interpreter takes your byte code file and carries out the instructions by translating them into instructions that your computer can understand.
In the same directory, enter at the prompt:
java FatCalories 
When you run the program you need to enter two numbers when the black command line window appears. The program should then write out those two numbers plus the percentage computed by the program. 

When you receive the error message: 
Exception in thread "main" java.lang.NoClassDefFoundError: FatCalories
It means: java cannot find your byte code file, FatCalories.class.
What to do: One of the places java tries to find your byte code file is your current directory. For example, if your byte code file is in /home/smith/java, you should change your current directory to that by typing the following command at the prompt and hit Return:
cd /home/smith/java
If you enter pwd at the prompt, you should see /home/smith/java. If you enter ls at the prompt, you should see your and FatCalories.class files. Now enter java FatCalories again.
If you still have problems, you might have to change your CLASSPATH variable. To see if this is necessary, try "unsetting" the classpath with the following command: 
Now enter java FatCalories again. If the program works now, you'll have to change your CLASSPATH variable. 

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...