confusion in if condition
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
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...)
liqiang yang
Ranch Hand
Posts: 92
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
thanks Freddy.
But why the right answer for above code is:
Prints: 1 2 4 false false true
But why the right answer for above code is:
Prints: 1 2 4 false false true
DY.
SCJP 5.0 (100%), SCWCD 5.0 (79%), SCBCD 5.0 (preparing...)
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Can someone explain this code please?
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
Posts: 92
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
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?
DY.
SCJP 5.0 (100%), SCWCD 5.0 (79%), SCBCD 5.0 (preparing...)
abhrodip paul
Greenhorn
Posts: 27
posted 17 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
| How do they get the deer to cross at the signs? Or to read this tiny ad? Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |







