views:

106

answers:

5

I've just seen this line of code in my housemates code.

Bool bool = method() > 0;

or

string name = "Tony";
boolean nameIsTony = name == "Tony";

This would result in nameIsTony becoming true.

So you can have an inline conditional statement?

What is this called?

+1  A: 
Bool bool = method() > 0;

assigns the result of the expression method() > 0 - which is a boolean value - to the variable bool.

Certainly this can be a bit hard to read sometimes - I prefer to use brackets in such cases, like

Bool bool = (method() > 0);

I guess "inline logical expression" is an apt name for it.

Péter Török
+6  A: 

name == "Tony" (or rather name.equals("Tony") as it should be) is a Boolean expression, so I'm guessing a Boolean expression is the closest term to what you are looking for.

It's an expression rather than a statement - it evaluates to something, rather than doing something. And it's not really conditional, as it always evaluates to a Boolean value - there's nothing conditional in its behaviour, just its result. So I'd go for "Boolean expression". The "inline" isn't really necessary.

David M
And what would be a not inline boolean expression? I mean, I'd say that it's just an expression. If we look at the whole statement, then it's a variable declaration with initializer.
polygenelubricants
Yes, good point.
David M
A: 

It's called "assignment".

František Žiačik
+2  A: 

its all about Operator Precedence

equality(==) has higher precedence than assignment(=).

so boolean nameIsTony = name == "Tony";

  1. first name == "Tony" expression evaluated as true

  2. and than assigned to nameIsTony.

Vivart
A: 

With few exceptions, expressions can be nested inside other expressions. Thus, each of these is boolean expression:

true
false
x > 0
(x >= 0) && (x <= 5)
100 == 100
Integer.parseInt("100") == 100
true & (true) & ((true)) & (((true)))

With regards to, say, local variable declaration, you can use any expression (as long as it's grammatically and semantically correct).

So there's really nothing special in something like:

boolean b = (x >= 0) && (x <= 5); // nothing special

In fact, you can also nest assignments like this:

x = y = z; // use judiciously!

Looking at the grammar

Consider the following excerpt from the Java Language Specification:

JLS 14.4 Local Variable Declaration Statements

A local variable declaration statement declares one or more local variable names.

LocalVariableDeclarationStatement:
        LocalVariableDeclaration ;

LocalVariableDeclaration:
        VariableModifiers Type VariableDeclarators

VariableDeclarators:
        VariableDeclarator
        VariableDeclarators , VariableDeclarator

VariableDeclarator:
        VariableDeclaratorId
        VariableDeclaratorId = VariableInitializer

VariableDeclaratorId:
        Identifier
        VariableDeclaratorId [ ]

VariableInitializer:
        Expression
        ArrayInitializer

Here we see that a local variable declaration statement allows an optional initialization for each identifier. Each VariableInitializer is grammatically either an Expression or an ArrayInitializer.

Thus, a someType anIdentifer = someExpression; in this context is simply a local variable statement with an initializer. someExpression can grammatically be any expression.


Note on ArrayInitializer

ArrayInitializer are things like { 1, 2, 3 } and { 4, 5, 6 }. Note that the ArrayInitializer grammatical rule is not part of the Expression production. This is why you can do, say, int[] x = { 1, 2, 3 }; in a declaration with initializer, but not later x = { 4, 5, 6 }; in an assignment.

polygenelubricants