ternary-operator

Why don't static member variables play well with the ternary operator?

一世执手 提交于 2019-11-30 08:29:01
Here's the deal. I have a static class which contains several static functions used for getting input. The class contains a private static member variable for indicating whether the user entered any information. Each input method checks to see whether the user entered any information, and sets the status variable accordingly. I think this would be a good time to use the ternary operator. Unfortunately, I can't, because the compiler doesn't like that. I replicated the problem, then simplified my code as much as was possible to make it easy to understand. This is not my original code. Here's my

Ternary operator associativity in C# - can I rely on it?

ε祈祈猫儿з 提交于 2019-11-30 08:18:26
Ahh, don't you just love a good ternary abuse? :) Consider the following expression: true ? true : true ? false : false For those of you who are now utterly perplexed, I can tell you that this evaluates to true . In other words, it's equivalent to this: true ? true : (true ? false : false) But is this reliable? Can I be certain that under some circumstances it won't come to this: (true ? true : true) ? false : false Some might say - well, just add parenthesis then or don't use it altogether - after all, it's a well known fact that ternary operators are evil! Sure they are, but there are some

Java - ternary operator weird behaviour

被刻印的时光 ゝ 提交于 2019-11-30 08:05:02
I was trying to remove the fractional part from a double in case it is whole using: (d % 1) == 0 ? d.intValue() : d And encountered the following behavior which i don't understand: public static void main(String[] args) { Double d = 5D; System.out.println((d % 1) == 0); // true System.out.println((d % 1) == 0 ? d.intValue() : "not whole"); // 5 System.out.println((d % 1) == 0 ? d.intValue() : d); // 5.0 } As you can see on the third line, the operator chooses the else value - 5.0 even though the condition (d % 1) == 0 is met. What's going on here? The return type of the ternary conditional

Remembering the Ternary Operator Syntax

谁都会走 提交于 2019-11-30 04:47:48
问题 Anyone have a good trick to remember the standard ternary syntax? Specifically whether the '?' or ':' comes first. I have consistently gotten this backwards over the years. 回答1: The condition you are checking is kind of like a question, so the question mark comes first. x > 0 ? 1 : 0 Think of this statement as three English sentences: "Is x greater than 0? Then 1. Else, 0." You have one sentence for each clause in the statement. The predicate: x > 0 ? /* Is x greater than 0? */ The "true"

Why doesn't the ternary operator like generic types with bounded wildcards?

可紊 提交于 2019-11-30 04:44:49
The following class defines two methods, both of which intuitively have the same functionality. Each function is called with two lists of type List<? super Integer> and a boolean value which specifies which of those lists should be assigned to a local variable. import java.util.List; class Example { void chooseList1(boolean choice, List<? super Integer> list1, List<? super Integer> list2) { List<? super Integer> list; if (choice) list = list1; else list = list2; } void chooseList2(boolean choice, List<? super Integer> list1, List<? super Integer> list2) { List<? super Integer> list = choice ?

Confusion in ternary operator and typecasting

家住魔仙堡 提交于 2019-11-30 03:59:41
问题 I gone through this question - why the result of : 1 ? (int *)0 : (void *)0 differs to the result of : 1 ? (int *)0 : (void *)1 How it is differ ? It should be 0 or (int*)0 . How to check the result ? Where we can use such type of expression ? 回答1: The only difference is in types: the first one returns an int * , the second one returns a void * . From C11 standard, §6.5.15 Conditional operator , ¶6: If both the second and third operands are pointers or one is a null pointer constant and the

How to turn if, else if logic into a ternary operator?

爱⌒轻易说出口 提交于 2019-11-30 03:46:33
I was just wondering if this was possible because i started using ternary operators to reduce lines of code and i am loving it. if (x==y) { z += x; } else if (x==z) { z += y; } else { z += 1; } i can do this now if there is only one if statement like this: z = x == y ? z += x : z += 1; It would be like this: z = x == y ? z + x : x == z ? z + y : z + 1; If you use z += x as an operand it will end up doing z = (z += x) . While it works in this special case, as the result of the expression z += x is the final value of z , it may not work in other cases. Howver, as all operations have the z += in

Are there any good reasons why ternaries in C# are limited?

旧街凉风 提交于 2019-11-30 03:09:14
问题 Fails: object o = ((1==2) ? 1 : "test"); Succeeds: object o; if (1 == 2) { o = 1; } else { o = "test"; } The error in the first statement is: Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and 'string'. Why does there need to be though, I'm assigning those values to a variable of type object. Edit: The example above is trivial, yes, but there are examples where this would be quite helpful: int? subscriptionID; // comes in as a

Ternary operator, syntax error when using assignment

混江龙づ霸主 提交于 2019-11-30 02:58:25
The following 3 lines of code below compile OK. (Please note that this code is an example of "artificial Java coding", and consequently wouldn't be seen in professionally written code.) int x, y; boolean b=true; x = b ? y=1 : 2; // Compiles OK. If I now change the code in line #3 above, so that it looks like the following code line below, the compiler generates an error. // Change the position of the "y assignment", and now the code doesn't compile. x = b ? 1 : y=2; Here is the syntax error message: Can someone please explain this behaviour (to a rookie Java learner)? Thank you. Short : This

Setting parameter to DBNull.Value using ternary syntax gives error?

十年热恋 提交于 2019-11-30 00:48:32
问题 I have the following bit of code to set a parameter that will be used in an INSERT statement to set a VARCHAR column in a SQL Server database. My value object (named ilo) has a property called Description that gets initialized to String.Empty, and then either gets set to some value read from XML, or if that XML element is empty it just stays as String.Empty. So when inserting into the database, if the property is still set to String.Empty, I'd like to have it insert a null value. database