public static void main is discussed in Simple terms for a very Beginner.
In Java, the execution starts from main() method. But for compilation, main() is not required. Java's main() method syntax is quiet different from C/C++.
Following is the public static void main complete signature
Every word in the public static void main statement has got a meaning to the JVM.
1. public: It is a keyword and denotes that any other class (JVM) can call the main() method without any restrictions.
2. static: It is a keyword and denotes that any other class (JVM) can call the main() method without the help of an object. More about static is available at static Keyword – Philosophy.
3. void: It is a keyword and denotes that the main() method does not return a value.
4. main(): It is the name of the method.
5. String args[]: The parameter is a String array by name args. The string array is used to access command-line arguments.
The Java program under execution takes some context area (execution area) in the RAM. JVM is in another context area. To call the main(), the JVM requires an object. When the JVM has not entered into the program, how it can create an object or get an object. To overcome this, allow the JVM to access the main() without object just by declaring static. Once the JVM enters, it can create hundreds of objects later.
Whether you pass right now command-line arguments or not, you must have the string array as parameter as it is the part of syntax.
Any word is missed in the above statement, the program compiles, but does not execute.
A program using main() is discussed in "Basic Class Structure, Compilation and Execution".
1. public static void main(String args[]) throws IOException: This sort of main() method is used in "BufferedInputStream and BufferedOutputStream" and in many places.
2. public static void main(String args[]) throws Exception: This type of main() method is used in "Communication with TCP/IP Protocol" and also in many places.
Other interested topics, you definitely like.
1. Java Naming Conventions – Readability
2. System.out.println
3. Java Drawbacks
4. Strongly typed language
5. Data Types Default Values – No Garbage
Helpful Post.
hi I AM VISHAL KUMAR…I WANT TO ask a question..???
why string arguement is used in public static void main..???
To access command-line arguments.
Respected Sir,
I m aashish & i am gujarati from Gujarat
I read so many books & learnt from other senior teachers also, but still I can’t understand the meaning of
“public static void main()”. pl. can you give me explain about in Hindi or Gujarati Lang.
Thanks
I know Hindi but cannot type Hindi.
Hi Aashish Shah ,here’s your answer
public static void main(String args[])
PUBLIC:we Used public access specifier here because public access specifier helps the JVM(Java virtual machine) to easily access the main method.
STATIC:Static are used here because of main() function is the first function which called without using of Class Object(So, to remove this thing static helps to say JVM at the runtime to use the CLASSNAME.MAIN() to call the main function)(Note: May u know about of it in JAVA language we can’t call any method without class Object. So “static” helps to call function using of Class name at the time of Run time.)
VOID: void is a return type (but it returns nothing) its depend on you what type of data you want from your function. we can also use int,float,double datatype by replacing VOID.
STRING ARGS[] : Its used for command line argument.(Take input at runtime)
May be these explanation helps you to understand about of public static void main(String args[])
Seriously you have written too good content in a such an easy language. The information is so useful and easy to understand.
Can you write something on like how the compiler and the bytecode of Java works?
LIke from the starting
1) u write a java program
2) then javac ………….
3) what exactly happens next at system level
hope if you could write i really wanted to know ….
Fabulous, աhat a webpage it іs! Thiѕ blog ρrovides
useful data tօ us, keep it up.
In c, main() fun. having 3 parameters, but in java only 1, how it manage??
Java needs only one and this is how Java was developed.
sir why an interface cannot implement another interface?
Because interface cannot give body for methods.
why is main has a return type of void only and why it only pass String as argument
In the programming either in C/C++ and Java also, main() method return type is not used anywhere in coding. For this reason main() has a return type of void and more over main() is a callback method. Regarding String array arguments, it is used in command-line arguments. Read this link.
http://way2java.com/arrays/command-line-arguments/
why we use ” public static void main(String args[])” what is its purpose and what is the function of each
This is already explained.
http://way2java.com/oops-concepts/public-static-void-mainstring-args/
Nice one.
Public: It is not a keyword. It is an Access Identifier
It is public and not Public. All keywords are lower case letters. public is a keyword used as an access specifier.
Sir, i have one question not related to above thread.
Can we insert duplicate keys in a hashmap ?
We can insert it but by doing so, previous data gets overrided.
example –
Map m = new HashMap();
m.put(“a”,”apple_one”);
m.put(“a”,”apple_two”);
m.put(“a”,”apple_three”);
System.out.println(m.entrySet());
It will print only [a=apple_three]
But i want to print like below
[a=apple_one,a=apple_two,a=apple_three]
Is this possible ? how ?
Not possible in Hashtable and also in HashMap. They are designed to accept no duplicate keys. Anyhow, can you show me one real time example for your code? Then I can suggest another solution.
what is the reason that main method can’t take int data type?
The main() method in Java is not overloaded (in C# it is overloaded). Why you require int parameter to main()? What you want to achieve? Let me know, I will get you the solution, if possible.
If you write int main (and you got predefined void main also in the code), it is not allowed as it does not come under method overloading. If you write with your own main returning int and do not contain predefined, it becomes your own user defined method. JVM will not call your mehthod.
Java proved it is not required to exist a language (like multiple inheritance is not required).