Question

I've been given some java code which contains a <> operator. What is the function of this operator?.

if(x <> 0) {
    doSomething();
}

I've never seen it before and its not on the operators page of http://docs.oracle.com/
I'd have put it down to a typo but it shows up twice in the same piece of code.

Was it helpful?

Solution 3

It's most likely an error the programmer missed, bc some ide's will automatically add the greater than sign when you type the less than sign first.

e.g. eclipse will convert < to <>, you just got to remember to delete the second operand.

this operator <> is actually a not equal to (!=) operator in other languages, like sql for example.

lastly like everybody else said, this <> expression is for generics in java.

OTHER TIPS

if (x <> 0) {
    doSomething();
}

This is not valid operator1 in Java2. So it won't compile. If you want to use non-equal operator you have to use != operator:

if (x != 0) { // this is valid use in Java
    doSomething();
}

Here you can find list of all valid operators in Java.

Example of usage <> symbol in generics:

A generic class is defined with the following format: class name<T1, T2, ..., Tn> { ... }

Here, you can see "your symbol":

The type parameter section, delimited by angle brackets (<>), follows the class name. It specifies the type parameters (also called type variables) T1, T2, ..., and Tn.

For more information you need to have look at Java generics. Hope this will make things clearer.

1Symbol <> is used in Java only while instantiating generic type.

2Symbol <> is valid for example in SQL language. Look at SQL operators.

This is not valid Java code, it will cause a compilation error:

if (x <> 0)

The correct way to express the above is:

if (x != 0)

The only part where the diamond operator is allowed is when instantiating a generic type, and only since Java 7. For instance:

List<Integer> numbers = new ArrayList<>();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top