This question already has answers here:
Ternary Operators Java [duplicate]
(7 answers)
Closed 6 years ago.
Can someone please explain, in simple English, the logic behind this statement?
return mContainsLoadingRow ? (getContentDataSize() + 1) : getContentDataSize();
Assuming mContainsLoadingRow is a boolean, if mContainsLoadingRow is true,
then return getContentDataSize() + 1.
If not, return getContentDataSize().
Is that the correct way to look at this?
this complete expression is know as Ternary Operator in Java.
Code Statement
mContainsLoadingRow ? (getContentDataSize() + 1) : getContentDataSize();
|| || ||
//boolean expression //return if true //return if false
here in this code
mContainsLoadingRow is a Boolean variable which contains either true or false. you can also change mContainsLoadingRow with any Boolean expression like (a>b or b==a or b <= a etc.)
? (question mark) :- enables us to fine whether it is true or false.
if true the expression (getContentDataSize() + 1) will be return.
if false then expressin getContentDataSize() value will be return.
int x = 0;
if (0 < 1){
x = 2;
}else{
x = 42;
}
// in short:
x = (0<1) ? 2 : 42;
So yes, you are right
Related
This question already has answers here:
Why boolean in Java takes only true or false? Why not 1 or 0 also?
(8 answers)
Closed 1 year ago.
In C++, 1 is treated as true and 0 is treated as false. So, internal conversion is possible in C++. But can this be possible in Java? Can we convert int to boolean or boolean into int as per our need in Java?
For Example, for this problem on GeeksForGeeks, the C++ code might look like -
class Solution {
public:
int findPosition(int N) {
if((N & (N -1)) or (N == 0)) return -1;
return log2(N) + 1;
}
};
How can write the same type of solution in Java?
Thank You.
C++ implicitly converts int to boolean. In Java, you will have to do that explicitly by yourself.
To convert an int to boolean, just check whether it's not zero like this: N != 0
So you have to modify your original expression to something like this:
if (((N & (N - 1)) != 0) || (N == 0))
Vice versa, to convert a boolean to an int, do something like this: int i = boolValue ? 1 : 0;
This question already has answers here:
What is the Java ?: operator called and what does it do?
(17 answers)
Closed 6 years ago.
newTilt = (newTilt > 90) ? 90 : newTilt;
What does this line of code mean? It's the first time that I see something like this in Android.
This is the full method that contains the line above:
public void onTiltMore(View view) {
if (!checkReady()) {
return;
}
CameraPosition currentCameraPosition = mMap.getCameraPosition();
float currentTilt = currentCameraPosition.tilt;
float newTilt = currentTilt + 10;
newTilt = (newTilt > 90) ? 90 : newTilt;
CameraPosition cameraPosition = new CameraPosition.Builder(currentCameraPosition)
.tilt(newTilt).build();
changeCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
It is a ternary expression. It says that if the newTilt at that point is above 90, it sets it to 90, otherwise it leaves it unchanged. You could kind of think of it in the way of an if-else statement.
if(newTilt > 90) {
newTilt = 90;
} else {
newtilt = newTilt; // which does nothing useful
}
This expression means that
newTilt = (newTilt > 90) ? 90 : newTilt;
is an expression which returns one of two values, 90 or newTilt. The condition, (newTilt > 90), is tested. If it is true the first value, 90 , is returned. If it is false, the second value, newTilt, is returned. Whichever value is returned is dependent on the conditional test, (newTilt > 90)
It's as someone has already said, it's called a ternary expression. It's something that you should really learn as I've seen it appear on quite a few interview questions. What this expression does, is if the expression evaluates to true then the left-side of the colon is used as the result and if it evaluates to false, then the right side is used. It's very often used as a shortcut to an if/else statement. In fact, you could write the above like this:
if (newTilt > 90)
newTilt = 90;
else
newTilt = newTilt;
Obviously, you don't need the else statement above, as newTilt doesn't change if it's less than 90. I'm just trying to show you how the ternary works.
In fact, a ternary is still a conditional expression.
int var = (expression) ? true value : false value;
Since the false condition actually does nothing, the above ternary would be better written as simply:
if (newTilt > 90)
newTilt = 90;
That's all you need.
This question already has answers here:
How to make loop infinite with "x <= y && x >= y && x != y"?
(4 answers)
Closed 7 years ago.
I had this question in my Java test where I had to assign values to a and b so this expression evaluates to true:
(a<=b && b<=a && a!=b)
Sadly, I had no idea what the answer was.
There's a simple trick here.
You cannot think this through with boolean logic only. Using that, this combination...
a is less than or equal to b, and
b is less than or equal to a, and
a is not equal to b
...would never return true.
However, the != operator compares references if its operands are objects.
So, the following will return true:
Integer a = 1;
Integer b = new Integer(1);
System.out.println(a<=b && b<=a && a!=b);
What happens here is: a as an object reference is not equal to b as an object reference, although of course they hold equal integer values.
This question already has answers here:
What is a Question Mark "?" and Colon ":" Operator Used for? [duplicate]
(7 answers)
Closed 8 years ago.
What does the following line do? Could someone help me write this line in "normal" code?
int change = (Math.random() - 0.5 < 0 ? -5 : 5);
This is a ternary operator the way it works is :
condition ? (things to do if true) : (things to do if false);
In your code what it does is :
if value of Math.random() - 0.5 < 0
then assign change a values of -5
else
assign change a value of 5.
This line takes a random number (between 0 and 1) and subtracts 0.5. If that value is less than 0 then change is set to -5, otherwise 5.
int change;
if((Math.random() - 0.5) < 0)
{
change=-5;
}
else
{
change=5;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the Java ?: operator called and what does it do?
In some code a ? is used to perform a mathematical equation.
What is it and how do you use it? Is it possible to provide an example and the reason for the final answer of an equation?
int count = getChildCount();
int top = count > 0 ? getChildAt(0).getTop() : 0;
Basically is the ternary operator:
String mood = (isHappy == true)?"I'm Happy!":"I'm Sad!";
if isHappy, then "I'm Happy!". "I'm Sad!" otherwise.
I presume you mean something like x = () ? y : z; notation? If that's the case, then the expression within the parentheses is evaluated as a boolean, if true x = y otherwise x = z
int count = getChildCount();
int top = count > 0 ? getChildAt(0).getTop() : 0;
Means that the top variable will contain the value of getChildAt(0).getTop() if the count variable is greater than 0, else it will equal to 0
My guess is you are referring to the ternary operator, which is used like this:
<some condition> ? <some value> : <some other value>;
For example:
int max = a > b ? a : b;
It's a shorthand for an if and is equivalent to:
int max;
if (a > b) {
max = a;
} else {
max = b;
}
but allows a one-line result in code.
When used well, it can make code much more clear due to its terseness. However caution is advised if the line becomes too long or complicated: The code only remains readable when the terms are brief.
The ? in an evaluative expression is called the ternary operator. It is essentially short-hand for an if() ... else block.
http://en.wikipedia.org/wiki/%3F:
I assume you're referring to the ternary operator. It's shorthand for certain kinds of if statements. Where you're making an assignment, like:
int dozen = (bakersDozen) ? 13 : 12;
Assuming bakersDozen is true, then dozen will be 13. If it's false, it will be 12.
int result = (a > b) ? 1 : 0;
is the same than
int result;
if (a > b)
result = 1;
else
result = 0;
? Is usually a ternary (or tertiary) operator. So let's explain what it is doing.
myValue = (a = b) ? 1 : 0;
The first part is your condition. "Does a equal b?"
The second part is the true response.
The third part is the false response.
So if a is equal to b, myValue will be 1. If a is not equal to b, myValue will be 0.
See http://en.wikipedia.org/wiki/Ternary_operation