3

here is my code

public class BinarySearch { public static int binsearch(int key, int[] a) { int lo = 0; int hi = a.length - 1; while (lo < hi) { int mid = (lo + hi) >> 1; key < a[mid] ? hi = mid : lo = (mid + 1); } return lo--; } } 

i got an error when compiling

Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error on tokens, Expression expected instead Syntax error on token "]", delete this token Syntax error, insert "]" to complete Expression 

and if i change '<' to '>' as

key > a[mid] ? hi = mid : lo = (mid + 1); 

got a total different error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error on token ">", -> expected 

I am really confused about the ternary operator usage in java. after all, this code works fine in c++

4
  • could you first break your code out into non-ternary if statements? I assure you the ternary works in java, but what you have written here is not java code. Commented Jun 19, 2015 at 17:59
  • That’s not specific to the ternary operator. In Java, unlike C, you can not place expressions where statements are expected. Commented Jun 19, 2015 at 18:05
  • 1
    "this code works fine in c++" This is because C++ is a lot more relaxed about statement-expressions. Java, on the other hand, allows only direct and compound assignment expressions to be used as statements. Commented Jun 19, 2015 at 18:17
  • pretty sure this binsearch doesn't work fine even in C++ Commented Jun 19, 2015 at 18:48

4 Answers 4

11

The compiler is having hard time parsing your expression because it is used like a statement-expression.

Since ternary operator is an expression, it should not* be used in place of a statement. Since you would like to control the assignment, which is a statement, with the condition, you should use a regular if:

if (key < a[mid]) { hi = mid; } else { lo = (mid + 1); ) 

* In fact, Java does not allow ternary expressions to be used as statements. You could work around this issue by wrapping your expression in an assignment or an initialization (see demo), but this would result in code that is hard to read and understand, so it should be avoided.

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

Comments

1

Ternary operator in java works only like this:

x=(a>b?a:b); 

This operation puts the larger of a and b
This is not allowed:

a>b?x=a:x=b; 

It maybe looks similar, but ternary operator branches in java can contain only value, not assigment


EDIT:
In your case I advice to use if statement

Comments

1

In this case you should use an if statement.

if(key > a[mid]) hi = mid; else lo = mid + 1; 

This is because the ternary operator is used for when you're setting a variable. For example:

foo = (firstVariable > secondVariable) ? 1 : 0; 

(something along those lines).

Comments

0

From the documentation:

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.

In other words, the conditional operator cannot be used to do something like this*:

key < a[mid] ? foobar1() : foobar2(); 
  • There is a workaround using reflection but at the end of the day, code succinctness does not trump readability.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.