What syntax is called Bool bool = object.method ()> 0 in Java

I just saw this line of code in my code for neighbors.

Bool bool = method() > 0;

or

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

This will lead to what nameIsTonywill become true.

So can you have a built-in conditional expression?

What is called?

+3
source share
6 answers

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

, - - , -. , - , . "Boolean expression". "" .

+6

(==) , (=).

so boolean nameIsTony = name == "Tony";

  • first name == "Tony" true

  • nameIsTony.

+2
Bool bool = method() > 0;

method() > 0 - boolean - bool.

, - ,

Bool bool = (method() > 0);

, " " .

+1

, , Java, (?:), :

Employee emp = "manager".equals(profile) ? new Manager() : new Engineer();

, "", Employee emp (), Engineer().

, "manager".equals(profile) profile.equals( "manager" ), , - , NPE ( ), null ( NPE, , beter way , ).

+1

"".

0

. , :

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

, , , ( ).

- :

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

:

x = y = z; // use judiciously!

Java:

JLS 14.4

.

LocalVariableDeclarationStatement:
        LocalVariableDeclaration ;

LocalVariableDeclaration:
        VariableModifiers Type VariableDeclarators

VariableDeclarators:
        VariableDeclarator
        VariableDeclarators , VariableDeclarator

VariableDeclarator:
        VariableDeclaratorId
        VariableDeclaratorId = VariableInitializer

VariableDeclaratorId:
        Identifier
        VariableDeclaratorId [ ]

VariableInitializer:
        Expression
        ArrayInitializer

, . VariableInitializer a Expression, ArrayInitializer.

, a someType anIdentifer = someExpression; - . someExpression .


ArrayInitializer

ArrayInitializer - , { 1, 2, 3 } { 4, 5, 6 }. , ArrayInitializer Expression. , , int[] x = { 1, 2, 3 }; , x = { 4, 5, 6 }; .

0

Source: https://habr.com/ru/post/1749921/


All Articles