java - "Missing return statement" within if / for / while -


i have question regarding return statements used within if() while() or for() statements. can see in following method, expecting return string value. problem if use return within if statement block, compiler return error missing return statement.

public string mymethod() {     if(condition)     {        return x;     } } 

of course change method header void , use system.out.println instead of return. right way it? missing something?

any highly appreciated.

if put return statement in if, while or for statement may or may not return value. if not go inside these statement method should return value ( null). ensure that, compiler force write return statement after if, while or for.

but if write if / else block , each 1 of them having return in compiler knows either if or else execute , method return value. time compiler not force you.

if(condition) {  return; } else {  return; } 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -