Jul 03

For Windows OS users

Before we begin with a java program , you need to have following softwares :

  • The Java SE Development Kit 6 (JDK 6) : If you do not have it ,then don’t worry .Download it from here .download
  • A text editor : Notepad is  sufficient .

Follow it upon installing jdk :

set PATH variable

  • Right-click on My Computer icon.
  • Select properties.
  • Choose Advanced option.
  • Select Environment variables option .
  • Click on New .
  • type PATH in variable name field.
  • type path to the (bin folder of jdk) in the variable value field.

for example

Suppose jdk1.6.0_<version> is installed in C:\program files\Java\

Then you would type the following in the variable value field:

C:\program files\Java\ jdk1.6.0_<version>\bin;

  • Finally click Ok.



Now, you are ready to do some programming in Java.

Please read it carefully .

All the Best ….

Follow the procedure :

1. Create a source  file (i.e <filename>.java)

  • Open Notepad.
  • Type the following code :

class Sample

{

public static void main(String[] args)

{
System.out.println(”This is my first program”);
}
}

  • Save the file as Sample.java in any directory.Let the directory be C:\Java\


Note:

* File name must be same as the name of the class which contains the main method .
* Since Java is case sensitive ,please be careful about the Lowercase and Uppercase letters.


2.Compiling the Source file (i.e <filename>.java)

  • Click on start
  • Press run
  • Type cmd
  • press enter key
  • Now,go to the directory where you have saved java file.

In above example ,we saved Sample.java in C:\Java\

So,we have

C:\Java>

  • Now type the following command to compile the program:

javac  <filename>.java

Here, javac is java compiler that translates source file into a class file which contains the bytecode that the Java Virtual Machine (JVM) can understand.


i.e javac Sample.java for above example.

So,overall it looks like :


C:\Java>javac Sample.java  

  • press enter key
  • Hence compilation is complete.

Now , what happens upon compilation ??

  • After compilation ,open the directory where you have saved java file.
  • Now,you will see a file Sample.class along with the Sample.java file.
  • This class file contains the bytecodes corresponding to source file.

3.Run the program

  • To run Sample.java , we have the following command :

java Sample

Here, java is java interpreter.

  • On command prompt , we have :

C:\Java>java Sample

  • It produces the following output :

This is my first program


Program Explanation

class Sample

{

public static void main(String[] args)

{
System.out.println(”This is my first program”);
}
}


Class Definition

  • The first line

class Sample


declares a class which is an object oriented construct.


  • Everything must be within a class.

  • class is a keyword.

  • Sample is a Java identifier that specifies the name of the class to be defined.

Braces  { }

  • Every class definition in Java begins with an opening brace “{” and ends with a matching closing brace “}” .

The main Line

  • The third line

public static void main (String []  args)

defines a method named main.

  • This is starting point for java interpreter to begin the execution of the program .
  • A java program can have any number of classes ,but  only one class must include a main method to initiate the execution.
  • Above line contains a number of keywords :

public : It is one of the access modifier. Here ,in the main Line ,it allows JVM to access the main method. It should always be present in the main Line.

static : The main method must be always be declared as static since the interpreter uses this method before any objects are created to invoke the main method.

void : The type modifier void states that the main method does not return any value.

The Output Line

  • The only executable statement in the program is

System.out.println(”This is my first program”);

This is similar to printf() statement of C .

or  cout<< construct of C++.


  • The method println always appends a newline character to the end of the string.
  • This means that any subsequent output will start on a new line.

written by Anup \\ tags: