0

How can I change

if(xmlComboBoxValues.get(0) == null){
    cstmt.setNull(i++,java.sql.Types.NVARCHAR); 
}
else {  
    cstmt.setString(i++, (String) xmlComboBoxValues.get(0));            
}

as a ? : expressing in java?

Here is what I have but the syntax is obviously wrong.

xmlComboBoxValues.get(0) == (null) ? cstmt.setNull(i++,java.sql.Types.NVARCHAR) : cstmt.setNull(i++,java.sql.Types.NVARCHAR);
6
  • 1
    Why do you need this? Feels like it would hugely sacrifice readability. Commented Nov 4, 2014 at 6:50
  • remove the brackets around null xmlComboBoxValues.get(0) == nulland it should be ok. Commented Nov 4, 2014 at 6:50
  • 1
    Expressions in the ternary operator must result in the same type and it cannot be a void method call, so you can't do that. Commented Nov 4, 2014 at 6:52
  • This question appears to be off-topic because it is asking for the impossible. Commented Nov 4, 2014 at 6:53
  • @EJP Asking for the impossible in itself shoudln't be off-topic. The "It is not possible because..." is a valid answer. Commented Nov 4, 2014 at 6:55

2 Answers 2

5

You can't do that for two reasons:

  • The methods have a void return type
  • You can't use a conditional expression as a statement

These are both symptoms of the same cause: you're misusing the operator. The purpose of the operator is to choose which of two expressions to use as the result of the overall expression... which is then used for something else. Computing an expression has slightly different aim from executing a statement.

Your original code is already idiomatic: if a condition is true, you want to execute one statement. Otherwise, you want to execute a different statement. Perfect for if/else.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank Jon, I have 10 comboboxes which act as filters for a table. Having an if/else for each combobox makes my code looks bulky so I thought of trying to put all on one line using the ? : operator.
@RicoStrydom: Sounds like you should put your combo boxes in a collection, the you only need to have that code once, in a loop. Alternatively, if you can't do that, extract the common code into a method you call for each of your combo boxes.
3

You can't do that with Ternary operator in Java since setNull() is void method.

From §JLS.15.25:

ConditionalExpression:
ConditionalOrExpression
ConditionalOrExpression ? Expression : ConditionalExpression

The conditional operator is syntactically right-associative (it groups right-to-left). Thus, a?b:c?d:e?f:g means the same as a?b:(c?d:(e?f:g)).

The conditional operator has three operand expressions. ? appears between the first and second expressions, and : appears between the second and third expressions.

The first expression must be of type boolean or Boolean, or a compile-time error occurs.

It is a compile-time error for either the second or the third operand expression to be an invocation of a void method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.