aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on operators Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Doubt on operators" Watch "Doubt on operators" New topic
Author

Doubt on operators

Venkat KG
Greenhorn

Joined: Nov 13, 2005
Posts: 10
Hi,

Can any one help out in this prog.I really cud not undersatnd what is happening in Line 1 and Line 2 .Why the o/p is printing as 122.

public static void main(String argv[]){
Test1 td = new Test1();
td.samcov();
}
public void samcov(){
int i=1;
int j=2;
if((i==20) && (j==(i=i*2))){ //Line1
}

System.out.print(i);
if((i==20) & (j==(i=i*2))){} // Line 2
System.out.print(i);

int x = i & 2;
System.out.print(x);
}


}
shaikkhadar basha
Greenhorn

Joined: Nov 22, 2005
Posts: 2
In Line1 && operator is used. In this case since LHS expression evaluates to false, RHS expression doesn't evaluate.
In Line2 & operator is used. In this case both LHS,RHS expression gets evaluated always.
So RHS j==(i=i*2)) assigns 2(i=i*2) to i and j.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on operators
 
Similar Threads
diamond pattern without parse.int (Someone help me T.T) :cry:
Static variable initialization...
Can someone please explain this problem?
an int outside a for loop, which is printed after the for loop
operators