• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

a question about "null"

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to the nice answer !

but BITWISE OPERATORS ARE NOT ON THE EXAM!

so....
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic