简体   繁体   中英

true/false Boolean value inside println method

public void run(){
    int x = 9; 
    int y = 9;

    println( "true or false = " + (x == y) );
    println("true or false = " + ( x < y) );
    prinltn("true or false = " + (x > y) );

}

In an example my book uses parentheses () inside the println method to test whether something is true or false. It was my first time seeing () used as a boolean test inside the println method. Previously I thought to solve the problem by something like

  if (x == y) {
     println("true or false = true");
  } else {
     println("true or false = false");
  }
  1. where can i find more information about boolean inside the println method?
  2. Is one more right than the other? Should i avoid using one of the examples above?

Is one more right than the other?

Right is not really the correct term to describe it since both of them works just fine. The only difference is the lines of code. So no. Both of them are right.

Should I avoid using one of the examples above?

Not exactly, but I do recommend you start making your code as short as possible to make it easier to maintain, like the first one.


As for what's happening with those println s it's just a simple String concatenation:

println("true or false = " + (x == y) );
//       true or false =       true

No. Do not avoid. Use () . It is not about inside println . it is all about String concatenation.

Just for a change try to execute the below statements and check the results.

println( "true or false = " + (x == y) );

and

println( "true or false = " +x == y) );

In the below case operator precedence comes in to picture.

Again coming to the point, why using parenthesis inside print method (x == y) , parenthesis have the high preciseness, so the statements inside them executes first before evaluating the whole expression.

When you test if something is true, that condition becomes a boolean value (a true or false ). Boolean values can be assigned and printed just as other types (like int ) can. So if you put:

System.out.println("true or false = " + (x == y));

it is the same as doing:

boolean f = (x==y);
System.out.println("true or false = " + f);

which is the same as doing:

boolean f;
if (x==y) {
    f = true;
} else {
    f = false;
}
System.out.println("true or false = " + f);

here () is not used for boolean test, it is used for precedence. If your are not using () then first x will be convert to String then your can't use == with string and integer

(incompatible operand types String and int)

To avoiding this we have to use ()

(x == y) 

I can suggest you first example is good, it reduce the number of line.

println("true or false = " + (x == y) );

The first one is advisable just because it has relatively less number of lines of code, but that doesn't mean that the other one is Incorrect. If at all, you want to make use of the boolean o/p of the test being done on x & y to be further used in your code, then you will have to go for-->

boolean b=(x==y);

Then 'b' can be Reused henceforth.

Try this :

String result = (x == y) ? "true" : "false";
System.out.println("true or false = " + result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM