Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Code Question

Options
  • 06-02-2007 8:20pm
    #1
    Registered Users Posts: 7,541 ✭✭✭


    I know this is prob the easiest question to answer ever but what does the following code snippet do?

    public MultipartRequest getRequest() {
    return request==null ? null : request;
    }

    I've no idea what the ? and : do. Cheers.


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    If the request result is equal to null, it will return null. Otherwise it will return the request.

    AFAIR, ? is the conditional operator, and each side of the : is true and false respectively.


  • Registered Users Posts: 7,541 ✭✭✭irlrobins


    Cheers, never seen that used before in Java.


  • Closed Accounts Posts: 461 ✭✭markf909


    The ? is a ternary operator and is available in a few other languages besides Java.

    The syntax can be described as follows

    <boolean expr> ? <if true then return first option> : <else return second option>

    so in the case of your code fragment

    The getRequest method will return null if the the MultipartRequest instance (request) is null otherwise it will return request.


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    the best example i can give is this
    if (a > b)
    {
      max = a;
    }
    else
    {
      max = b;
    }
    
    max = (a > b) ? a : b;
    

    both are the exact same.
    the ? and : are a fancy looking if/else block.

    so in your code it will test to see if the request is null, if it is, it returns null, if not it will return request.

    *edit* beaten to it :mad: :D


  • Registered Users Posts: 5,335 ✭✭✭Cake Fiend


    Couldn't you skip that and just return request?
    If it's null, the method will return null, if request has a value, it'll return that value...?

    (not a Java programmer, just browsing, so don't murder me if I'm missing something silly :))


  • Advertisement
  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Cake Fiend wrote:
    Couldn't you skip that and just return request?
    If it's null, the method will return null, if request has a value, it'll return that value...?

    (not a Java programmer, just browsing, so don't murder me if I'm missing something silly :))
    Null is a special case in Java like in many things.

    As best I remember, if you attempt to return an object (in this case request) that's null, you'll receive an error along the lines of "Object not initialised" or "Object is null". However, if you just return null, all on it's own, then that's not exactly the same thing. Null is not an actual value, so to say that
    Object myObject = null;
    Isn't the same as saying that the "value" of myObject is null.

    I'm also suspicious of the statement

    request==null

    in the above code. Is there not an isNull() method or something like that?


  • Closed Accounts Posts: 461 ✭✭markf909


    I'm also suspicious of the statement

    request==null

    in the above code. Is there not an isNull() method or something like that?

    No that == would be fine for a comparison with null for the simple reason:

    If request is null then a call to request.isNull() ( or more specifically in Java request.equals(null) ) would trigger a NullPointerException.


  • Registered Users Posts: 6,316 ✭✭✭OfflerCrocGod


    It still seems fairly pointless code as you will still need to test for the Null value being returned so you may as well return the object and test it wherever the getter is called. I think a better method would be isRequestNull() tbh and just have the getter being a getter.


  • Registered Users Posts: 7,541 ✭✭✭irlrobins


    I didn't write the code. I just was trying to understand what it was doing. ;)


  • Registered Users Posts: 37,485 ✭✭✭✭Khannie


    Good ol' ternary operator...confusing people since 1967.

    I love it myself, but I wouldn't use it in code that others will be reading. It offers no performance advantage over an if / else block anyway.


  • Advertisement
  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    I've seen it in C# and JavaScript and love it too. Although I try to avoid it for the same reasons as Khannie.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Khannie wrote:
    Good ol' ternary operator...confusing people since 1967.

    I love it myself, but I wouldn't use it in code that others will be reading. It offers no performance advantage over an if / else block anyway.

    I love it in PHP scripts. At the start of the script, you may have 30 variables which you're expecting as input from the user, or if it hasn't been input, you want a default value.

    Scripts I write have a shedload of lines at the top all similar to;

    $someVar = (isset($_GET))? $_GET : -1;

    If I wrote all of these out in if...else blocks, I personally think it would be far less readable than ternary statements.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    I do find it very readable myself but you have to get used to it I suppose.


  • Registered Users Posts: 37,485 ✭✭✭✭Khannie


    seamus wrote:
    If I wrote all of these out in if...else blocks, I personally think it would be far less readable than ternary statements.

    Yeah, I agree. I think though, once you like it and understand it with a glance it's hard to abstract yourself to the point where you can consider if / else's more elegant looking.


Advertisement