aspose file tools*
The moose likes Beginning Java and the fly likes diff bet using return true false at same time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "diff bet using return true false at same time " Watch "diff bet using return true false at same time " New topic
Author

diff bet using return true false at same time

thirumala raju
Greenhorn

Joined: Jan 03, 2008
Posts: 9
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

Joined: Feb 05, 2004
Posts: 105

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 ]

Website: Learn Java by Examples
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
thirumala raju
Greenhorn

Joined: Jan 03, 2008
Posts: 9
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

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

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

Joined: Dec 21, 2006
Posts: 20
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
 
 
subject: diff bet using return true false at same time