4

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
  • 2
    Other people have already answered your question, but I felt that you can also write "variable ++" or "variable --", which means that the variable count increases by 1, or decreases by 1, respectively. – Jave Dec 19 '11 at 15:29

4 Answers4

21

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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Dominik Sandjaja
  • 6,326
  • 6
  • 52
  • 77
16

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

This notation comes from C.

ethan
  • 780
  • 7
  • 20
AlexR
  • 114,158
  • 16
  • 130
  • 208
8

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

Jomoos
  • 12,823
  • 10
  • 55
  • 92
3

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.

Cubic
  • 14,902
  • 5
  • 47
  • 92