• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

diff bet using return true false at same time

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
Ranch Hand
Posts: 119
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic