answersLogoWhite

0


Best Answer

It is a syntax error, because a value returning method must return a value, and not writing a return statement with a value is tantamount to returning without a value.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is wrong with not writing a return statement in value-returning method in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Difference in using a method that return value and method that does not return value in object orinted programming?

A method that return a value should have a return statement. The method signature should indicate the type of return value. While in the case of a method that does not return a value should not have a return statement and in the signature, the return type is void. When using a method that doesn't return a value, a programmer can not get a value from that function, but instead, it can only change variable values and run other methods.


Why use void in main of java?

No. void is not a data type. It is mandatory for all java methods to return something and if it is not going to return anything, we have to mark the method with a "void" return type to let the JVM know that it must not expect anything from the method.


Can return statement be in a void method in java?

the main method in java is the client code therefore doesn't return any values Unlike languages like C/C++, the user doesn't specify an error return code by returning from the main method. Instead they should use System.exit(code) to do this. If the Java main method returns, the default code of zero is returned.


What are return statements in java?

you can understand what is the purpose of return in java after this simple example Basically return use in java to return some values from a method note: this sample will work on all java versions, i test it on JDK 1.4 enjoy the code. file name : Class1.java ----- package mypackage1; //package name public class Class1 //class name + file name the { public String add(int a,int b) //a method in the class; its type is String and it take a and b variables; a and b are int { int answer = a+b; //this is some code in the class; maybe some operations; no need to know whats happening in this method System.out.println("I'm in Class1"); //this is some code in the class; maybe some operations; no need to know whats happening in this method return " I'm the returned value "+ answer; ////this is what you need to take from the method } public static void main(String [] arg) { Class1 c1 = new Class1(); //creating c1 object from Class1 Class System.out.println(c1.add(10,6)); //printing the returned values from the method; } } ----------------- output: I'am in Class1 I'am the returned value 16


Is 'void' a data type in java?

No. The void keyword is used to signify that a method will not return any objects. For example, if you type in "double", you'll need a return statement that has a double. So "void" means that there will be nothing returned.

Related questions

Difference in using a method that return value and method that does not return value in object orinted programming?

A method that return a value should have a return statement. The method signature should indicate the type of return value. While in the case of a method that does not return a value should not have a return statement and in the signature, the return type is void. When using a method that doesn't return a value, a programmer can not get a value from that function, but instead, it can only change variable values and run other methods.


Why use void in main of java?

No. void is not a data type. It is mandatory for all java methods to return something and if it is not going to return anything, we have to mark the method with a "void" return type to let the JVM know that it must not expect anything from the method.


Can return statement be in a void method in java?

the main method in java is the client code therefore doesn't return any values Unlike languages like C/C++, the user doesn't specify an error return code by returning from the main method. Instead they should use System.exit(code) to do this. If the Java main method returns, the default code of zero is returned.


What are return statements in java?

you can understand what is the purpose of return in java after this simple example Basically return use in java to return some values from a method note: this sample will work on all java versions, i test it on JDK 1.4 enjoy the code. file name : Class1.java ----- package mypackage1; //package name public class Class1 //class name + file name the { public String add(int a,int b) //a method in the class; its type is String and it take a and b variables; a and b are int { int answer = a+b; //this is some code in the class; maybe some operations; no need to know whats happening in this method System.out.println("I'm in Class1"); //this is some code in the class; maybe some operations; no need to know whats happening in this method return " I'm the returned value "+ answer; ////this is what you need to take from the method } public static void main(String [] arg) { Class1 c1 = new Class1(); //creating c1 object from Class1 Class System.out.println(c1.add(10,6)); //printing the returned values from the method; } } ----------------- output: I'am in Class1 I'am the returned value 16


How does return statement work in vb?

No Return statement in VB programming


Is 'void' a data type in java?

No. The void keyword is used to signify that a method will not return any objects. For example, if you type in "double", you'll need a return statement that has a double. So "void" means that there will be nothing returned.


When is a return statement required in a method?

1) When you want to end the method prematurely for any reason. However, it is usually recommended to have a single exit point in a method, so you may want to avoid this. 2) When the method produces a result, which must be returned to the calling program.


What is a return statement used for?

It means end the function. Functions automatically end when execution reaches the end of the function, but you can return from a function at any point within the function with a return statement. If the function returns a value to its caller, you must provide a reachable return statement along with the value you wish to return.


What statement returns a value from a function?

return


Can an overridden method return void instead of an object?

Sure. An overridden method can return anything it wants.


What does a void method do?

In Java, this keyword is used to specify that the method has no return value.


How can a method return an object?

With the return statement, followed by the object you want to return. For example, in Java:String myMethod1() {return "abcde";// Note: The above is shorthand notation; you can also use the new operator.}MyDate myMethod2() {MyDate x = MyDate(2012, 1, 1);return x;// Note: This assumes you have created the class MyDate,// With the corresponding constructor.}