+ 2
[DUPLICATE] What return value does in java.please somebody explain
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;
}
+ 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
+ 11
static void sum (int a,int b){
System.out.println(a+b);
}
+ 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
+ 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
+ 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
+ 1
guys please check this one out and tell me what's wrong
https://code.sololearn.com/cL1xijw7wuTb/?ref=app
+ 1
what did you mean by that comment
+ 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
+ 1
sorry my fault