+ 2

[DUPLICATE] What return value does in java.please somebody explain

18th Mar 2018, 4:25 AM
Devansh Gupta
Devansh Gupta - avatar
11 Answers
+ 11
depends on the purpose of method , suppose U just wanted to print "hello world" using a method , then return type will be void [as U are printing a value , not returning something] example 1) ::: static void printHelloWorld(){ System.out.println("hello world"); } example 2) now if U want to get sum of 2 numbers(let are integers ) using method getSum , here return type will be of integer as sum of 2 integers will obviously an integer only int sum = getSum (1,2); //method call from the main method //method definition static int sum (int a , int b){ return a+b; }
18th Mar 2018, 4:33 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 13
ignore that comment //just see the code & remove your doubts //btw I didn't get even 1 best-marked answer from U my friend ... helping from the beginning 😅 in many of the posts
18th Mar 2018, 5:38 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 11
static void sum (int a,int b){ System.out.println(a+b); }
18th Mar 2018, 5:03 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 10
public class Program { static String coding() { return "hello world"; } public static void main(String args[]){ String A="say"; String B=A+coding(); System.out.println(B);} } //corrected 👍 //srly , m (S gnry) a1(unhl) ... rs lv t qr
18th Mar 2018, 5:36 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
but what if I don't type return in there but just write SOP (a+b) it is gonna ask me the values of a and b while the code runs. if yes what is the return good for and please correct me wherever I am wrong
18th Mar 2018, 4:55 AM
Devansh Gupta
Devansh Gupta - avatar
+ 1
OK OK wait if I say that return function changes a method into a variable containing the value of the method after execution of the method so that it can be used anywhere will it be correct
18th Mar 2018, 5:19 AM
Devansh Gupta
Devansh Gupta - avatar
+ 1
guys please check this one out and tell me what's wrong https://code.sololearn.com/cL1xijw7wuTb/?ref=app
18th Mar 2018, 5:26 AM
Devansh Gupta
Devansh Gupta - avatar
+ 1
what did you mean by that comment
18th Mar 2018, 5:37 AM
Devansh Gupta
Devansh Gupta - avatar
+ 1
what is best marjed yes I am really thankful to you for helping me remove my doubts out from the beginning but I have started sololearn a few weeks back(2 or3) so I feel its obvious that I will feel doubts so please keep helping me
18th Mar 2018, 5:44 AM
Devansh Gupta
Devansh Gupta - avatar
+ 1
sorry my fault
18th Mar 2018, 5:48 AM
Devansh Gupta
Devansh Gupta - avatar