• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

public static long main

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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.

Thanks.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks,
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic