Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Majji's mock exam doubt

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

How the following code is giving true as output...

int i=10;
int j=10;
boolean b=false;
if(b=i==j)
System.out.println("true");
else
System.out.println("false");

as conditional operators have higer precedence i==j will be evaluated and returns true ,but b is defined as false,so
if(b=....) returns false ..please can anybody help me..thanks in advance
regds
krishna
 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by krishna:
Hi,

How the following code is giving true as output...

int i=10;
int j=10;
boolean b=false;
if(b=i==j)
System.out.println("true");
else
System.out.println("false");

as conditional operators have higer precedence i==j will be evaluated and returns true ,but b is defined as false,so
if(b=....) returns false ..please can anybody help me..thanks in advance
regds
krishna

since i==j returns true, that is assigned to b. Assignment b=true returns true,and hence the answer
Savithri
[This message has been edited by Savithri Devaraj (edited August 17, 2000).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic