System.out.println("hello"); is the statement that will print to the console whatever you pass as a string parameter (in this case, "hello");
So, the second code will print out "hello".
The first code will not for two reasons:
1) This method does not print anything, just returns a "hello" string (returning does not mean printing)
2) if you are running you code from the command prompt using
> java Hello
you will get the following error message:
Exception in
thread "main" java.lang.NoSuchMethodError: main
The thing is that for JVM (Java Virtual Machine) to run a class as an application, that class has to have the following method, defined precisely as here:
public static void main( String[] args ) { }
If the main() method is not declared either as public or static, or if its return type is something other than void, or if it accepts any other arguments than array of strings, then JVM will not be able to start you application.
Now, your method has a return type of String. Compiler will pass it as a legal method (other classes/objects can still invoke it) and compile it, but at runtime JVM does not find the "public static void main( String[] args )" signature and therefore cannot start the application.
Hope this helps,
Andris
[ January 17, 2005: Message edited by: Andris Jekabsons ]