Reputation: 11468
I am trying to learn Java now and this is the hello world program and it already have started to baffle me. I am used to python and I found this tutorial (ebook) simple and concise for programmers who have python background.
Hello world program in Java from the book:
public class Hello {
public static void main (String[] args) {
System.out.println("Hello World!");
}
}
As the book says, the equivalent code for this in python is:
class Hello(object):
@staticmethod
def main(args):
print "Hello World!"
I completely understand the python code. However, I have a problem with Java code and I want to be clear before I proceed further so that I get the root knowledge of language in my brain.
The book says (as copied from book):
...we have one parameter. The name of the parameter is args however, because everything in Java must have a type we also have to tell the compiler that the value of args is an array of strings. For the moment You can just think of an array as being the same thing as a list in Python. The practical benefitt of declaring that the method main must accept one parameter and the parameter must be a an array of strings is that if you call main somewhere else in your code and and pass it an array of integers or even a single string, the compiler will flag it as an error.
This does not make any sense to me. Why can I not pass anything since my function doesn't require anything? What happens if I just pass (String args).
Since I am completely newbie to Java, please bear with me.
Upvotes: 3
Views: 854
Reputation: 1022
Every program has an entry point/starting point from where its execution would start. Java searches for a specific method signature it will start from in a class to run your application which is the 'main' method.
public static void main(String args[]);
This must be implemented in order for you to start your program ( in a class you want to start off from). And because of this rule/restriction we must pass array of strings ( which are similar to list of strings in Python) as arguments.
Now for the second part, if your program does not require any parameters at startup, dont pass any. You will get args as an empty String array in main method. The following line would print out the length of arguments passed to your main method.
System.out.println("Length of arguments = " + args.length);
You might also want to look at Sun's Java guide for starters. I hope this answers your question.
Upvotes: 2
Reputation: 346260
First of all, note that the main method is called by the JVM to start the program and passed the command line arguments. It can be called by Java code, but this is very rarely done.
Anyway, this is the signature of the method:
public static void main (String[] args)
It says that it requires a parameter that is an array of Strings. If you call it like this:
main(new String[1]);
or this:
main(methodThatReturnsAStringArray());
it will work. But these will cause a compiler error:
main(new int[0]);
main("test");
Because the type of the parameter in the call does not match the type the method signature requires. You can pass a null pointer:
main(null);
Because arrays are a reference type, and null
is a valid value for all reference types. But then the method will have to test for that case, otherwise it will throw a NullPointerException
when it tries to access the array.
Another thing you can do is overloading, by declaring another method:
public static void main (String args)
So when you call
main("test");
the compiler would determine that there is a method with a matching signature and call that.
Basically, the point of all this is that many programmer errors are caught by the compiler rather than at runtime (where they may only be discovered in some special circumstances if it's a rarely executed code path).
Upvotes: 1
Reputation: 287755
In Java, there is no top-level code as in Python (i.e. there is no direct equivalent to just
print('Hello world')
). Nevertheless, a Java program needs some kind of entry point so that it can start executing code. Like many other languages (i.e. C/C++, C#, Haskell), a function with the name main
serves for this purpose and is called by the runtime (i.e. Java interpreter).
When calling the main
function, the runtime uses a certain number (and types) of arguments. The function must match these. You're free to then call other methods with any signature you like:
public class Hello {
public static void hi() {
System.out.println("Hello World!");
}
public static void main (String[] args) {
hi();
}
}
Upvotes: 2
Reputation: 8714
As you know, Python uses "duck typing": it doesn't matter what type something is, only what it can do. As a result, you never need to declare types for your variables.
In Java, that's not true: every variable has a declared type, and the compiler enforces that type. Trying to store, for example, a String
reference in an int
-declared variable will produce a compile-time error. Proponents of duck typing claim that this decreases flexibility, but strong-typing enthusiasts point out that compile-time errors are easier to fix than run-time bugs.
But the same is true of your method arguments. Since your method requires an argument of type String[]
, it must be provided an argument of type String[]
. Nothing else will do.
Fortunately, since it's the main
method, the Java interpreter takes care of passing in an argument: specifically, an array of the command-line args with which your program was executed. If you'd like to ignore it, feel free. Your program will run just fine without paying attention to the argument, but it's invalid if one isn't passed in.
(By the way, if this were any method but the main
method, you'd be free to declare it with whatever argument types you'd like, including no arguments at all. But since the Java interpreter will be passing in an array of the command line arguments, this particular method must be prepared to accept them.)
Upvotes: 2
Reputation: 649
When you execute your programme from command line, Commandline parameter can be more than one string.
eg.
Java myprogramme param1 param2 param3
all this parameters - param1, param2, param3 are passed as string[].
This is common for all program, who pass zero, one or more params.
As a programmer your responsibility to check those command line params.
Upvotes: 0
Reputation: 15063
Well, there are two things going on here. First, is function signatures - since you declare main
to expect and accept only an array of strings, it'll raise an error if you try to pass it something else. But not because the function refuses to accept it, but because the compiler won't know what you're trying to call. See, you can have multiple functions with the same name, but different arguments. So if you were trying to call main(1)
, the compiler would look for a function main that accepts one (or more) integers, not find it, and raise an exception.
The other thing going on here is that when you start a program, the compiler looks for this particular signature - public static void main (String[] args)
and nothing else. Can't find it? The program won't run.
Upvotes: 4
Reputation: 133567
If your function does not require anything (so it has no parameters) then you are allowed to avoid passing anything, this is done by declaring it as
void noArgumentsFunction() {
// body
}
But the main
function, that is a boiler plate, must accept an array of String
s. That's why you are forced to declare the signature to accept it (and then ignore it in case). The funcion must accept this parameter because it's the entry point for your program and any Java program must support a array of parameters that is passed with command line (exactly as every C/C++ program, also if you are not forced to do it).
Upvotes: 1
Reputation: 88977
Well, you are passing in something. Whenever you run a java program, command line arguments are passed in as the args
argument, so you need to accept those, even if you don't use them.
Upvotes: 6