NVL IsNull equivilent in Java? (Null handling)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DiggidyMack69

    NVL IsNull equivilent in Java? (Null handling)

    Hello all,
    I was wondering if there a method/function etc that is simular to
    Oracle's Nvl function. The funtion checks a parameter and if it is
    null then is will output a given alternative. i.e. nvl(parameter1,
    value1) if parameter1 is null then send value1.
    I have a servlet with the following snippet of code that I'd like to
    streamline as this is one of many checks to be made...ideas?


    if(request.getP arameter("idcol ")!= null)
    {
    int idcol = Integer.parseIn t(request.getPa rameter("idcol" ).trim());
    }else{
    int idcol = 0;
    }
  • Steve Lieberman

    #2
    Re: NVL IsNull equivilent in Java? (Null handling)

    DiggidyMack69@h otmail.com (DiggidyMack69) wrote in message news:<c86ce4f.0 308111305.3b776 f40@posting.goo gle.com>...[color=blue]
    > Hello all,
    > I was wondering if there a method/function etc that is simular to
    > Oracle's Nvl function. The funtion checks a parameter and if it is
    > null then is will output a given alternative. i.e. nvl(parameter1,
    > value1) if parameter1 is null then send value1.[/color]

    There's nothing directly equivalent to that function. However, there are
    two alternatives:

    (1) Using the tertiary operator (also used in C/C++)

    The expression: <condition> ? <a> : <b>
    evaluates to <a> if <condition> is true or <b> if condition is
    false.

    Your snippet could be rewritten as:
    int idcol = (request.getPar ameter("idcol") != null) ?
    Integer.parseIn t(request.getPa rameter("idcol" ).trim()) : 0;

    (2) Writing the function you're looking for yourself and putting it in
    some static function in a utility class.

    Steve

    Comment

    • DiggidyMack69

      #3
      Re: NVL IsNull equivilent in Java? (Null handling)

      sl@digilevel.co m (Steve Lieberman) wrote in message news:<beef3a29. 0308120855.2953 f1e1@posting.go ogle.com>...[color=blue]
      > DiggidyMack69@h otmail.com (DiggidyMack69) wrote in message news:<c86ce4f.0 308111305.3b776 f40@posting.goo gle.com>...[color=green]
      > > Hello all,
      > > I was wondering if there a method/function etc that is simular to
      > > Oracle's Nvl function. The funtion checks a parameter and if it is
      > > null then is will output a given alternative. i.e. nvl(parameter1,
      > > value1) if parameter1 is null then send value1.[/color]
      >
      > There's nothing directly equivalent to that function. However, there are
      > two alternatives:
      >
      > (1) Using the tertiary operator (also used in C/C++)
      >
      > The expression: <condition> ? <a> : <b>
      > evaluates to <a> if <condition> is true or <b> if condition is
      > false.
      >
      > Your snippet could be rewritten as:
      > int idcol = (request.getPar ameter("idcol") != null) ?
      > Integer.parseIn t(request.getPa rameter("idcol" ).trim()) : 0;
      >
      > (2) Writing the function you're looking for yourself and putting it in
      > some static function in a utility class.
      >
      > Steve[/color]


      Much cleaner, thanks!!

      Comment

      Working...