Pages

Ads 468x60px

For New Update Use this Link http://dotnethubs.blogspot.in/ Offer for you

.

Wednesday 10 April 2013

Public static void main String arg means?

Public static void main String arg means?

  Catergoies-: how to install and configure the tomcat server

                      How to reset IIS with the command Prompt

Introduction:

public static void main(String args[])
{
      
}

Public-:  
   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.In this case, main( ) must be declared as public, since it must be called by code outside of its class when the program is started.
Static -:  
   The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made.
Void -:     
  The keyword void simply tells the compiler that main( ) does not return a value. As you will see, methods may also return values.

main( )-:  
 As stated, main( ) is the method called when a Java application begins. Keep in mind that Java is case-sensitive. Thus, Main is different from main. It is important to understand that the Java compiler will compile classes that do not contain a main( ) method. But the Java interpreter has no way to run these classes. So, if you had typed Main instead of main, the compiler would still compile your program. However, the Java interpreter would report an error because it would be unable to find the main( ) method.

String args[]-:
String args[ ] declares a parameter named args, which is an array of instances of the class String. Objects of type String store character strings.

args-: 
'args' stands for arguments. These are the arguments sent to the main method. Usually they are command line arguments.
For an example of how they are used, we will assume your program is called HelloWorld.

So when you want to run the program you would normally type:
java HelloWorld
and the program runs and displays the string "Hello World" to the console.

Now, you could set it up so the person who runs the program can include their name. You could use a command line argument to have the name come into the program, like so.
java HelloWorld Mark
Then your program will display this to the console.
Hello World Mark

So you would have something like this in your program.
public static void main(String[] args) {

System.out.print("Hello World");

if (args.length > 0) { // Checks if any arguments are present.
System.out.print(" " + args[0]);
}
}

Since args is an array, the first item in the array will be a string of "Mark". You access the first item with the number 0.

Now, for a slightly harder example. Just say you wanted the program to display "Hello World" if the user did not enter a name, and "Hello <the name>" is someone entered the name. You could do something like this.

public static void main(String[] args) {

System.out.print("Hello ");
if (args.length > 0) {
System.out.println(args[0]);
} else {
System.out.println("World");
}
}

No comments:

Post a Comment

 

..




New Updates

Related Posts Plugin for WordPress, Blogger...

Related Result