d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Help!!!
Add Reply New Topic New Poll
Member
Posts: 2,020
Joined: Apr 30 2011
Gold: 143.00
Oct 11 2012 08:25pm
Got this from my teacher in Advanced Sections for java. Really having trouble understanding this. Can anyone help please?
Code
Java has a selection operator of the form

condition? value1 : value2

The value of that expression is either value1 if the condition is true or value2 if it is false. For example, we can compute the absolute value as

y = x >= 0 ? x : -x;

which is a convenient shorthand for

if (x >= 0)
y = x;
else
y = -x;

The selection operator is similar to the if/else statement, but it works on a different syntactical level. The selection operator combines values and yields another value. The if/else statement combines statements and yields another statement.

For example, it would be an error to write

y = if (x > 0) x; else -x; // Error

The if/else construct is a statement, not a value, and you cannot assign it to a variable.

We don't use the selection operator in this book, but it is a convenient and legitimate construct that you will find in many Java programs.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 11 2012 08:33pm
i call it the ternary operation instead of "selection operator" but what's the problem?

return (booleancondition ? return_something_if_true : return_something_if_false);

must be same datatype.

think of it as the nvl operator in oracle. lets pretend for a moment you want to deal with string functions. what happens if you get a null somewhere? then when you invoke the method, you'll get NullPointerException. so you want to use "" instead of null

public static String nvl(String originalValue, String ifNullValue){
if (value == null)
return ifNullValue;
else
return originalValue;
}

same thing as:
public static String nvl(String originalValue, String ifNullValue){
return originalValue == null ? ifNullValue : originalValue;
}

use-age:
int getLengthOfString(String s){
return nvl(s).length();
}

normally if s is null, you get NPE. to avoid it you use the nvl function

This post was edited by carteblanche on Oct 11 2012 08:34pm
Member
Posts: 2,020
Joined: Apr 30 2011
Gold: 143.00
Oct 11 2012 09:06pm
Quote (carteblanche @ Oct 11 2012 09:33pm)
i call it the ternary operation instead of "selection operator" but what's the problem?

return (booleancondition ? return_something_if_true : return_something_if_false);

must be same datatype.

think of it as the nvl operator in oracle. lets pretend for a moment you want to deal with string functions. what happens if you get a null somewhere? then when you invoke the method, you'll get NullPointerException. so you want to use "" instead of null

public static String nvl(String originalValue, String ifNullValue){
    if (value == null)
        return ifNullValue;
    else
        return originalValue;
}

same thing as:
public static String nvl(String originalValue, String ifNullValue){
return originalValue == null ? ifNullValue : originalValue;
}

use-age:
int getLengthOfString(String s){
return nvl(s).length();
}

normally if s is null, you get NPE. to avoid it you use the nvl function


Thanks. Actually clarified exactly what I was having a problem with. Thanks. Vouch.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll