OOP CS3391 UNIT-1 (Lecture Note-9)
OOP CS3391 UNIT-1 (Lecture Note-9)
STEPS:
Set the path of the compiler as follows (type this in command prompt):
Set path=”C:\Program Files\Java\jdk1.6.0_20\bin”;
To create a Java program, ensure that the name of the class in the file is
the same as the class name of the file.
Save the file with the extension .java (Example: HelloWorld.java)
To compile the java program use the command javac as follows:
javac HelloWorld.java
This will take the source code in the file HelloWorld.java and create the
javabytecode in a file HelloWorld.class
To run the compiled program use the command java as follows:
java HelloWorld
(Note that you do not use any file extension in this command.)
Example 1: A First Java Program:
class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
Save: HelloWorld.java
Compile: javac HelloWorld.java
Run: java HelloWorld
Output:
Hello World
Example 2: A Second Java Program to double the number:
import java.util.Scanner; // Scanner is a class which contains necessary methods to provide a user an
access to the i/p console.
public class Example
{
public static void main(String args[])
{
int num=0,res;
Scanner in=new Scanner(System.in); //creating object of Scanner class to access the i/p stream.
System.out.println("Enter a Number :");
num=in.nextInt();
res=num*2;
System.out.println("The double of given number is "+res);
}
}