Choose Language

Thursday 8 November 2012

4. Creating a Simple Java Program

 
This post features Compiling and Executing a Sample Java Program. Before that, Lets look on to the sample java code.

You can use any Text Editors such as notepad, textpad, ultraedit, Our recommendation is to use notepad if you are a windows based user. For testing purpose, you enter the following code in your notepad and save it with a filename (any name) 

For example, example.java.
Note: Extension is quite important if you simply save it as with a filename without the

extension . java, then it would save to its default extension .txt which wont let your code to run by your java compiler. Hence  use file name .Java.

 Class example  
 {   
 public static void main(String args[]) {   
     System.out.println("Welcome to Java Tutorial in Rand Java");   // printing the String to command window
 }   
 }  

Open your command prompt, By going to run type cmd, then press OK.
Now go to the location where you saved your file.

For example:


               E:\JAVA\LEVEL 2\DAY (4)>c:  
               C:\Users\Administrator>cd/d E:\JAVA\LEVEL 2\DAY (4)  
               E:\JAVA\LEVEL 2\DAY(4)>   
  where the above mentioned "E:\JAVA\LEVEL 2\DAY (4)" is the location where I saved my .java file.


For Compiling:
                                          Javac filename.java


For Execution:              Java Classname 

so you will be running as without quotes "java example" 

Output:




Inside the Code:

// is used to make comments inside the code
// example is the user-defined class name, Note that all our code should rest within the class 
// Note that user defined classes will start with the Upper Case Letter.
// public is an access specifier which enables your main method visibility.
// static is a keyword used to execute before creating an instance of a class(i.e. , objects)
// Void is a return type which return null
// Main is pre-defined method
// String is a predifined class, args[] possess the user's activities inside the main method.
// String It is used for parsing inputs via comment line arguments
// System is  a pre-defined Class.
// out is a pre-defined variable.
// System.out is a console output
// println is a pre-defined method used to print the output.in the console (i.e, command line)
// Compile with javac example.java
// Execute with java example


0 comments:

Post a Comment

Trending Now. . .

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by RANDJITH | Bloggerized by Randjith - Subscribe to Learn in Tamil | coupon codes