• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Boolean methods

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused with boolean methods.

boolean frighten(int d){
System.out.println("arrrrgh");
return true;
}


boolean frighten(int x){
System.out.println("a bite");
return false;
}


What will be the output of these methods ?
first metho will print, because its return value is true and second method will not print because its return value is false. Is that right ?

My confusio is, the method will return something only if return value is true otherwise if the value is false it will not output anything. So for boolean method to return anything, the return value has to be true, am I right ?

Pls explain, and thanks for your co-operation.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what a method returns is unrelated to what it does. ideally there is some logical connection, but there doesn't have to be.

a method that is declared as boolean simply means that it will return a boolean - either true or false. The method could print something, could compute a 13th root of a 100 digit number, play a song... doesn't matter what it does, but when it's done, it sends a boolean back to the caller.

This value in no way effects what the method does. so, in your examples...

an int is passed into both methods. it happens that that int is never used, but it's still there if you need it.

both methods then print their respective strings. one then returns true to the caller, and the other returns false. those values in no way alter what the method does.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider this

if(x){
System.out.print(" x is true, so I am printing");
}else
{
// x is false, I don't print anything
}

this piece of code will do exactly what you said. but this is not a function.

All functions with a return type must return a value of that type.
So, both your functions have to return a boolean value i.e. either true or false.

so if you call your first function as
System.out.println(frighten(1));
it will print:
arrrrgh
true

and same if you do with your second function definition, it will print
a bite
false

the functions do what they are programmed to do and then return the value irrespective of what it is (as long as it confirms to the return type).
 
jignesh soni
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply guys.

But still not clear.
I will put the whole issue here. This is taken fron Head First Java book


class Monster{
boolean frighten(int d){
System.out.println("arrrrrgh");
return true;
}
}

class Vampire extends Monster {
boolean frighten(int x){
System.out.println("a bite?");
return false ;
}
}


class Dragon extends Monster {
boolean frighten(int degree){
System.out.println("breath fire");
return true;
}
}

public class MonsterTestDrive{
public static void main(String[] args){
Monster[] ma = new Monster[3] ;
ma[0] = new Vampire();
ma[1] = new Dragon();
ma[2] = new Monster();

for(int x=0; x<3; x++){
ma[x].frighten(x);
}
}
}



will this be the output for this coding , when I run MonsterTestDrive at command prompt ?

a bite ? false
breath fire true
arrrrgh true

Pls explain.
Thanks for youe replies
 
Ranch Hand
Posts: 95
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bro,

I tried this



And there is a syntax error at the line that the method is duplicated.
How did you compiled it?
Is anything wrong with my code?

Tnks.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


will this be the output for this coding , when I run MonsterTestDrive at command prompt ?

a bite ? false
breath fire true
arrrrgh true


Where are you printing the returned value ? Nowhere. "return true;" does not print anything. It justs returns true. Same for "return false;", which returns false. Nothing printed here either.
If you want to print out the returned value, you could change your code like this :

Here is another example (that does not compile):

[ December 13, 2007: Message edited by: Christophe Verre ]
 
jignesh soni
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are you printing the returned value ? Nowhere. "return true;" does not print anything. It justs returns true. Same for "return false;", which returns false. Nothing printed here either.
If you want to print out the returned value, you could change your code like this :

So my question is, if boolean method just prints whatever is on println irrespective of return value true or false then whats the meaning of return value ? what is the significance of it ?
 
jignesh soni
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
--------------------------------------------------------------------------
Where are you printing the returned value ? Nowhere. "return true;" does not print anything. It justs returns true. Same for "return false;", which returns false. Nothing printed here either.
If you want to print out the returned value, you could change your code like this :
--------------------------------------------------------------------------
So my question is, if boolean method just prints whatever is on println statement irrespective of boolean value true or false, then whats the significance of true or false value and why do we need it ?
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andre,

Originally posted by Andre Brito Fonseca:
Bro,

I tried this



And there is a syntax error at the line that the method is duplicated.
How did you compiled it?
Is anything wrong with my code?

Tnks.



What you are trying to do is making a duplicate method inside the same class itself,which compiler will not allow.
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Christophe Verre,

Originally posted by Christophe Verre:

...Here is another example (that does not compile):


[ December 13, 2007: Message edited by: Christophe Verre ]



Just make a couple of changes in your code and it will surely work.Change you code like this
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Balasubramanian :

Just make a couple of changes in your code and it will surely work.


You don't seem to understand the problem here. It's not about compile errors, but about the meaning of boolean values. I was just posting a kind of algorithm here. Please read the first post to understand what is being discussed here.

jignesh:

So my question is, if boolean method just prints whatever is on println statement irrespective of boolean value true or false, then whats the significance of true or false value and why do we need it ?


Let's take it back to your first example :

As long as the frigthen method is called, you now understand that "a bite" will be printed out, don't you ? So what is the use of "return false;" ? Your method returns a boolean. You will use that returned value to do something else after this method has been called. You may want to do different things according to the returned value. For example, print another message only when the returned value is true :

This method is not very interesting, so let's change it a bit. Let's imagine that only kids under 5 years old will get frightened in the monster house :
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K i got that point.
 
jignesh soni
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Christopher , I guess now I have begu to understand boolean. That was good explanation.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic