aspose file tools*
The moose likes Beginning Java and the fly likes Boolean methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Boolean methods" Watch "Boolean methods" New topic
Author

Boolean methods

jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Harish Kashyap
Ranch Hand

Joined: Jun 14, 2000
Posts: 118
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

Joined: Dec 10, 2007
Posts: 150
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
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

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.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16


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 ]

[My Blog]
All roads lead to JavaRanch
jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
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

Joined: Dec 10, 2007
Posts: 150
--------------------------------------------------------------------------
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 ?
Balasubramanian Chandrasekaran
Ranch Hand

Joined: Nov 28, 2007
Posts: 215

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

Joined: Nov 28, 2007
Posts: 215

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Nov 28, 2007
Posts: 215

K i got that point.
jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
Thanks Christopher , I guess now I have begu to understand boolean. That was good explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boolean methods
 
Similar Threads
How does return type "boolean" affect print statement in a method ?
Query about Short-Circuit
Confusing TRUE and FALSE
help on HFJ exercise
It should be true.........