Forums Register Login

Question about return and System.out.println()

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
Very clear explaination, I think I understand it now and apparently I need to first spend some time learning some basics.
Thx again!
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 10595 times.
Similar Threads
Query with String statement
signature of constructor
constructors
Help on testprogram and subclass please
NX: RMI and Mutlithreading
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:33:57.