Maybe this question has been answered before, but the word if occurs so often it\'s hard to find it.
The example doesn\'t make sense (the expression is
This has to do with the difference between a statement, and an expression. An expression has a value, whereas a statement does not.
Using your examples, notice these classifications:
StringBuilder sb; // statement
sb = new StringBuilder("test") // expression
StringBuilder sb = new StringBuilder("test"); // statement
Notice that only the middle portion is a expression.
Now we move onto your conditional statement. The syntax for using the not-equals operator is
expression != expression
So on both sides of the != you need something that actually has a value (this just makes sense). Ergo, you cannot have statements on either side of the operator. This is why the one version of your code works, while the other does not.