In Java, the addition operator ( ) can be used to add two numbers or concatenate two strings. For numeric types, performs numerical addition, and for string types, is used to concatenate strings. It should be noted that if the operand types are different, Java will automatically perform type conversion and convert the result to a larger type.
Addition operation in Java
In Java, the addition operator is used to combine two numbers or characters Add strings.
Numerical addition
For numeric values, the addition operator is
. For example:
<code class="java">int a = 5; int b = 10; int sum = a + b; // sum 等于 15</code>
String addition
For strings, the addition operator is used to concatenate two strings. For example:
<code class="java">String firstName = "John"; String lastName = "Doe"; String fullName = firstName + " " + lastName; // fullName 等于 "John Doe"</code>
Type conversion
If the addition operand types are different, Java will automatically perform type conversion. For example:
<code class="java">int a = 5; double b = 3.14; double sum = a + b; // sum 等于 8.14</code>
Other Notes
The above is the detailed content of How to write addition in java. For more information, please follow other related articles on the PHP Chinese website!