Thursday 10 November 2016

How to write, compile and Run First Program





Lets start from building a classical"Hello World" program, which is generally used as the first program for learning nay new language.


Hello World Program: 

  • Open notepad.
  • Write the following code into it. 

            public class HelloWorld{
                                public static void main(String[] args){

                                          System.out.println("Hello World!");
                   }

            }

  • To save your program, move to File menu and select save as option.
  • Save your program as "HelloWorld.java" in some directory. 
  • Name of the file must be same as name of the public class in the file. As java is case-sensitive, so if   your class name is HelloWorld, than file name must be HelloWorld. Otherwise the Java compiler will refuse to compiler the program. 
  • I saved my program in C:\Java\jdk\bin
  • Now open command prompt and type cd File Location i.e cd C:\Java\jdk\bin.
  • To compile java program type javac HelloWorld.java and press Enter
  • Now type java HelloWorld to run your program.
  • You can see the HelloWorld! would be displayed on console. Hurrah! You are successful in writing, compiling and executing your first program in java.


 A Closer Look at the First Sample Program:

  // Hello World Program

  • This is a comment. Single-line comment start with two forward slashes(//). Like most other programming languages, Java lets you enter a remark into a program's source file. The contents of a comment are ignored by the compiler.

public class Hello World{ 
  • This line uses the keyword class to declare that a new class is being defined. HelloWorld  is an identifier that is the name of  class. 
  • The entire class definition, including all of  its members, will be between the opening curly brace ({) and the closing curly brace ({). 


public static void main(String args[]) {
  • All Java applications begins execution by calling main()
  • The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When  a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. 
  • main()  must be declared as public, since  it must be called by code outside of  its class when  the program is started. 
  • The keyword static allows main() to be called without having  to instantiate a particular instance of the class. This is necessary because main is called by the Java interpreter before any objects are made. 
  • The keyword void simply tells the compiler that main() doesn't return any value.
  • The main() is the method called when program begins. 
  • Any information that you need to pass to a method is received by variables specified within the set of parentheses. These variables are called parameter. In main() there is only one parameter, String args[] declare a parameter name args, which is an array of  instances of class Stringargs receives any command-line arguments presents when program executed.
  • The last character you see is the {. This is the sign of start of the method in our example start of the main()'s body. All of the code will occur should be written between the method's  starting and  ending braces. 
 System.out.println("Hello World!");
  • This string outputs the string "Hello World!" , followed by a new line on the console. 
  • Output is accomplished by the built-in function println() method.
  • Here System is a predefined class that provides access to the system, and out is the output stream that is connected to the console. 
  • At the end of the statement  you see a semi-colon(;). All  java statement in Java end with a semi-colon.
      }

   }
  • Ending braces- the first } in the program ends main() and the last } ends the HelloWorld class definition.
         

No comments:

Post a Comment

If you have any doubt please let me know

Methods in JAVA Language

 In general, a way may be thanks to perform some task. Similarly, the tactic in Java may be a collection of instructions that performs a sel...