Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Numbers and Strings

Adding Numbers and Strings

Caution!

In Java, the ‘+’ operator serves dual purposes: for both addition and concatenation.

It performs addition when used with numerical values and concatenation when used with strings.

When you add two numbers in Java, the result will be another number.

Example

int x = 10;
int y = 20;
int z = x + y;  // z will be 30 (an integer/number)
Adding two strings results in their concatenation.

Example

String x = "10";
String y = "20";
String z = x + y; // z will be 1020 (a String)
Adding a number to a string results in string concatenation.

Example

String x = "10";
int y = 20;
String z = x + y; // z will be 1020 (a String)