Index:
- Arithmetic Operators
- Arithmetic Operators on Numbers
- Arithmetic Operators on Chars
- Arithmetic Operators on Strings
- [[#concatenation-with—operator|Concatenation with
+
Operator]]- Concatenation of Strings with Other Data Types
- Implicit Conversion in Concatenation
- Special Case Concatenation Order Matters
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and data. For example,
a + b;
Here, the +
operator is used to add two variables a and b. Similarly, there are various other arithmetic operators in Java.
Operator | Operation |
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo Operation (Remainder after division) |
Arithmetic Operators on Numbers
Division Operator
- Division two integers ⇒ Result: integer
- Division floating-point with at least one floating-point ⇒ Result: floating-point
Arithmetic Operators on Chars
When you perform an arithmetic operation on a char
, it gets implicitly cast to an int
value. This int
value corresponds to the Unicode value of the character.
Here’s an example:
Arithmetic Operators on Strings
In Java, strings are objects that represent sequences of characters. The Java platform provides the String
class to create and manipulate strings. While arithmetic operators like +
, -
, *
, /
, and %
are not directly applicable in the same way as they are with numeric data types, the +
operator can be used with strings, but it behaves differently. It is used for concatenation, which means joining strings together end-to-end to create a new string.
Concatenation with +
Operator
The +
operator can also be used to concatenate strings with other data types. When one of the operands is a string, the +
operator concatenates the string representation of the other operand.
Implicit Conversion in Concatenation
When using the +
operator with mixed data types where at least one operand is a string, Java automatically converts the other operand to its string representation before concatenation.
In this case, the integer 100
is automatically converted to the string "100"
before it is concatenated with the string "Score: "
.
Special Case: Concatenation Order Matters
When concatenating strings with other types in a single expression, the result might depend on the order and grouping of the operations due to how expressions are evaluated from left to right.
In the first line, the string "Sum: "
is concatenated with 10
resulting in "Sum: 10"
, which is then concatenated with 20
, resulting in the string "Sum: 1020"
. In the second example, because of the parentheses, 10 + 20
is evaluated first, resulting in 30
, which is then concatenated with "Sum: "
, producing the expected result of "Sum: 30"
.
Concatenation of Characters and Strings
In Java, the char
data type represents a single character. Unlike strings, when you try to concatenate two char
values using the +
operator, the result is not a string concatenation but a numeric addition, because char
values are treated as integers.
To concatenate char
values as strings, you can prepend or append an empty string (""
) to the expression. This forces Java to treat the operation as string concatenation rather than numeric addition.