Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Question about return and System.out.println() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about return and System.out.println()" Watch "Question about return and System.out.println()" New topic
Author

Question about return and System.out.println()

Chun Lee
Greenhorn

Joined: Jan 16, 2005
Posts: 3
Hi guys,

I'm new to Java programming and I just read a few chapters of the textbook my instructor assigned. I'm a little confused about some statement usage:

I wrote a class containing 2 methods look like this:

public void method-name()
{
System.out.println("Hello");
}

AND

public String method-name()
{
return "Hello";
}

After compiling, I created an object of this class. It turns out I got the same result when I call those methods to this object I just created. However these are two types of methods which are procedure and function respectively,so what is the difference between the usage of them?

Why can I still get an string-like result using the method with "void" specified? Shouldn't that return no value?

and how can I get rid of the double quotation marks around the words with return statement?

thank you very much!
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
First, you cannot get rid of the quotation marks from the return "Hello"; statement, because to indicate that a phrase is a string, you have to put quatation marks around that phrase (otherwise the compiler won't know what it is).

Second, public void method-name() method does not return anything. Instead it prints out a "Hello" to the console.
The other method does not print out anything to console, but returns a string "Hello".

Based on your question, my guess is that the rest of your code looks smth like this:
If that's so, then in this code the "main()" method calls that "method-name()" method which returns a string (because the predifined "println()" method takes a string as an argument).

So it is not your second method that prints out "Hello" (like the first one did), but it returns a string, and the "System.out.println( method-name() );" statement gets this string and prints it out.

Is this make sense or is this too complicated/cryptic?

Regards,
Andris
Chun Lee
Greenhorn

Joined: Jan 16, 2005
Posts: 3
First of all thx for reply

it really help but.... I guess I should clarify my question this way:
two class file

AND

the first one didn't print out anything in the console and the second did, could you pls explain more on the difference of that?

Thank you in advance!

[ January 17, 2005: Message edited by: Chun Lee ]
[ January 17, 2005: Message edited by: Chun Lee ]
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
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 ]
Chun Lee
Greenhorn

Joined: Jan 16, 2005
Posts: 3
Very clear explaination, I think I understand it now and apparently I need to first spend some time learning some basics.
Thx again!
 
Consider Paul's rocket mass heater.
 
subject: Question about return and System.out.println()
 
Similar Threads
Help on testprogram and subclass please
constructors
Query with String statement
signature of constructor
NX: RMI and Mutlithreading