AbdullahR
AbdullahR

Reputation: 1041

What does return exactly do in a boolean method?

In this code:

public static void main(String []args){
   int [] a = {10, 20, 30};
   if(m1(a))
     m2(a, 0);
   else
     m2(a, 1);
   for(int i = 0; i < a.length; i++)
     System.out.println(a[i] + " ");
}

public static boolean m1(int [] a){
   return a[0] == a[1] / 2;
}

public static void m2(int [] a, int b){
    a[b] = 1;
}
}

in m2, what does the line comes after return , which is a[0] == a[1] / 2 do? is it a chick statement ? because it is bet confusing when it comes after return

Upvotes: 1

Views: 7494

Answers (6)

chrisdotcode
chrisdotcode

Reputation: 1591

I think you mean m1.

Anyway, it helps if you read the code like this:

public static boolean m1(int [] a){
   return (a[0] == (a[1] / 2));
}

Take note of operator precedence: parenthesis are evaluated before equality. That is, return the equality between a[0] and the result of the expression (a[1] / 2).


If you don't know your precedence rules, you could alternatively read the code as:

public static boolean m1(int [] a){
    int i = a[1] / 2;
    boolean foo = (a[0] == i);
    return foo;
}

Upvotes: 5

Mike Nakis
Mike Nakis

Reputation: 61984

There is a certain kind of programmers who would rather code this as follows:

if( a[0] == a[1] / 2 )
    return true;
else
    return false;

We scorn at this kind of programmers, and during lunch break we do not sit anywhere near them.

So, to answer your question, return a[0] == a[1] / 2; is a perfectly valid statement, and any experienced programmer would rather see it like this rather than in any other way. The '==' inside the expression tells us that this expression is evaluating to a boolean value, and then this boolean value is returned by the function. That's absolutely fine.

If it would help you to better understand it, you could express it as return a[0] == (a[1] / 2); but if you know your operator precedence it is unnecessary, and in any case the alternative (return (a[0] == a[1]) / 2;) could not possibly work because the division operator is not applicable to boolean values.

Upvotes: 1

Brian Roach
Brian Roach

Reputation: 76898

Think of it in terms of an if statement - what does the following do?

if (a[0] == (a[1] / 2)) {
...

It's a boolean result of an evaluation - true or false. If it's true, do something.

Using it with return returns that boolean result to the caller.

Upvotes: 2

ruakh
ruakh

Reputation: 183301

In a method that returns a value, the return statement indicates what value to return. For example, this method:

public static int timesTwo(int i)
{
    return 2 * i;
}

returns two times its argument; so this method:

public static void main(String... args)
{
    System.out.println(timesTwo(7)); // prints 14.
}

prints 14.

Your example:

public static boolean m1(int [] a)
{
    return a[0] == a[1] / 2;
}

returns true if a[0] == a[1] / 2; otherwise, it returns false.

Upvotes: 0

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

return a[0] == a[1] / 2

returns true if the expression is actually true, ie. a[0] is equal to a[1] divided by 2, and false otherwise.

Upvotes: 5

SLaks
SLaks

Reputation: 887433

That's a boolean expression stating the value that the method will return.
It's no different from a method that returns an int.

Upvotes: 2

Related Questions