File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confusion in if condition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confusion in if condition" Watch "confusion in if condition" New topic
Author

confusion in if condition

liqiang yang
Ranch Hand

Joined: Jan 20, 2008
Posts: 92
Hi, all

I got a confusing question:


How deal with this baby? "if(obj[0] == obj[1] & (obj[1]=obj[2])!=null)"
What's the result after compiler interpret "(obj[1]=obj[2])"?
I think this is just an assignment statement. Anyone can explain a little bit?



[BSouther: Added UBB CODE tags]
[ January 30, 2008: Message edited by: Ben Souther ]

DY.
SCJP 5.0 (100%), SCWCD 5.0 (79%), SCBCD 5.0 (preparing...)
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

It basically means you assign obj[2] reference to obj[1] and if obj[2] is null , obj[1] will be null. Thus, it's false.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
liqiang yang
Ranch Hand

Joined: Jan 20, 2008
Posts: 92
thanks Freddy.
But why the right answer for above code is:

Prints: 1 2 4 false false true
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129
Can someone explain this code please?
abhrodip paul
Greenhorn

Joined: Oct 16, 2007
Posts: 27
1: public class Question41 {
2:public static void main(String[] args) {
3:Object[] obj = new Object[3];
4for (int i = 0; i < obj.length; i++)
5obj[i] = (i % 2 == 0) ? new Object() : obj[i - 1];
6if (obj[0] == obj[1] & (obj[1] = obj[2]) != null)
7System.out.print("1 ");
8if (obj[1] == obj[2] && (obj[2] = obj[0]) != null)
9System.out.print("2 ");
10if (obj[1] == obj[0] || (obj[0] = obj[1]) == null)
11System.out.print("3 ");
12if (obj[2] == obj[0] | (obj[0] = obj[2]) != null)
13System.out.print("4 ");
14System.out.println((obj[0] == obj[1]) + " " + (obj[1] == obj[2]) + " "
+ (obj[0] == obj[2]));
}
}


in line 4 and 5 all oblect will created
obj[0] and obj[1] refer to same object say obj1 in Heap
obj[2] refer to another object say obj2 in Heap

in line 6 both condition will be get evaluated even first is false it is both for & and |

here in line 6 obj[0] == obj[1] is true and now obj[1] will refer to obj2

in line 7 it will print 1

in line 8 obj[1] == obj[2] will be true and obj[2] will refer obj1

in line 9 will print 2

in line 10 obj[1] == obj[0] willbe false or obj[0] will refer obj2 and is not null so return false
as both are false 3 will not get printed

in line 12 obj[2] == obj[0] is true and for | operator it will evaluate the next condition that is
(obj[0] = obj[2]) != null) and obj[0] will refer obj1

in line 13 will print 4

so now obj[0] and obj[2] refering to obj1
and obj[1] refering to obj2

so
obj[0] == obj[1] will false
obj[1] == obj[2] will false
obj[0] == obj[2]) will true



So the answer is right , hey I know tis it little confusing but try to draw and solve it is not a tough at all
liqiang yang
Ranch Hand

Joined: Jan 20, 2008
Posts: 92
Thanks abhrodip!

line 4 and 5 all object will created
obj[0] and obj[1] refer to same object say obj1 in Heap
obj[2] refer to another object say obj2 in Heap

My confusing point is:
obj[0], obj[1] and obj[2] are created by "new" keyword
plus Object's constructor and Object array elements should be initialized with "null". So obj[0]=obj[1]=obj[2]=null

I don't know what's wrong with it?
abhrodip paul
Greenhorn

Joined: Oct 16, 2007
Posts: 27
Here Object array is created not the Actual Object.

Object [] obj = new Object[3];
will create three obj instance which are pointing towords a location on heap that is null.

after that in for loop each instance obj[0],obj[1],obj[2] will be initialized with Objects on heap.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confusion in if condition