File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

diff bet using return true false at same time

 
thirumala raju
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean met1()
{ if (s!= null)
return false;
return true;
}
*** this above code fine if i use brackets like below code it does not work why..?

boolean met1()
{
if (s!= null)
{
return false;
return true;
}
}
 
Wayan Saryada
Ranch Hand
Posts: 105
IntelliJ IDE MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second code doesn't work because after the return false statement, the return true statement is not reachable so the compiler will complain.

On the other hand, the first code have a different exit point which will cause only one return statement will be executed. If s is not null then it return false, if null return true.
[ January 03, 2008: Message edited by: I Wayan Saryada ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Those two pieces of code are not the same. Please use code tags when you post code on JavaRanch, so that the formatting of the code is preserved.

The first piece of code is the same as this here below.

Do you now understand how it works?

The second piece of code does not compile because the statement "return true;" is unreachable.
 
thirumala raju
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply saryada, young,

you said, based on the condition it will return boolean value. That is wrong. Based on the sequence of statements placed in the method only it will return the boolean value. And i have used the same statements in both IF condition and inside the method. Could you please eaplain me how come that exit of the statements can differ


regards
raja
SCJP 1.4
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps it helps if we reformat the code:

if (s!=null) return false;
return true;

Do you now see why the second line is reachable?
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by thirumala raju:
thanks for your reply saryada, young,

....Based on the sequence of statements placed in the method only it will return the boolean value. And i have used the same statements in both IF condition and inside the method....


Here what you mean "Sequence of statements placed in the method".

And i will show you how your exit statements differ.

Your first code when formatted will be in the form(as Jesper Young already quoted).


But in your later case



That is the problem with your code.
 
kishore kovil
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tirumala Raju,

It is a good programming practice to include the braces even for a single statement inside a loop .
Hope this clears your confusion.

Thanks,
Kishore
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic