Is void main() OK in this case?

  • Thread starter Chuck Heathfield
  • Start date
C

Chuck Heathfield

Hello,

Basic question.

public static void main(String args[])

is fine, right? I know it's common, but is it correct? I don't
need 'int' before the 'main', right?


Thanks,
Chuck Heathfield
 
J

Joshua Cranmer

Chuck said:
Basic question.

public static void main(String args[])

is fine, right? I know it's common, but is it correct? I don't
need 'int' before the 'main', right?

public static void main(String[] args)
or the varargs variant:
public static void main(String... args)

are the methods that the Java VM will attempt to invoke when passing in
the class name to java. Any other signature (differing return value,
differing name in case, lack of static, lack of public access, incorrect
argument type and/or count) will not be recognized by the VM.

Naturally, the method must be enclosed in a class.

Finally, why is your followup-to set to c.l.c, which ain't even in your
newsgroup list?
 
L

Lew

For many basic questions about Java programming, basic answers can be found in:
public static void main(String args[])
is fine, right? I know it's common, but is it correct? I don't
need 'int' before the 'main', right?

Joshua said:
public static void main(String[] args)

As Joshua illustrates here, it is idiomatic in Java to place the square
brackets after the type ('String') rather than the variable ('args') in a
declaration, except when there are specific reasons to do the other.
or the varargs variant:
public static void main(String... args)

Quoted nearly verbatim from the JLS. Good show.
are the methods that the Java VM will attempt to invoke when passing in
the class name to java. Any other signature (differing return value,
differing name in case, lack of static, lack of public access, incorrect
argument type and/or count) will not be recognized by the VM.

Naturally, the method must be enclosed in a class.

For answers to this kind of question, go right to the defining specification:
Finally, why is your followup-to set to c.l.c, which ain't even in your
newsgroup list?

I never even saw the original post until the response quoted it.
 
M

Mike Schilling

Lew said:
As Joshua illustrates here, it is idiomatic in Java to place the
square brackets after the type ('String') rather than the variable
('args') in a declaration, except when there are specific reasons to
do the other.

What reasons might those be? AFAIK, it's always best to put the
square brackets after the type.
 
L

Lew

Mike said:
What reasons might those be? AFAIK, it's always best to put the
square brackets after the type.

I've seen it done with "multi-dimensional" arrays where the first /n/
dimensions are specified:

int matrix [] [] = new int [5] [5];

I make sense of this by thinking that the dimensionality inheres to the
instance, pointed to my 'matrix', as a specific implementation, not a type.

It's also useful for declaring multiple related items in one line:

int scalar, matrix [] [];

That assumes you don't excoriate multiple declarations per line.


Since Java allows both positions, the reasons to choose one are convention,
clarity and documentation of intent. To my mind, putting the brackets with
the type:
int [] [] matrix;
documents the type, the other way documents the item. Normally I intend
source to be read in terms of types, so I'll favor bracketing the type, but
sometimes the sense and purpose of the code seems clearer the other way.
 
D

Daniel Pitts

Chuck said:
Hello,

Basic question.

public static void main(String args[])

is fine, right? I know it's common, but is it correct? I don't
need 'int' before the 'main', right?


Thanks,
Chuck Heathfield

So, I wrote this reply before realizing the f-u was set to comp.lang.c.
It seems like this may have been an attempt to troll. However, giving
the benefit of the doubt, here is the answer:

In Java, it must always be
public static void main (String[] args) // Pre 1.4 preferred
or
public static void main (String...args) // 1.5 or later preferred
or
public static void main (String args[]) // Never preferred, but valid


You might be confusing Java main with C/C++ main, where void is not
valid (but sometimes works anyway).

In C/C++ the correct signature would always be:

int main(int argc, char**argv);
 
M

Mark Space

Mike said:
What reasons might those be? AFAIK, it's always best to put the
square brackets after the type.


In my opinion, it's because the type of the (array) object is String[],
so it's done for consistency and clarity.

int[] ia = {1, 2, 3, 4};
List<int[]> ial = Arrays.asList( ia );

Class<?> type = int[].class;


Works the same as it does for other class objects.
 
K

Keith Thompson

