Difference between returning and printing values in Java?
pencilwars
2009-09-20 15:03:29 UTC
What is the difference between printing and returning values in Java?
Three answers:
?
2009-09-21 00:06:52 UTC
//PK
Returning: It means a method returns back some result to the caller of this method.
Printing: It means a method is just sending result ot standart output device (screen)
See below example:
public class Test20 {
/**
* This method will return the result to caller
* (Not print on screen)
* @return
*/
public String getValue (){
return "Hello";
}
/**
* This method only print the value on screen
*/
public void show(){
System.out.println ("Hello");
}
public static void main (String[] args) {
Test20 t = new Test20();
String result = t.getValue();
System.out.println ("Result returned from method = "+result);
t.show(); //just calling the method not getting any result
}
}
?
2009-09-20 15:33:33 UTC
Printing a value actually post's it to the console/application screen. Returning a value however just gives a value back from whomever may call's that specific method.
anonymous
2016-05-21 13:48:26 UTC
The other respondent has given excellent answer. To add little more. Array size will not be changed during life of the array. That is if we create a 10 element array, it can be increased or reduced during its life. Some times this is hurdle and some other times it may lead to wastage of memory. Whereas vector, whenever the allocated space is full and you want to add one more element then its allocated space is automatically doubled and new element is added to it. Also, memory shrinking also takes place.
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.