First Java Program

August 6th, 2009

class Example
{
public static void main(String args[])
{
System.Out.println(”This is a simple java program”);
}
}

Output:
This is a simple java program

Description:
Let us explain the basic of a simple java program and how to write , compile and execute it.

For most of the computer Languages the name of the file is immaterial. But in case of java the name we give to a source file is very important. In our sample program it should be Example.java.


Now many of the users will ask why? Let us explain

The name of the class defined by the program is also Example. This is not a coincidence. In Java , all code must reside inside the class. By Convention the name of that class should match the name of the file that holds the Source code

We compile the file as

C:\>javac Example.java

The javac compiler creates a file called Example.class that contain the bytecode version of the program. Bytecode is the intermediate representation of  program. and it will be executed by JVM (Java Virtual Machine).

Now after generating the byte code we execute it to generate the output.

Now for this we need the java application launcher , called java.We write

C:\java Example

It will generate the desired output:  This is a simple java program

Features of Java

August 6th, 2009

 Compiled and interpreted : Usually a computer language is either a compiled and interpreted language. But java combines these two approaches and thus make java a two stage system. The java compiler translates the source code into what is known as bytecode  instructions. In the second stage system, java interpreter generates the machine code that can be directly executed by the machine that is running the java program.

Platform independent and portable : the most striking feature of the language is that it is platform-neutral. Java is the first programming language that is not tied with any hardware or operating system.

Object-oriented : Java is purely object-oriented language. Almost everything in java is an object. All program code and data resides inside the objects and classes.

 

Robust and secure : Java is a robust language. It has strict compile time and run time checking for data types. It incorporates the concept of exception handling which captures the serious errors  and eliminates the risk of crashing the system.With the absence of pointers, java ensures that program cannot gain access to memory location without proper authorization.

Distributed : Java is designed as distributed language for creating applications on networks. It has the ability to share both data and programs.

Small,simple and familier : Java is small and simple language. Many features of c and c++ that are either redundant or sources of unreliable code are not part of java. For exam : java does not use pointers ,preprocessor header files, goto statement, multible inheritance, operator overloading etc.

Multithreaded and interactive : Multithreaded means handling multiple tasks simultaneously. Java supports multithreaded programs. This means we need not wait for the application to finish one task before beginning another. Exam : we can listen to an audio clip and at the same time downloading an applet from a distant computer.


High performance : Java performance is impressive for an interpreted language mainly due to the use of intermediate code java artitecture is also designed to reduce overheads during runtime.

Dynamic and extensible : Java is a dynamic language. Java is capable of dynamically linking into new class  libraries, methods and objects

Java Development Tools

August 6th, 2009

The JDK is a development environment for building applications, applets, and components using the Java programming language.

Tools and utilities that will help one to develop,execute, debg, and document programs written in Java programming language are provided in the bin subdirectory.

Let us state some of the development tools.

Tool  Description
javac  The compiler for the Java programming language
java  The launcher for java applications. In this release, a single
 launcher is used for both development and deployment
javadoc  API documentation generator
appletviewer  Run and debug applets without a web browser
jar  Manage java archive (JAR) files
jdb  The java Debugger
javah  C header and stub generator. Used to write native methods
javap  Class file disassembler
extcheck  Utility to detect JAR conflicts

Running a simple java program in command prompt environment

August 6th, 2009

 

First we have to write the code of the program. For this purpose we have to use a text editor say a notepad or we can use any text editor.

We have to save this notepad file and this should be of extension .java. Say we save the file as abc.java.

Then during second step we have to compile this .java file. We execute the command as follows:

 C:\javaprograms> javac abc.java

If there are any errors in our program, they will be displayed on the screen.

If the compilation is succcessful, a file abc.class will be created.

 

 During third step we run our program by executing the command java as

C:\javaprograms> java abc

It will display the output of the program written.

arr  Tip : While compiling we have to give full file name including extension like  javac abc.java. While running we have to provide only class name like java abc , no need to mention the extension.

Tips for working in command prompt environment

August 6th, 2009

Let us know how to work in a command prompt environment to run java programs:

Please note that for convenience we have to keep all our programs in aparticular directory. It is a nice idea to give it a userdefined name say “javaprograms”.



In windows there is an environment variable “PATH”. We can set it by using the set command. When we issue any command like javac, windows searches it in the path. If our javac program is in the directory specified by the path, then we can simply invoke it.

Once you enter the DOS command prompt, you should change the directory to “javaprograms” and start working.

Create a batch file named “cmdjava.bat” as

set PATH=%path%;C:\JDK6\bin;C:\javaprograms
set classpath=.;C:\javaprograms;
cd C:\javaprograms
command

When we run this batch file, it sets the PATH for us, and also sets classpath and takes us to our normal working folder.

Learn Java Free::Free Java Tutorials

August 1st, 2009

Hi Guys,

We are introducing a new java section.You can learn about java,find list of all java programs,sample java programs for free.

Please feel free to submit your programs and queries on this blog.

Thanks
admin