Daniel Pitts said:
Chuck said:
Hello,
Basic question.
public static void main(String args[]) is fine, right? I know it's
common, but is it correct? I don't
need 'int' before the 'main', right? Thanks,
Chuck Heathfield

So, I wrote this reply before realizing the f-u was set to
comp.lang.c. It seems like this may have been an attempt to troll.
However, giving the benefit of the doubt, here is the answer:
[...]

No benefit of the doubt is warranted. This is part of a pattern of
abusive messages. If you see a message with followups inappropriately
set to comp.lang.c, please ignore it. (I won't bore you with the
other header information that indicates the original poster is a
troll.)
 
A

Arne Vajhøj

Chuck said:
Basic question.

public static void main(String args[])

is fine, right? I know it's common, but is it correct? I don't
need 'int' before the 'main', right?

Java is not C.

You use void main in Java and int main in C.

Arne
 
A

Arne Vajhøj

Daniel said:
In Java, it must always be
public static void main (String[] args) // Pre 1.4 preferred
or
public static void main (String...args) // 1.5 or later preferred
or
public static void main (String args[]) // Never preferred, but valid

I would use String[] even at 1.5+ because it is more
traditional syntax and it will (practically) never be used
from Java with a variable number of arguments.

Arne
 
Z

Zach

Chuck said:
Basic question.
public static void main(String args[])
is fine, right? I know it's common, but is it correct? I don't
need 'int' before the 'main', right?
Thanks,
Chuck Heathfield

So, I wrote this reply before realizing the f-u was set to comp.lang.c.
It seems like this may have been an attempt to troll. However, giving
the benefit of the doubt, here is the answer:

In Java, it must always be
public static void main (String[] args) // Pre 1.4 preferred
or
public static void main (String...args) // 1.5 or later preferred
or
public static void main (String args[]) // Never preferred, but valid


Please stop posting this stuff in comp.lang.c, the topic of this
newsgroup is C not Python!

Zach
 
A

Arne Vajhøj

Zach said:
public static void main(String args[])
is fine, right? I know it's common, but is it correct? I don't
need 'int' before the 'main', right?
So, I wrote this reply before realizing the f-u was set to comp.lang.c.
It seems like this may have been an attempt to troll. However, giving
the benefit of the doubt, here is the answer:

In Java, it must always be
public static void main (String[] args) // Pre 1.4 preferred
or
public static void main (String...args) // 1.5 or later preferred
or
public static void main (String args[]) // Never preferred, but valid


Please stop posting this stuff in comp.lang.c, the topic of this
newsgroup is C not Python!

You think Daniel is posting Python in comp.lang.java.programmer ?

Tsk tsk !

Arne
 
M

Mike Schilling

Arne said:
Daniel said:
In Java, it must always be
public static void main (String[] args) // Pre 1.4 preferred
or
public static void main (String...args) // 1.5 or later preferred
or
public static void main (String args[]) // Never preferred, but
valid

I would use String[] even at 1.5+ because it is more
traditional syntax and it will (practically) never be used
from Java with a variable number of arguments.


Really? I think it could be done so quite often, by a test program
emulating a user who types in different numbers of arguments, or
includes and omits flags, etc.
 
A

Arne Vajhøj

Mike said:
Arne said:
Daniel said:
In Java, it must always be
public static void main (String[] args) // Pre 1.4 preferred
or
public static void main (String...args) // 1.5 or later preferred
or
public static void main (String args[]) // Never preferred, but
valid
I would use String[] even at 1.5+ because it is more
traditional syntax and it will (practically) never be used
from Java with a variable number of arguments.

Really? I think it could be done so quite often, by a test program
emulating a user who types in different numbers of arguments, or
includes and omits flags, etc.

Good point.

Maybe I have just been doing too little console app programming.

Arne
 
M

Martin Gregorie

Mike said:
Arne said:
Daniel Pitts wrote:
In Java, it must always be
public static void main (String[] args) // Pre 1.4 preferred or
public static void main (String...args) // 1.5 or later preferred or
public static void main (String args[]) // Never preferred, but valid
I would use String[] even at 1.5+ because it is more traditional
syntax and it will (practically) never be used from Java with a
variable number of arguments.

