Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Main Method

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,
can anyone please tell me what is difference between
1.public static void main ();
2.public static void main(String args []);
3.public static void main (String... args);
i know its kind of noob question but i am little confused with it
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jaspreet manchinda wrote:Hello Everyone,
can anyone please tell me what is difference between
1.public static void main ();
2.public static void main(String args []);
3.public static void main (String... args);
i know its kind of noob question but i am little confused with it


1. A public, static and void method named main which takes no arguments.
2. A public, static and void method named main which takes argument as array of String. This method also acts as entry point of program (if main class is proper et-cetera)
3. Same as 2. Only syntax is different. Instead of String array, here there's usage of var-args.

I hope this helps.
 
jaspreet manchinda
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quote:

Same as 2. Only syntax is different. Instead of String array, here there's usage of var-args.


thank you for your reply but when to use main without argument and when to use wit var-args
 
Ranch Hand
Posts: 165
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jaspreet manchinda wrote:Quote:

Same as 2. Only syntax is different. Instead of String array, here there's usage of var-args.


thank you for your reply but when to use main without argument and when to use wit var-args



Your java program execution always begin with standard main method with standard arguments i.e. public static void main(String[] args){}.
But you can at any no. of times overload main method in your same program as per your requirements.

I hope it is clear to you now.
 
jaspreet manchinda
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah!! its much more clear now thanks for your replies guys
 
Greenhorn
Posts: 22
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jaspreet manchinda wrote:Hello Everyone,
can anyone please tell me what is difference between
1.public static void main ();
2.public static void main(String args []);
3.public static void main (String... args);
i know its kind of noob question but i am little confused with it




1. Is a normal method which is being used in the program and is not the point of entry of execution of the program, having this method. Its just a static method.
2. This is the method which is the point of entry for execution of a program. Execution will start from here.
3. String args[] holds the same meaning as String ... args, where the later is called var-ags which takes or holds multiple values of type String. All that is said in '2', applies here too.
 
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


At compile time this code will work fine

But at run time you will get an Exception.

Remember Exceptions come during Runtime
 
Vinod Vijay
Ranch Hand
Posts: 165
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:

At compile time this code will work fine

But at run time you will get an Exception.

Remember Exceptions come during Runtime



Ofcourse, it will compile because no syntax or Java rule violations. But will not run because the interpretor could not locate standard main().
 
Vishal Hegde
Ranch Hand
Posts: 1087
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinod Vijay wrote:

Vishal Hegde wrote:

At compile time this code will work fine

But at run time you will get an Exception.

Remember Exceptions come during Runtime



Ofcourse, it will compile because no syntax or Java rule violations. But will not run because the interpretor could not locate standard main().



Thats what I was conveying
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:


Note that the above is no different than

The larger point here is that main() is no different from any other method. You can overload it, you have have it or even not have it. It is all valid java. The only time it matters is when you want to START your program. The JVM looks for the main method with the signature of "public static void main(String [] args)"
 
Vinod Vijay
Ranch Hand
Posts: 165
Tomcat Server Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Hegde wrote:

Vinod Vijay wrote:

Vishal Hegde wrote:

At compile time this code will work fine

But at run time you will get an Exception.

Remember Exceptions come during Runtime



Ofcourse, it will compile because no syntax or Java rule violations. But will not run because the interpretor could not locate standard main().



Thats what I was conveying



I know, just added my words to that
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic