• Post Reply Bookmark Topic Watch Topic
  • New Topic

main() as int type  RSS feed

 
sireesha vadlamani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i write the program as follows?

 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried it?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll even give you a hint...Can you write a program like this?

 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can most certainly ask your fellow Ranchers that question but there are other things that may know the answer...the Java Compiler and the Java Virtual Machine!
I copied your code, tried to compile it and might even have tried to run it.
Why don't you give it a shot? Try to compile the code and if it compiles, try to run it.
Post back with your results and let's see if we got the same thing
 
sireesha vadlamani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I'll even give you a hint...Can you write a program like this?



I can compile thye program but i got a warning message that "Exception in thread "main" java.Lang.NoSuchMethodError:main"
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that surprise you? After all, your program doesn't have a method defined as


does it?

When you run the java command, and give it the name of a class - something like "java A", the jvm starts up, and looks in that class for a method defined EXACTLY as above. It must be public. It must be static. It must be void, and it must take a String array as an argument (without getting into the variable args issue).

So, you can write the program as above. You can compile it. but you can't run it because it does not have the correct definition of a main method.

What do you suppose this means for your



Does that have the REQUIRED main method to RUN?
 
sireesha vadlamani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Does that surprise you? After all, your program doesn't have a method defined as


does it?

When you run the java command, and give it the name of a class - something like "java A", the jvm starts up, and looks in that class for a method defined EXACTLY as above. It must be public. It must be static. It must be void, and it must take a String array as an argument (without getting into the variable args issue).

So, you can write the program as above. You can compile it. but you can't run it because it does not have the correct definition of a main method.

What do you suppose this means for your



Does that have the REQUIRED main method to RUN?

I got it now and i got another doubt....
class A
{
public static void main(String[] s)
{
System.out.println(s);
}
}
The above program is compiled and executed i got the output as "[Ljava.lang.Stirng;@3e25a5" even though it does not follow the same syntax as required for main() method.
What is that output??
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...though it does not follow the same syntax as required for main() method


Actually, it does follow the method signature structure for the main() entry method. The name you give to the parameter variable doesn't matter to the signature.

The convention, however, would dictate you use the parameter name of args, but any valid variable name is fine. The name doesn't change anything in the signature.

Now, the reason the output is "[Ljava.lang.Stirng;@3e25a5" (sic) is because String[] is an Array of Strings and an Array is an Object.
An Object that doesn't override the toString() method will use the default Object super class toString() method, which prints the memory reference address in hexacode.

If you want an Array to print out the values stored in the Array, you would need to call the Arrays class's static toString() method:


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!