Meaning of *= in Java

随声附和 提交于 2020-01-11 09:35:07

问题


I see an unfamiliar notation in the Android source code: *=

For example: density *= invertedRatio;

I am not familiar with the star-equals notation. Can somebody explain it?


回答1:


density *= invertedRatio; is a shortened version of density = density * invertedRatio;

This notation comes from C.




回答2:


In Java, the *= is called a multiplication compound assignment operator.

It's a shortcut for

density = density * invertedRatio;

Same abbreviations are possible e.g. for:

String x = "hello "; x += "world" // results in "hello world"
int y = 100; y -= 42; // results in y == 58

and so on.




回答3:


It is a shorthand assignment operator. It takes the following form:

variable op= expression;

is short form of

variable = variable op expression;

So,

density *= invertedRatio;

is equivalent to

density = density * invertedRatio;

See the following link for more info:

How to Use Assignment Operators in Java




回答4:


Just like Da said, it's short for density = density * invertedRatio; - it's nothing Android specific, it's standard Java. You will find this (and similar operators) in many languages with a C-like syntax.



来源:https://stackoverflow.com/questions/8563469/meaning-of-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!