class A { static String a; public static void main(String[] args)
{ System.out.println("A1a is:" + a==null); }
*** All is prints is false. It does not print 'A1a is:' WHy is this? If I do... System.out.print("A2a is:" ); System.out.println(a==null); It prints..
'A2a is:true' that is what I expected, but the first one I am still confused about.
Anthony Villanueva
Ranch Hand
Joined: Mar 22, 2002
Posts: 1055
posted
0
Originally posted by Anthony Smith: All is prints is false. It does not print 'A1a is:' WHy is this?
Operator precedence. Your code as is checked if the String "A1a is:" + a is null. Try using System.out.println("A1a is:" + (a==null)); instead.
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
Because it is doing this: ("A1a is:" + a) == null try changing your code to this: "A1a is:" + (a==null) You have to remember the order that operators are invoked.
Ha Ha, I am going to become the precedence king.I just now answered one question(Question about multiple assignments) regarding the precdence and yet again: System.out.println("A1a is:" + (a==null)); Before that + has higher precedence than == so it concates a with "A1a is :" and then checks for null which is false.If you put the backet then it will work fine. Got it, Regards, Jana
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
posted
0
Now, wouldn't a "precedence king" post replies to questions preceding all other replies?