I am a computer science major and just finished my first semester of school. I found that I would be learning Java, Python, C, Scheme and Haskell throughout my academic career. I decided to start learning Java in advance to increase my chances of success when I encounter it, and the other languages, in class. I am about 63 percent of the way through the CodeCademy curriculum for Java and I also have two books (Heads Up JavaandCore Java: Volume 1 Fundamentals) which I study from as well.
Here is my question. If I have a "void" method, it is my understanding that it does not return anything so therefore the type of return does not need to be specified when defining the method. So in order to see a solution when my basic calculator program runs my "addition" method in main, I can see my solution in my terminal because my method included System.out.println(solution);. Now, if I have a method that DOES return something, I define it as a "double" and ask it to return my double integer "solution" which obviously would equal x * y. Now I want the solution to appear on my terminal when I run my little program, but "returning" the value doesn’t seem to do that and I am unable to include System.out.println within the method itself. Am I only able to include the command in the main method? Such as System.out.println(M.addition(15, 15) in order to see my solution when I run the program using a NONvoid method?
Thank you in advance for any illumination on the subject.
I mostly wanted to dip my toes into StackOverflow this afternoon as I know I will be getting very friendly with the site and it’s inhabitants in the near future.
Thank you.
Case
public class Main {
double solution;
public double addition(int x , int y){
solution = x + y;
System.out.println(solution);
return solution;
}
//This one does not work
public double multiplication(int x, int y){
solution = x * y;
System.out.println(solution);
return solution;
}
//This one does work
public double division(int x, int y){
solution = x / y;
System.out.println(solution);
return solution;
}
public static void main(String[] args){
Main M = new Main();
M.addition(15,15);
M.multiplication(10,100);
M.division(6,2);
M.division(1500, 5);
}
}
I tried invoking my method that contained a return statement in my main method and I expected to see the variable "solution" printed in my terminal, as that is what I included in my return statement, but did not.
Edit: I read the post about including pictures of code and removed my photo. Thank you for the instruction on protocol.
>Solution :
First things first – When you return from a method, that means "we’re done with this method call, exit at this point." Void methods do not return anything, while non-void methods return a value of a specified type.
In your code, the double multiplication(int x, int y) method takes in two integers and then returns the product. Do you want the method to print this product every time you call it? if so, move the print statement to before you return so it is actually run before you exit from the method.
If not, in your main method, you can print the returned value as follows:
System.out.println(M.multiplication(10, 100);.