Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question about Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a question about "null"" Watch "a question about "null"" New topic
Author

a question about "null"

Dodo Anakin
Greenhorn

Joined: Jul 19, 2007
Posts: 10
hi , everybody !

I am confused by following code

\\--------------------------------
public static void test(String str){
if(str == null | str.length() == 0){
system.out.println("empty");
}
else
system.out.println("not empty");
}
\\--------------------------------

then I run the method like this

test(null);

the result is : An exception is thrown at runtime


can someone explain ?
Priyam Srivastava
Ranch Hand

Joined: Oct 29, 2006
Posts: 169


this is because in line:: if(str == null | str.length() == 0)--->you are using '|' operator in which both
the operands get evaluted and since str is assigned to null and using '.' operator on null always throws a null pointer exception as::
str.length() is equivalent to null.length()
use '||' in place of '|' and there will be no exception.


"History would be kind to me, for I intend to write it."
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"anakindodoloveme anakindodoloveme,"

Please check your private messages by clicking on the My Profile link near the top of the page.

Thanks,
Marc


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Here is more on logical[shorty circuit] and bitwise operators

[Bold]Logical && {Short circuit}[/Bold]


Here if [Bold] expr1 results in false [/Bold] then the result of the complete expre expr1 && expr2 will always be false, So JVM will not compute expr2.


[Bold]Logical OR {Short circuit}[/Bold]

Here if [Bold] expr1 results in true [/Bold] then the result of the complete expre expr1 || expr2 will always be true, So JVM will not compute expr2.


[Bold]Bitwise And {Non short-circuit][/Bold]

Here if [Bold] expr1 results in false [/Bold] even though the result of the expression expr1 & expr2 will always result in false, JVM will still go ahead and evaluate expr2.


[Bold]Bitwise Or {Non short-circuit][/Bold]

Here if [Bold] expr1 results in true [/Bold] even though the result of the expression expr1 | expr2 will always result in true, JVM will still go ahead and evaluate expr2.


Hope this clears everything.
Thanks
Deepak


SCJP, SCWCD, SCBCD
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Here is more on logical[shorty circuit] and bitwise operators

Logical && {Short circuit}


Here if expr1 results in false then the result of the complete expre expr1 && expr2 will always be false, So JVM will not compute expr2.


Logical OR {Short circuit}

Here if expr1 results in true then the result of the complete expre expr1 || expr2 will always be true, So JVM will not compute expr2.


Bitwise And {Non short-circuit]

Here if expr1 results in false even though the result of the expression expr1 & expr2 will always result in false, JVM will still go ahead and evaluate expr2.


Bitwise Or {Non short-circuit]

Here if expr1 results in true even though the result of the expression expr1 | expr2 will always result in true, JVM will still go ahead and evaluate expr2.


Hope this clears everything.
Thanks
Deepak
Dodo Anakin
Greenhorn

Joined: Jul 19, 2007
Posts: 10
thanks to the nice answer !

but BITWISE OPERATORS ARE NOT ON THE EXAM!

so....
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by dodoloveme:
BITWISE OPERATORS ARE NOT ON THE EXAM!
That is not correct. In conjunction with booleans, |, & and ^ are simple logical operators.

What you won't find in the SCJP 5 Exam are bitwise operations. Something like this:
[ July 29, 2007: Message edited by: Manfred Klug ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a question about "null"