• Post Reply Bookmark Topic Watch Topic
  • New Topic

Boolean expression in System.out.println();  RSS feed

 
prateek shaw
Ranch Hand
Posts: 46
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All.

I just want to understand why give compile error ?

bad operand types for binary operator '|' System.out.println(true|false+"shaw");

I can use arithmetic operator like + ,- %and / but not the binary operator , i just would like to understand reason behind this , i did not find any where.


 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See operator precedence: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

+ has higher precedence over bitwise |
 
prateek shaw
Ranch Hand
Posts: 46
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that is correct and but still i had confusion that why it is giving error.
I knew rule precedence all the Boolean operator has less precedence over arithmetic operator.

But i was just missing line which should explain me why it is throwing error.

And now i understand.

basically  System.out.println(true|false+"hello");
Since + operator has more precedence over | there for it will became like true|"falsehello" and since | operator work if both operand boolean and in  this case it is not
that is reason why it is throwing exception.

Thanks for replay which force me to rethink  and now i understand fully. i  got missing link.  
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is executed like this
  • true |  now looks at right side is there higher precedence operator than | answer is yes i.e. + additive operator so goes to +
  • true |  ( false + "hello")  does conversion of boolean to Boolean and then that Boolean reference to String then does String concatination results "falsehello".
  • true | ("falsehello") now you can see | the bitwise and logical operators may be used to compare two operands of numeric type or two operands of type boolean. All other cases result in a compile-time error so | operator can't be applied to boolean and String.
  • so you get that error.
    Try doing this see what you get
     
    Campbell Ritchie
    Marshal
    Posts: 55772
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    prateek shaw wrote:. . . that is reason why it is throwing exception. . . .
    I hope you are not having an exception thrown; Junilu and Ganish have explained why that code will not even compile. A compile‑time error is different from an exception.
    One way to make it compile is to add ():-
    ... (true | false) + " hello" ...
    That will evaluate to
    "true hello"
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!