Java Numbers and Strings

Adding Numbers and Strings

Important Note: Java uses the + operator for both addition and concatenation.

Tutorials dojo strip
  • When you add numbers, the result is their sum.
  • When you add strings, the result is their concatenation (joining).




Adding Numbers

If you add two numbers, you’ll get a numeric result:

Example

JAVA




Adding Strings

If you add two strings, you’ll get a concatenated string:

Example

JAVA




Adding a Number and a String

If you add a number and a string, the result will still be a concatenated string:

Example

JAVA




Additional Information

String concatenation in Java can be performed using several methods. Besides using the + operator, you can use methods such as StringBuilder or StringBuffer for more efficient concatenation in loops or large applications.

Example with StringBuilder

JAVA

Efficiency Note

Using StringBuilder or StringBuffer is recommended for better performance when concatenating strings in loops or when dealing with large strings.

Tutorials dojo strip