Really? I think it could be done so quite often, by a test program
emulating a user who types in different numbers of arguments, or
includes and omits flags, etc.

Good point.

Maybe I have just been doing too little console app programming.
I've been doing quite a lot and using pretty much the gamut of argument
types (file names, options, options with values, both long [--optname]
and short [-o] option naming conventions) and have yet to find anything
that can't be easily handled by a 'String[] args' argument declaration.
 
T

Tom Anderson

Mike said:
Arne Vajhøj wrote:
Daniel Pitts wrote:
In Java, it must always be
public static void main (String[] args) // Pre 1.4 preferred or
public static void main (String...args) // 1.5 or later preferred or
public static void main (String args[]) // Never preferred, but valid
I would use String[] even at 1.5+ because it is more traditional
syntax and it will (practically) never be used from Java with a
variable number of arguments.

Really? I think it could be done so quite often, by a test program
emulating a user who types in different numbers of arguments, or
includes and omits flags, etc.

Good point.

Maybe I have just been doing too little console app programming.

I've been doing quite a lot and using pretty much the gamut of argument
types (file names, options, options with values, both long [--optname]
and short [-o] option naming conventions) and have yet to find anything
that can't be easily handled by a 'String[] args' argument declaration.

The point of String... is not to change the way you implement main - it
makes absolutely no difference at all - but to make it more convenient to
call main from other code. For instance, if you want to write a test
harness, you can do:

@Test
testMain() {
MyApp.main("destroy", "noc-list.txt");
}

Rather than:

@Test
testMain() {
MyApp.main(new String[] {"destroy", "noc-list.txt"});
}

It's a modest improvement.

tom
 
A

Andreas Leitgeb

Martin Gregorie said:
I've been doing quite a lot and using pretty much the gamut of argument
types (file names, options, options with values, both long [--optname]
and short [-o] option naming conventions) and have yet to find anything
that can't be easily handled by a 'String[] args' argument declaration.

This question is only tied to whether you ever *call* main-methods from
your own code. If you do, then declaring main(String... args)
means, that you can call it from Java code as such:
SomeProgram.main("-o", "foobar", "--optname", "snafu", filename);
(and the compiler creates the String[] for you)

If you declared it with String[] args, you'll have to create the
String array exlicitly (as you always had to do in pre-1.5 Java).

If you never call main-methods from your own Java code, then you
won't ever notice the difference for main-methods - definitely not
from main()'s implementation.
 
L

Lew

No benefit of the doubt is warranted.  This is part of a pattern of
abusive messages.  If you see a message with followups inappropriately
set to comp.lang.c, please ignore it.  (I won't bore you with the
other header information that indicates the original poster is a
troll.)

Interestingly, in clj.help:

Where's the FAQ for this newsgroup? Thanks.

and the followup to that message was set to comp.lang.c.

A spoofer?
 
M

Martin Gregorie

The point of String... is not to change the way you implement main - it
makes absolutely no difference at all - but to make it more convenient
to call main from other code. For instance, if you want to write a test
harness, you can do:

@Test
testMain() {
MyApp.main("destroy", "noc-list.txt");
}

Rather than:

@Test
testMain() {
MyApp.main(new String[] {"destroy", "noc-list.txt"});
}

It's a modest improvement.
I missed that, probably because I've never tried to call main() from
another program.

I don't leave testing code in classes, so missed that use too.

The closest I've come was to write a "Launcher" class to select the
program to run from the application's jar file. Even there String[] was
fine since the launcher merely loads the main class, named in args[0],
and passes args[1]-args[args.length] to the class its just loaded.
 
L

Lew

Keith said:
Yes, as I indicated above, it's a forgery, part of a harassment
campaign against the comp.lang.c newsgroup and against me personally.
(I do know how to find FAQs.)

I apologize for not recognizing that more quickly.

Several of us here in the clj groups had someone spoofing our posts a
few months back, somewhat more viciously but equally annoyingly. Some
contributors suggested that Usenet needs a digital-signature scheme to
prevent such spoofs.

I conclude that you (the real Keith) are a worthwhile newsgroup
contributor, to have attracted the attention of a spoof-troll.

Keep up the good work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top