aspose file tools*
The moose likes Beginning Java and the fly likes public static long main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public static long main" Watch "public static long main" New topic

public static long main

Adrian Perry
Ranch Hand

Joined: Jul 21, 2005
Posts: 42

I had appeared for an interview, that has a couple of question which I felt I could have answered a little bit more satisfactorily

1) Is public static long main(String args[]) a valid method, if yes why if not why?
Googled this question, but did not get a satisfactory reply. It seems this method is valid in C++ but not in java.

2) What is the difference between public static void main(String args[]) and public static void main(String []args)
My reply:Both are valid methods and we can use either one.
The panel was'nt satisfied.

Kindly can you answer the above questions esp the first one.

Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
1. Yes it is, but it is not a valid entry point. There is nothing invalid about this method, but if you try to use it as the programs starting point it will throw java.lang.NoSuchMethodError: main Exception in thread "main" and exit. However, use that method as anything but the starting point and it will be ok, as long as it isn't in the same class as the 'real' main. Different return types are not enough to differentiate overloaded methods.

2. I am pretty sure they are equivalent.
[ May 12, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra

Joined: May 12, 2006
Posts: 1
good question had a look on the net and heres what sun say

You can write an array declaration like this:
float anArrayOfFloats[]; //this form is discouraged

However, convention discourages this form because the brackets identify the array type, and so they should appear with the type designation, not with the array name.
As with declarations for variables of other types, the declaration for an array variable does not create an array and does not allocate any memory to contain array elements. The code must create the array explicitly and assign it to anArray.

hope this helps
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
1) Rusty is quite correct.
2) Both forms are equally correct. Which you prefer is personal preference (at least in Java, in other languages they may have different implications) and a matter of long philosophical discussions about what each means to convey to the reader (while having the same result when compiled). Sun prefers one form, so that's what most certified programmers semi-automatically use (writing an app for SCJD certification really indoctrinates you to follow Sun coding conventions religiously).

If the people interviewing you didn't agree with your statement that the two are equivalent (to the compiler at least, and for Java) they should have given you their reasons and you should have had a nice discussion.
Maybe that was their reason for disagreeing in fact, trying to get you to talk instead of just answering rote questions.

Adrian Perry
Ranch Hand

Joined: Jul 21, 2005
Posts: 42
Thanks everybody for the quick reply.
Rusty was right when he said public static long main cannot be a starting point of the program. We also know that the return type here is long, so definately we have to call this method.
If we call it from another class we should get a long value in the calling method and we should be able to print that long value. But this is not happening.
Can anybody give demo of how this works and the conditions where is used explicitly. A small code might help.

Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Like this very simple code?

It works like any other method. The fact that the other method is called main has no significance at all. It is a static method that accepts whatever arguments you want to pass and returns a long. IMO, it is a poor name for a method in most cases.
I agree. Here's the link:
subject: public static long main