• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about return and System.out.println()

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very clear explaination, I think I understand it now and apparently I need to first spend some time learning some basics.
Thx again!
reply
    Bookmark Topic Watch Topic
  • New Topic