• Post Reply Bookmark Topic Watch Topic
  • New Topic

mock01a Q41  RSS feed

 
Dmitry Golynkin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
code:
__________________________________________________
public class Question41{
public static void main(String[] args){
Object[] obj = new Object[3];
for(int i=0;i<obj.length;i++)
obj[i] = (i%2==0)?new Object() : obj[i-1];
if(obj[0] == obj[1] & (obj[1]=obj[2])!=null)
System.out.print("1 "); //line 1
if(obj[1] == obj[2] && (obj[2]=obj[0])!=null)
System.out.print("2 "); //line 2
if(obj[1] == obj[0] || (obj[0]=obj[1])==null)
System.out.print("3 "); //line 3
if(obj[2] == obj[0] | (obj[0]=obj[2])!=null)
System.out.print("4 "); //line4
System.out.println((obj[0]==obj[1])+" "+(obj[1]==obj[2])+" "+(obj[0]==obj[2]));
}
}__________________________________________________
What bitwise | and & do in this case?
[ August 27, 2002: Message edited by: Dmitry Golynkin ]
[ August 27, 2002: Message edited by: Dmitry Golynkin ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From JLS 15.22.2 Boolean Logical Operators &, ^, and |

When both operands of a &, ^, or | operator are of type boolean, then the type of the bitwise operator expression is boolean.
For &, the result value is true if both operand values are true; otherwise, the result is false.
For ^, the result value is true if the operand values are different; otherwise, the result is false.
For |, the result value is false if both operand values are false; otherwise, the result is true

Basically, when | or & are used (instead of || or &&) both operands are evaluated.
 
Dmitry Golynkin
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Valentin!
[ August 27, 2002: Message edited by: Dmitry Golynkin ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!