• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Regarding Return Type.

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

The following code gives error:

public static int m1()
{
int i;
if(true)
return 1;
}

Please clarify my doubt.

Thanks in advance.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your method declares that it will return an int, but the compiler says "missing return statement." What is returned if the "if" condition is false?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's a tricky question
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Apana,
FYI-u have forgot to initialize the variable "i" in the static method,since i is a local variable.This also might be one of the reason why it is not compiled.
If wrong with my mess....
Let me know........

Regards
Kirba.




public static int m1()
{
int i;
if(true)
return 1;
}
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi devi,
I think it is not initialization problem.
It is bcz of if statement. right guys??
 
Apna Apana
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi suresh,
you are correct.Please give me the clear explanation for the error.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error is :
This method must return a result of type int

The error is not becuse of i is not initialized, One would see the error when i is used without initializing [The local variable i may not have been initialized].
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends
the problem is not with the variable initialization, compiler will not complain about initialization till we use that variable. In this case just 'i' declared only, no usage. So, no problem with that.
The problem is the place of the return statement.
If we are using it in any conditional statement, we should be sure that for every flow the method can return. i.e. there would be 'else' statement and it should return any integer value.
for example:
-------------------------------------------
package basics;
public class ReturnTest{
public int method(){
int i;
switch(9){
case 3:
System.out.println("3");
return 3; // line 8
case 4:
System.out.println("4");
case 9:
System.out.println("9");
return 9;// line 13
default:
System.out.println("default");
return 0;// line 16
}

}
public static void main(String[] args){
System.out.println(new ReturnTest().method());
}
}

----------------------------------------------------
in this example it make sure that for every flow, there is some return.
(look at for case 4: there is no return but as switch continue to the next case if there is no break statement, it will find the return.So no problem. If we use break for case 4: it will complain 'missing return statement').

i hope it will clear

regards
Mallik
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mallik,

if you don't use code tags, all your indentations will be lost.

To indent your code properly:

At the posting page, mark the part of your posting that should be indented. Usually that will be your code (or some tabellaric output).

Hit the - Button below.
that's it!


Yours,
Bu.
 
If you look closely at this tiny ad, you will see five bicycles and a naked woman:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic