The first part of groovy study notes

 

productId = product?.productId ?: optProductId ?: request.getAttribute("productId");

 Because groovy is similar to java, at first glance, this statement feels very similar to java's ternary operator "?:", but I don't know what the judgment condition of this statement is. Later, I experimented and found that this statement is like this , with three variables:

 

 

produc?.productId

 

optProductId
request.getAttribute("productId")

  If only one of the three variables has a value, then the value of productId defined above is the value of the variable. If there are multiple variables with a value, the value of the variable with the first value is taken.

  Let's talk about the function of "?" in the first variable. In fact, this symbol and "??" in ftl are both judging non-null. The non-null here means that the variable is not null. If it is null, the program will continue. The execution does not report a null pointer error.

At first, I understood that if you don't define a variable and then use this operator, it will work. Later, I found that this operator will only work when it is null.

For a detailed introduction to groovy's operators, see this blog: groovy language specification value operator

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326943320&siteId=291194637