Sunday, June 14, 2009

Your first Java application

Creating a Source File HelloWorld

• Any text editor can be used to create a Source file
– E.g. Notepad,word pad
• Type the following on your text editor

class HelloWorld
{
public static void main (String[] arguments)
{
System.out.println (“Hello World!!!”);
}
}

• Save the file as “HelloWorld.java” in a convenient folder (E.g.
“C:\MyJava”).
• Source Files should be saved with a .java extension. The Java
compiler will only handle source files with this file extension.

Compiling your Source File

• I assume that you have saved your source file as
“C:\MyJava\HelloWorld.java”, and you have correctly set the PATH variable.
• In the command prompt and set “C:\MyJava” as the current directory.
– You can do this by typing: cd C:\MyJava on the command prompt.
• Type: javac HelloWorld.java on the command prompt.
• If you have followed the steps correctly and all is well, you should see no
output. If not, you might see some error messages. If so, go back and recheck
your work.
• As we mentioned, javac.exe is a compiler, which converts java source code
into Java bytecode. Here, it complies the java source code in
“HelloWorld.java” and saves the resulting java bytecode as
“HelloWorld.class” in the same directory (E.g.
“C:\MyJava\HelloWorld.class”).

Executing your Java Bytecode File

• Type: java HelloWorld on the command prompt.
• You should see “HelloWorld!!!” displayed.
• As we mentioned, java.exe is an interpreter, that plays the role of the Java Virtual Machine converting the java bytecode into the machine code of the host computer and executing it.
Here, it takes the java bytecode in “HelloWorld.class” and interprets it.

What is Programming?

Programming:-

• A programming language is an artificial language that can be used to control the behavior of a computer.
• Source code is any series of statements written in a programming language.
• Computer programming (often shortened to programming or coding) is the process of writing, testing, and maintaining the source code of computer programs.

Computer Programs

• Source code cannot be directly understood by a computer, and hence must be converted into a
computer-executable form before it can run.
• The source code is either converted into an executable file (compilation), or executed on the
fly (interpretation).
• The term computer program may refer to source code, written in a programming language, or to the executable form of this code.

Compilers and Interpreters

• Computers cannot directly understand Source code. Computers understand their own language known as Machine Language or Machine Code. Different computer platforms have different Machine Languages. – For example, each of Microsoft Windows on Intel Pentium
Architecture and Mac OS X on Apple Macintosh Architecture would not “understand” the others machine code.
• Hence, a programming language’s source code needs to be converted into Machine Code before it can be used to control the behaviour of a computer.
• This conversion is done either by a compiler or an interpreter.

Compilers

• Compiler is a computer program that converts programmer friendly symbols in to machine friendly instructions.
• Compilers are often used to convert source code into machine code.
• Compilers convert the entire source code into machine code at one go. The machine code is executed only once all the source code has been converted.
• Since, different computer platforms have different machine languages, source code compiled into machine code for one architecture might not run on another.

Interpreters

• Interpreters differ from Compilers in that they never convert the whole source code
at one go.
• An interpreter usually converts one source code statement, executes the resulting machine code and then converts the next source code statement and so on.

Steps in writing a program

• There are four general phases during the development of any computer program

– Specify the problem
– Analyze and break down into a series of steps towards solution
– Write the code
– Compile, test and run the program


An introduction to Java

This free tutorial series teaches the basics of Java programming. No previous Java experience is required, and its perfect for the beginner. Tutorials cover a wide range of topics, from applets and applications, to more advanced issues such as class design, event handling and networking. For best results, you should read tutorials in sequence - but feel free to skip over a few lessons if you've already been learning Java from a book or elsewhere.