• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question about "null"

 
Dodo Anakin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"anakindodoloveme anakindodoloveme,"

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

Thanks,
Marc
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to the nice answer !

but BITWISE OPERATORS ARE NOT ON THE EXAM!

so....
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic