Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

confusion in if condition

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
liqiang yang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Freddy.
But why the right answer for above code is:

Prints: 1 2 4 false false true
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain this code please?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic