In Java, the +
operator is used to perform addition. When the +
operator is used with two operands, it adds the operands and returns the result.
For example, the following code adds two int
variables and assigns the result to a third int
variable:
int a = 2; int b = 3; int c = a + b; // c is now 5Sourcw:eww.lautturi.com
The +
operator can also be used with other numeric data types, such as float
, double
, and long
. For example:
float d = 2.5f; float e = 3.0f; float f = d + e; // f is now 5.5 double g = 2.5; double h = 3.0; double i = g + h; // i is also 5.5
In these examples, the +
operator adds the operands and returns the result as the same data type as the operands.
In addition to its use as an arithmetic operator, the +
operator can also be used to concatenate strings in Java. When the +
operator is used with two string operands, it concatenates the operands and returns the result as a new string.