aspose file tools*
The moose likes Beginning Java and the fly likes null question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "null question" Watch "null question" New topic
Author

null question

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
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
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
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Eureka Jana
Greenhorn

Joined: Sep 04, 2002
Posts: 18
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
Now, wouldn't a "precedence king" post replies to questions preceding all other replies?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Eureka Jana
Greenhorn

Joined: Sep 04, 2002
Posts: 18
Hummmmmmm.....
Oh ho Actually I missed that by time constraint.I had a work here after I tried to reply.So I missed the train.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
3 replies within 6 minutes that all agree. Not bad.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null question